0% found this document useful (0 votes)
9 views6 pages

Single LL

Uploaded by

srushtiraut62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Single LL

Uploaded by

srushtiraut62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1 #include<stdio.

h>
2 #include<stdlib.h>
3
4 struct Node
5 {
6 int data;
7 struct Node *link;
8 }*f = NULL;
9
10
11 void createList()
12 {
13 int n, x;
14 struct Node *temp, *q;
15
16 printf("\nEnter No. Of Elements:");
17 scanf("%d",&n);
18
19 for(int i=1 ; i<=n ; i++)
20 {
21 printf("\nEnter A Value:");
22 scanf("%d",&x);
23
24 temp = malloc(sizeof(struct Node));
25
26 temp->data = x;
27 temp->link = NULL;
28
29 if(f == NULL)
30 f=temp;
31 else
32 {
33 q=f;
34 while(q->link != NULL)
35 {
36 q = q->link;
37 q->link = temp;
38 }
39 }
40 }
41 printf("\nLinked List Of %d Of Nodes Created Successfully", n);
42 }
43
44
45 void addBegin(int x)
46 {
47 struct Node *temp;
48
49 temp = malloc(sizeof(struct Node));
50
51 temp->data = x;
52 temp->link = NULL;
53
54 if(f == NULL)
55 f = temp;
56 else
57 {
58 temp->link = f;
59 f = temp;
60 }
61 }
62
63
64 void insertPosition(int x, int p)
65 {
66 struct Node *temp, *q;
67 q = f;
68
69 for(int i=1 ; i<p-1 ; i++)
70 {
71 if(q = NULL)
72 {
73 printf("\nINVALID CHOICE!!");
74 return;
75 }
76 q = q->link;
77 }
78
79 if(q != NULL)
80 {
81 temp = malloc(sizeof(struct Node));
82 temp->data = x;
83 temp->link = q->link;
84 q->link = temp;
85
86 printf("\nValue Inserted Successfully");
87 }
88 else
89 {
90 printf("\nINVALID POSITION!!");
91 }
92
93 }
94
95
96 void reverseList()
97 {
98
99 struct Node *p, *c, *n;
100 p = NULL;
101 c = f;
102 n = NULL;
103
104 while(c != NULL)
105 {
106 n = c->link;
107 c->link = p;
108 p = c;
109 c = n;
110 }
111
112 f = p;
113
114
115 }
116
117
118
119
120
121
122
123 void deleteNode(int x)
124 {
125 struct Node *p, *q;
126
127 if(f == NULL)
128 {
129 printf("\nLinked List Does Not Exist");
130 }
131
132 f = q;
133
134 while(q != NULL)
135 {
136 if(q->data != x)
137 {
138 f = q;
139 q = q->link;
140 }
141 else
142 {
143 if(q == f)
144 {
145 f = f->link;
146 q->link = NULL;
147 }
148 else if(q->link == NULL)
149 {
150 p->link = NULL;
151 }
152 else
153 {
154 p->link = q->link;
155 q->link = NULL;
156 }
157
158 free(q);
159
160 printf("\nValue Deleted Successfully");
161 return ;
162 }
163 }
164
165 printf("\nVAlue Does Not Exist");
166 }
167
168
169 void display()
170 {
171 struct Node *q;
172
173 if(f == NULL)
174 printf("\nLinked List Does Not Exist");
175 else
176 {
177 q = f;
178
179 printf("\nValue From Linked List");
180
181 while(q != NULL)
182 {
183 printf("\t%d",q->data);
184 q = q->link;
185 }
186 }
187 }
188
189
190 void main()
191 {
192 int ch, x, p, a=0;
193
194
195 while(a==0)
196 {
197 printf("\nLinked List Operations:");
198 printf("\n1. Create List");
199 printf("\n2. Add At Beginning");
200 printf("\n3. Add At Any Position");
201 printf("\n4. Delete Any Node");
202 printf("\n5. Reverse A List");
203 printf("\n6. Display Node");
204 printf("\n7. Quit");
205
206 printf("\nEnter Any Choice:");
207 scanf("%d",&ch);
208
209 switch(ch)
210 {
211 case 1:
212 createList();
213 break;
214
215 case 2:
216 printf("\nEnter Any Value:");
217 scanf("%d", &x);
218
219 addBegin(x);
220 break;
221
222 case 3:
223 printf("\nEnter Any Number:");
224 scanf("%d", &x);
225
226 printf("\nEnter The Position:");
227 scanf("%d", &p);
228
229 insertPosition(x , p);
230 break;
231
232 case 4:
233 printf("\nEnter Any No. Which Has To Be
Deleted");
234 scanf("%d", &x);
235
236 deleteNode(x);
237 break;
238
239 case 5:
240 reverseList();
241 break;
242
243 case 6:
244 display();
245 break;
246
247 case 7:
248 a=1;
249
250 default :
251 printf("\nWARNING !! INVALID CHOICE!!");
252
253 }
254 }
255 }
256
257
258

You might also like