-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path09 ● Single Linked List.cs
208 lines (204 loc) · 5.03 KB
/
09 ● Single Linked List.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
namespace Single_Linked_List
{
class Node
{
public int data;
public Node next;
}
class List
{
public int coun;
public Node head;
public List()
{
head = null;
coun = 0;
}
public void add_front(int item)
{
Node temp = new Node();
temp.data = item;
temp.next = head;
head = temp;
coun++;
}
public void add_back(int item)
{
Node temp = new Node();
temp.data = item;
if(head==null)
{
head = temp;
}
Node p = head;
while (p.next != null)
{
p = p.next;
}
p.next = temp;
temp.next = null;
coun++;
}
public void insert_after_value(int item,int search)
{
Node p = head;
while(p.data!=search)
{
p = p.next;
}
Node temp = new Node();
temp.data = item;
temp.next = p.next;
p.next = temp;
coun++;
}
public void insert_before_value(int item, int search)
{
Node p = head;
while (p.next.data != search)
{
p = p.next;
}
Node temp = new Node();
temp.data = item;
temp.next = p.next;
p.next = temp;
coun++;
}
public void insert_in_position(int item, int pos)
{
Node p = head;
int p1 = 1;
while (p1!=pos-1)
{
p = p.next;
p1++;
}
Node temp = new Node();
temp.data = item;
temp.next = p.next;
p.next = temp;
coun++;
}
public void search(int item)
{
Node p = head;
bool found = false;
while(p.next!=null)
{
if(item == p.data)
{
found = true;
break;
}
p = p.next;
}
if (found)
Console.WriteLine("Found {0} in the linkedlist ", item);
else
Console.WriteLine("Not found in the linked list ");
}
public void delete_in_position(int pos)//مش جاي في الميد
{
Node p = head;
int p1 = 1;
while(p1+1 < pos)
{
p = p.next;
p1++;
}
p.next = p.next.next;
coun--;
}
public void delete_by_item(int item)//مش جاي في الميد
{
Node p = head;
int p2 = 1;
if (p.data == item)
{
delete_front();
}
else
{
while (p.data != item)
{
p = p.next;
p2++;
}
delete_in_position(p2);
}
coun--;
}
public void delete_last()//مش جاي في الميد
{
Node p = head;
while (p.next.next!=null)
{
p = p.next;
}
p.next = null;
coun--;
}
public void delete_front()
{
head = head.next;
coun--;
}
public void reverse()
{
Node prev = null, current = head, odam = null;
while(current != null)
{
odam = current.next;
current.next= prev;
prev = current;
current = odam;
}
head = prev;
}
public void print()
{
Node p = head;
while(p.next!=null)
{
Console.Write(p.data + " ");
p = p.next;
}
Console.Write(p.data + "\n");
}
public void size()
{
Console.WriteLine("LinkedList size = {0}",coun );
}
}
class Program
{
static void Main(string[] args)
{
List L = new List();
L.add_back(10);
L.add_back(11);
L.add_back(12);
L.add_front(13);
L.add_front(14);
L.add_back(15);
L.delete_by_item(14);
L.size();
L.insert_after_value(999, 15);
L.insert_in_position(999, 3);
L.insert_before_value(919, 10);
L.reverse();
L.size();
L.delete_in_position(2);
L.delete_last();
L.delete_last();
L.delete_front();
L.size();
L.delete_front();
L.print();
L.search(919);
L.size();
}
}
}