-
Notifications
You must be signed in to change notification settings - Fork 1
/
slinkedlist.cpp
300 lines (252 loc) · 7.22 KB
/
slinkedlist.cpp
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//ctrl shift h opens git-plus menu
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* link;
}; // CREATING A STRUCTURE NODE
typedef struct node NODE; // defining struct node as one word "NODE"
NODE* head=NULL; //note: *warning dont declare it inside main without passing it to other functions!
// note: *DONT ALLOCATE HEAD ITS JUST A NORMAL POINTER*
NODE* current=NULL; //note: *warning dont declare it inside main without passing it to other functions!
NODE* temp=NULL;//note: *warning dont declare it inside main without passing it to other functions!
int CreateList(); // function for creating list
int PrintList(); // function to print the singly linked list
int CountNodes(); //function to count nodes
int InsertNode();// function to insert node at selected position
int DeleteNode();//function to delete node from selected position
int SearchNode();//finding node by data
int PrintReverse(); //print the list in reverse without actually reversing the list
int main()
{
int choice=0;
current=(NODE*)malloc(sizeof(NODE));//allocation of traversing variable must be done only once
while(choice!=9)
{
printf("\nChoose what to do: \n");
printf("\n1.Create List\n2.Print List \n3.Count nodes\n4.Insert Node \n5.Delete Node \n6.Search Node by value \n7.Print List in reverse \n9. Exit \n");
scanf("%d",&choice);
switch (choice) {
case 1: CreateList();
break;
case 2: PrintList();
break;
case 3: CountNodes();
break;
case 4: InsertNode();
break;
case 5: DeleteNode();
break;
case 6: SearchNode();
break;
case 7: PrintReverse();
break;
case 9: exit;
break;
default: printf("Enter Proper Input");
break;
}
}
}
int CreateList()
{
int n,loop,value;
printf("How many nodes do you want to add? ");
scanf("%d",&n); // ASKING USER THE LENGTH OF THE LIST
for(loop=0;loop<n;loop++){
temp=(NODE*)malloc(sizeof(NODE));
temp->link=NULL;
printf("\n Enter value at node %d= ",loop+1);
scanf("%d",&temp->data);
if(head==NULL)
{
head=temp;
}
else{
current->link=temp;
}
current=temp;
}//end of for
}
//INT CreateList ENDS
int PrintList(){
current=head;
while(current!=NULL){ //condition such that current will reach last block and become equal to NULL and then stop
printf("%2d ->" ,current->data );
current=current->link;
}
}
//INT PrintList ENDS
int count=0;
int CountNodes(){
current=head;
while(current!=NULL){
count++;
current=current->link;
}
printf("Number of nodes is= %d \n",count);
return count;
}// INT CountNodes ENDS
int InsertNode(){
int position,count;
temp=NULL; // avoiding temp pointing to garbage value
temp=(NODE*)malloc(sizeof(NODE)); // allocating temp
count=CountNodes(); // because we need size of list
current=head;
printf("Please enter the value you want at new node\n");
scanf("%d",&temp->data); // storing data directly in temp
printf("At what position do you want to add a node?");
printf("\n1. First \n2. Middle \n3. Last \n4.Custom");
scanf("%d",&position);
switch(position){
case 1://first position node deleted
{ temp->link=NULL;
current=head;
current=current->link;//saving location of second node
head=current->link; //linking temp to second node by overriding existing node
break;
}
case 2:
{
temp->link=NULL;
current=head;
for(int x=0;x<=count/2;x++){
if(x=count/2-1){
temp=current;
}
current=current->link;
}
temp->link=current->link;
break;
}
case 3:
{
temp->link=NULL;
current=head;
while(current->link!=NULL){
current=current->link;
}
current->link=temp;
temp->link=NULL;
break;
}
case 4:
{ int custom;
temp->link=NULL;
current=head;
printf("At what location do you want to insert node?\n");\
scanf("%d",&custom);
for(int x=0;x<custom;x++){
current=current->link;
}
current->link=temp;
temp->link=NULL;
break;
}
}
printf("Lets check the list now\n");
PrintList();
}//InsertNode ENDS
int DeleteNode(){
int position;
temp=NULL; // avoiding temp pointing to garbage value
temp=(NODE*)malloc(sizeof(NODE)); // allocating temp
count=CountNodes(); // because we need size of list
current=head;
printf("\n");
printf("Current number of nodes is as below\n");
printf("\n");
CountNodes();
printf("Which Node do you want to delete?");
printf("\n1. Beginning \n2. Middle \n3. Last \n4.Custom ");
scanf("%d",&position);
switch(position){
case 1://delete node at beginning
{ temp->link=NULL;
current=head; //saving current first node in current
head=temp;// changing first node to temp
temp=current->link;//linking new first (originally second)with
break;
}
case 2:
{//delete middle node
temp->link=NULL;
current=head;
for(int x=0;x<=count/2-1;x++){
current=current->link;// for traversing till middle-1
temp=current; //save middle-1 node as temp
}
current=current->link; //traverse to middle node
temp->link=current->link;
break;
}
case 3:
{//delete last node
temp->link=NULL;//initializing temp
current=head;//start traversing
temp=current; //first save currents old value (present node-1) in NULL
current=current->link;//go to next node
if (current->link==NULL) {
/* as we reach last node
current is last node
but temp is the second last node*/
temp->link=NULL;
/*we over-ride the link of last node
present in second last node:
in order to remove the last node*/
}
break;
}
case 4://removing value at custom node!
{ int custom;
temp->link=NULL;//initializing temp
current=head;//start traversing
printf("Which node do you wish to remove\n");\
scanf("%d",&custom);
for(int x=0;x<custom;x++){
temp=current; //first save currents old value (present node-1) in NULL
current=current->link;//go to next node
}
/* as we reach last node
current is last node
but temp is the second last node*/
temp->link=NULL;
/*we over-ride the link of last node
present in second last node:
in order to remove the last node*/
break;
}
}
printf("\n");
printf("Let's Check the list now!\n");
PrintList();
}
int SearchNode(){//finding a node by value
int value;
printf("Enter value to be found\n");
scanf("%d",&value );
int n=CountNodes();
current=head;//start traversing
for(int x=0;x<n;x++){
if(current->data=value){
printf("The element %d you searched is at node number%d\n",value,x+1 );
printf("You can check it as below\n");
PrintList();
}//if ends
current=current->link;//traverse to next node
}
}
int PrintReverse(){//print linked list in reverse without actually reversing
NODE* rev;
int i,j,n,value;
n=CountNodes();
printf("The reversed list is\n" );
for(i=n;i>0;i--){
rev=head;
for(j=1;j<i;j++){
rev=rev->link;
}
printf("i=%d j=%d \n",i,j );
printf("%d-> \n",rev->data);
}
}