1 | #include<stdio.h> | |
2 | #include<stdlib.h> | |
3 | struct node | |
4 | { | |
5 | int data; | |
6 | struct node *next; | |
7 | }; | |
8 | struct node *header=NULL; | |
9 | struct node *ptr=NULL,*temp=NULL; | |
10 | void create(); | |
11 | void insert_at_front(); | |
12 | void insert_at_end(); | |
13 | void insert_at_middle(); | |
14 | void delete_at_front(); | |
15 | void delete_at_end(); | |
16 | void delete_at_middle(); | |
17 | void display(); | |
18 | int main() | |
19 | { | |
20 | int ch; | |
21 | header=(struct node*)malloc(sizeof(struct node)); | |
22 | header->next=NULL; | |
23 | ptr=header; | |
24 | //clrscr(); | |
25 | printf("\n\n1.create \n2- insert at front\n3-insert at middle\n4-insert at end\n5-display\n6.delete_at_front\n7.delete_at_middle\n8.delete_at_end\n9-exit"); | |
26 | while(1) | |
27 | { | |
28 | printf("\nenter ur choice : "); | |
29 | scanf("%d",&ch); | |
30 | switch(ch) | |
31 | { | |
32 | case 1: create(); | |
33 | break; | |
34 | case 2: insert_at_front(); | |
35 | break; | |
36 | case 3: insert_at_middle(); | |
37 | break; | |
38 | case 4: insert_at_end(); | |
39 | break; | |
40 | case 5: display(); | |
41 | break; | |
42 | case 6: delete_at_front(); | |
43 | break; | |
44 | case 7: delete_at_middle(); | |
45 | break; | |
46 | case 8: delete_at_end(); | |
47 | break; | |
48 | case 9: exit(0); | |
49 | default: printf("wrong choice"); | |
50 | } | |
51 | } | |
52 | } | |
53 | void create() | |
54 | { | |
55 | char ch; | |
56 | while(1) | |
57 | { | |
58 | insert_at_end(); | |
59 | printf("if u want to continue _press 'y' or 'n' :"); | |
60 | scanf("\n%c",&ch); | |
61 | if(ch=='y'||ch=='Y') | |
62 | continue; | |
63 | else | |
64 | break; | |
65 | } | |
66 | } | |
67 | void insert_at_front() | |
68 | { | |
69 | int ele; | |
70 | struct node *new1; | |
71 | new1=(struct node*)malloc(sizeof(struct node)); | |
72 | if(new1==NULL) | |
73 | { | |
74 | printf("out of space"); | |
75 | return; | |
76 | } | |
77 | printf("enter ele:"); | |
78 | scanf("%d",&ele); | |
79 | new1->next=header->next; | |
80 | header->next=new1; | |
81 | new1->data=ele; | |
82 | printf("inserted successfully"); | |
83 | } | |
84 | void insert_at_end() | |
85 | { | |
86 | struct node *new1; | |
87 | int ele; | |
88 | new1=(struct node*)malloc(sizeof(struct node)); | |
89 | ptr=header; | |
90 | if(new1==NULL) | |
91 | { | |
92 | printf("out of space"); | |
93 | return; | |
94 | } | |
95 | printf("enter Inserted ele:"); | |
96 | scanf("%d",&ele); | |
97 | /* Move the pointer to upto end of the node*/ | |
98 | while(ptr->next!=NULL) | |
99 | { | |
100 | ptr=ptr->next; | |
101 | } | |
102 | new1->next=NULL; | |
103 | ptr->next=new1; | |
104 | new1->data=ele; | |
105 | printf("inserted successfully"); | |
106 | } | |
107 | void insert_at_middle() | |
108 | { | |
109 | struct node *new1; | |
110 | int ele; | |
111 | int pos,count=0; | |
112 | new1=(struct node*)malloc(sizeof(struct node)); | |
113 | ptr=header; | |
114 | if(new1==NULL) | |
115 | { | |
116 | printf("out of space"); | |
117 | return; | |
118 | } | |
119 | printf("enter Inserted ele:"); | |
120 | scanf("%d",&ele); | |
121 | printf("enter where it to be inserted at what position number:"); | |
122 | scanf("%d",&pos); | |
123 | /* Calculate the no of elements in the list */ | |
124 | while(ptr->next!=NULL) | |
125 | { | |
126 | count++; | |
127 | ptr=ptr->next; | |
128 | } | |
129 | /*Compare the entered_positions number is valid or not using_count the no.of elements in the list*/ | |
130 | if(count<pos) | |
131 | { | |
132 | printf("pos is out of range"); | |
133 | return; | |
134 | } | |
135 | ptr=header; | |
136 | count=0; | |
137 | /*Move the ptr upto particular entered position number*/ | |
138 | while(count<pos-1) | |
139 | { | |
140 | ptr=ptr->next; | |
141 | count++; | |
142 | } | |
143 | new1->next=ptr->next; | |
144 | ptr->next=new1; | |
145 | new1->data=ele; | |
146 | printf("inserted successfully"); | |
147 | } | |
148 | void display() | |
149 | { | |
150 | ptr=header; | |
151 | /* Check the List is Empty or not*/ | |
152 | if(ptr->next==NULL) | |
153 | { | |
154 | printf("list is empty"); | |
155 | return; | |
156 | } | |
157 | while(ptr->next!=NULL) | |
158 | { | |
159 | printf("%d -> ",ptr->next->data); | |
160 | ptr=ptr->next; | |
161 | } | |
162 | printf("NULL"); | |
163 | } | |
164 | void delete_at_front() | |
165 | { | |
166 | ptr=header; | |
167 | /* Check the List is Empty or not*/ | |
168 | if(ptr->next==NULL) | |
169 | { | |
170 | printf("list is empty deletion is not possible"); | |
171 | return; | |
172 | } | |
173 | temp=ptr->next; | |
174 | ptr->next=ptr->next->next; | |
175 | free(temp); | |
176 | printf("deleted succssfully"); | |
177 | } | |
178 | void delete_at_end() | |
179 | { | |
180 | ptr=header; | |
181 | /* Check the List is Empty or not*/ | |
182 | if(ptr->next==NULL) | |
183 | { | |
184 | printf("list is empty deletion is not possible"); | |
185 | return; | |
186 | } | |
187 | /*Move the ptr upto last but one node*/ | |
188 | while(ptr->next->next!=NULL) | |
189 | { | |
190 | ptr=ptr->next; | |
191 | } | |
192 | temp=ptr->next; | |
193 | ptr->next=NULL; | |
194 | free(temp); /* Clear the Memory*/ | |
195 | printf("deleted_succssfully"); | |
196 | } | |
197 | void delete_at_middle() | |
198 | { | |
199 | int pos,count; | |
200 | ptr=header; | |
201 | /* Check the List is Empty or not*/ | |
202 | if(ptr->next==NULL) | |
203 | { | |
204 | printf("list is empty_deletion is not possible"); | |
205 | return; | |
206 | } | |
207 | count=0; | |
208 | /* Calculate the no.of elements in the list */ | |
209 | while(ptr->next!=NULL) | |
210 | { | |
211 | ptr=ptr->next; | |
212 | count++; | |
213 | } | |
214 | printf("enter deleted_position number:"); | |
215 | scanf("%d",&pos); | |
216 | /*Compare the entered_positions number is valid or not using count the no.of elements in the list*/ | |
217 | if(count<pos) | |
218 | { | |
219 | printf("position is out of range"); | |
220 | return; | |
221 | } | |
222 | ptr=header; | |
223 | count=0; | |
224 | /*Move the ptr upto particular_entered _position number*/ | |
225 | while(ptr->next->next!=NULL&&count<pos-1) | |
226 | { | |
227 | ptr=ptr->next; | |
228 | count++; | |
229 | } | |
230 | temp=ptr->next; | |
231 | ptr->next=ptr->next->next; | |
232 | free(temp); | |
233 | printf("deleted succssfully"); | |
234 | } |
OUTPUT: