-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinglylinkedlist.cpp
More file actions
264 lines (235 loc) · 4.74 KB
/
Copy pathsinglylinkedlist.cpp
File metadata and controls
264 lines (235 loc) · 4.74 KB
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
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct snglist
{
struct node *head;
struct node *tail;
}snglist;
void init(snglist *l)
{
l->head=l->tail='\0';
}
void addfirst(snglist *l,int data)
{
node *p=(node*)malloc(sizeof(node));
p->data=data;
p->next='\0';
if(l->head=='\0')
{
l->head=p;
l->tail=p;
}
else
{
p->next=l->head;
l->head=p;
}
}
void addlast(snglist *l,int data)
{
node *p=(node*)malloc(sizeof(node));
p->data=data;
p->next='\0';
if(l->head=='\0')
{
l->head=p;
l->tail=p;
}
else
l->tail->next=p;
l->tail=p;
}
void insat(snglist *l,int data)
{
node *p=(node*)malloc(sizeof(node));
node *current;
p->data=data;
current=l->head;
if(l->head==NULL)
{
l->head=p;
l->tail=p;
}
while(p->data < current->data)
{
current=current->next;
}
p->next=current->next;
current->next=p;
}
void display(snglist *l)
{
node *current;
current=l->head;
int i=1;
while(current!=NULL)
{
printf("%d. %p [%d | %p] \n",i,current,current->data,current->next);
current=current->next;
i++;
}
}
int deletefirst(snglist *l)
{
int x;
node *current;
current=l->head; //you declared current w malloc here ,were not actually allocating any momry
if(l->head==NULL)
{
printf("LINKED LIST IS EMPTY \n");
return 0;
}
else if(l->head == l->tail) //only 1 elemnt exists
{
x=current->data;
l->head=l->tail=NULL;
free(current);
return x;
}
else{ // mre than 1 element exists
x=current->data; // <--- yeh wali line to likhi hi nai thi
l->head=current->next;
free (current);
return x; //else if ka hi locha tha
}
}
int deletelast(snglist *l)
{
node *current=l->head;
node *pred=current;
int x;
if(l->head==NULL)
{
printf("LINKED LIST IS EMPTY \n");
return 0;
}
else if(l->head==l->tail) // 1 element
{
x=current->data;
l->head=l->tail=NULL;
free(current);
return x;
}
else
{
while(current->next != NULL)
{
pred=current; //previous element
current=current->next; //increments current (traversing)
}
x=current->data; //YAHA PE BHI SAME YE LINE NAHI LIKHI THI
l->tail=pred;
pred->next=NULL;
free(current); //apne pred ko free karva rahe the lekin CURRENT FREE HOGA
return x;
}
}
void search(snglist *l,int target)
{
node *current=l->head;
while(current!=NULL)
{
if(current->data==target)
{
printf("match found \n");
return;
}
current=current->next;
}
printf("match not found \n");
}
void reverse(snglist *l,snglist *lr)
{
int x;
node *current = l->head;
while(current != NULL)
{
x=current->data;
addfirst(lr,x);
current=current->next;
}
return ;
}
int count(snglist *l)
{
int count=0;
node *current=l->head;
while(current != NULL)
{
count++;
current=current->next; // bhai ye line to main hai tu likhna bhul gaya bc
}
return count;
}
void menu()
{
while(1)
{
int choice,num1,num2,num3,searchvalue;
snglist list,list2;
printf("-------- SINGLY LINKED LIST MENU-------\n");
printf("\n 1.INSERT-insert at first \n 2.INSEND-insert at last \n 3.INSAT-insert in middle \n 4.DELETE FIRST \n 5.DELETE LAST \n 6.REVERSE \n 7.SEARCH \n 8.COUNT \n 9.DISPLAY \n 10.EXIT \n");
printf("enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Insert a value at first: ");
scanf("%d",&num1);
addfirst(&list,num1);
break;
case 2:
printf("Insert a value at last: ");
scanf("%d",&num2);
addlast(&list,num2);
break;
case 3:
printf("Insert a value at middle: ");
scanf("%d",&num3);
insat(&list,num3);
break;
case 4:
printf("\n deleted value is %d",deletefirst(&list));
break; // this function returns an int type value, you just called the funcin, never printed the deleted value
case 5:
printf("\n deleted value is %d",deletelast(&list));
break;
case 6:
reverse(&list,&list2);
printf("\n the reversed list is: \n ");
display(&list2);
break;
case 7:
printf("Enter the value you wanna search: ");
scanf("%d",&searchvalue);
search(&list,searchvalue);
break;
case 8:
printf("the count is %d \n",count(&list));
break;
case 9:
printf("displaying value........\n");
display(&list);
break;
case 10:
printf("GOODBYE SHITHEAD.....");
exit(0);
break;
default:
printf("Invalid input \n");
}
}
}
int main()
{
snglist list,list2;
init(&list); // you forgot to initialze lol
init(&list2);
menu();
return 0;
}