Linked list in C.

 

01  #include<stdio.h>
02 #include<stdlib.h>
03
04 int size_counter();
05
06 struct node
07 {
08 int data;
09 struct node *next;
10 }*start;
11
12 void insertEnd(int num)
13 {
14 struct node *temp,*right;
15 temp= (struct node *)malloc(sizeof(struct node));
16 temp->data=num;
17 right=(struct node *)start;
18
19 while(right->next != NULL)
20 right=right->next;
21 right->next =temp;
22 right=temp;
23 right->next=NULL;
24 }
25
26
27
28 void insertBegin( int num )
29 {
30 struct node *temp;
31 temp=(struct node *)malloc(sizeof(struct node));
32 temp->data=num;
33 if (start== NULL)
34 {
35 start=temp;
36 start->next=NULL;
37 }
38 else
39 {
40 temp->next=start;
41 start=temp;
42 }
43 }
44 void insertSepc(int num, int loc)
45 {
46 int i;
47 struct node *temp,*left,*right;
48 right=start;
49
50 if(loc<=1)
51 {
52 insertBegin(num);
53 return;
54 }
55
56 for(i=1;i<loc;i++)
57 {
58 left=right;
59 right=right->next;
60
61 if(right==NULL)
62 {
63 insertEnd(num);
64 return;
65 }
66 }
67 temp=(struct node *)malloc(sizeof(struct node));
68 temp->data=num;
69 temp->next=left->next;
70 left->next=temp;
71
72 }
73
74 int delete_start()
75 {
76 struct node *temp;
77 if(start==NULL)
78 return 0;
79 else
80 {
81 temp=start;
82 start=start->next;
83 printf("No deleted is %d",temp->data);
84 return 1;
85 }
86 }
87
88
89 int delete_end()
90 {
91 struct node *temp,*prev;
92 if(start==NULL)
93 return 0;
94 else if((start)->next==NULL)
95 {
96 temp=start;
97 start=NULL;
98 printf("No deleted is %d",temp->data);
99 return 1;
100 }
101 else
102 {
103 prev=start;
104 temp=start->next;
105
106 while(temp->next!=NULL)
107 {
108 prev=temp;
109 temp=temp->next;
110 }
111 prev->next=NULL;
112 printf("No deleted is %d",temp->data);
113 return 1;
114 }
115 }
116
117 int delete_item(int num)
118 {
119 struct node *temp, *prev;
120 temp=start;
121
122 while(temp!=NULL)
123 {
124 if(temp->data==num)
125 {
126 if(temp==start)
127 {
128 start=temp->next;
129 free(temp);
130 return 1;
131 }
132 else
133 {
134 prev->next=temp->next;
135 free(temp);
136 return 1;
137 }
138 }
139
140 else
141 {
142 prev=temp;
143 temp= temp->next;
144 }
145 }
146 return 0;
147 }
148
149
150 void display(struct node *r)
151 {
152 r=start;
153 if(r==NULL)
154 {
155 return;
156 }
157 while(r!=NULL)
158 {
159 printf("%d ",r->data);
160 r=r->next;
161 }
162 printf("n");
163 }
164
165
166 int size_counter()
167 {
168 struct node *n;
169 int c=0;
170 n=start;
171 while(n!=NULL)
172 {
173 n=n->next;
174 c++;
175 }
176 return c;
177 }
178
179
180 int main()
181 {
182 int ch,num,pos;
183 struct node *n;
184 start=NULL;
185
186 printf("nLinked Listnn");
187
188 do
189 {
190 printf("nSelect Operationnn");
191 printf("1.Insert at beginning.n");
192 printf("2.Insert at end.n");
193 printf("3.Insert at a specific position.n");
194 printf("4.Delete from beginning.n");
195 printf("5.Delete from end.n");
196 printf("6.Delete a specific item.n");
197 printf("7.Displayn");
198 printf("8.Sizen");
199 printf("9.Exitn");
200
201 printf("Enter your choice : ");
202 scanf("%d",&ch);
203
204 switch(ch)
205 {
206 case 1:
207 printf("Enter the number to insert : ");
208 scanf("%d",&num);
209 insertBegin(num);
210 break;
211
212 case 2:
213 printf("Enter the number to insert : ");
214 scanf("%d",&num);
215 insertEnd(num);
216 break;
217
218 case 3:
219 printf("Enter the number to insert : ");
220 scanf("%d",&num);
221 printf("Enter the position where to insert : ");
222 scanf("%d",&pos);
223 insertSepc(num,pos);
224 break;
225
226 case 4:
227 delete_start();
228 break;
229
230 case 5:
231 delete_end();
232 break;
233
234 case 6:
235 if(start==NULL)
236 printf("List is Emptyn");
237 else
238 {
239 printf("Enter the number to delete : ");
240 scanf("%d",&num);
241 if(delete_item(num))
242 printf("%d deleted successfullyn",num);
243 else
244 printf("%d not found in the listn",num);
245 }
246 break;
247
248
249 case 7:
250 if(start==NULL)
251 {
252 printf("List is Emptyn");
253 }
254 else
255 {
256 printf("Element(s) in the list are : ");
257 }
258 display(n);
259 break;
260
261 case 8:
262 printf("Size of the list is %dn",size_counter());
263 break;
264
265 case 9:
266 printf("End of Programn");
267
268
269 }
270 }while(ch!=9);
271 return 0;
272 }
 
 
OUTPUT :

Linked List

Select Operation

1.Insert at beginning.
2.Insert at end.
3.Insert at a specific position.
4.Delete from beginning.
5.Delete from end.
6.Delete a specific item.
7.Display
8.Size
9.Exit
Enter your choice : 1
Enter the number to insert : 10

Select Operation

1.Insert at beginning.
2.Insert at end.
3.Insert at a specific position.
4.Delete from beginning.
5.Delete from end.
6.Delete a specific item.
7.Display
8.Size
9.Exit
Enter your choice : 2
Enter the number to insert : 45

Select Operation

1.Insert at beginning.
2.Insert at end.
3.Insert at a specific position.
4.Delete from beginning.
5.Delete from end.
6.Delete a specific item.
7.Display
8.Size
9.Exit
Enter your choice : 7
Element(s) in the list are : 10 45

Select Operation

1.Insert at beginning.
2.Insert at end.
3.Insert at a specific position.
4.Delete from beginning.
5.Delete from end.
6.Delete a specific item.
7.Display
8.Size
9.Exit
Enter your choice : 3
Enter the number to insert : 15
Enter the position where to insert : 2

Select Operation

1.Insert at beginning.
2.Insert at end.
3.Insert at a specific position.
4.Delete from beginning.
5.Delete from end.
6.Delete a specific item.
7.Display
8.Size
9.Exit
Enter your choice : 7
Element(s) in the list are : 10 15 45

Select Operation

1.Insert at beginning.
2.Insert at end.
3.Insert at a specific position.
4.Delete from beginning.
5.Delete from end.
6.Delete a specific item.
7.Display
8.Size
9.Exit
Enter your choice : 9
 
 

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *