1: /*
2: ==============================================================
3: Name : Doubly Linked List
4: Author : Mujahid, Shivkumar
5: Version :
6: Copyright : Your copyright notice
7: Description : Hello World in C, Ansi-style
8: ===========================================================
9: */
10: #include<stdio.h>
11: #include<stdlib.h>
12: typedef struct dnode
13: {
14: int data;
15: struct dnode *next,*prev;
16: }dnode;
17: dnode *creat();
18: dnode *insert_b(dnode *head,char x);
19: dnode *insert_e(dnode *head,char x);
20: dnode *insert_m(dnode *head,char x);
21: dnode *delete_b(dnode *head);
22: dnode *delete_e(dnode *head);
23: dnode *delete_m(dnode *head);
24: void displayforward(dnode *head);
25: void displaybackward(dnode *head);
26: void main()
27: {
28: int m,n,choice;
29: char x;
30: dnode *head;
31: //clrscr();
32: head=creat();
33: displayforward(head);
34: do
35: {
36: printf("\n_________________________________________________________\n");
37: printf("1. Insert\n2. Delete\n3. Display Forward\n4. Display Backward\n");
38: printf("Enter Your choice: ");
39: scanf("%d",&n);
40: printf("\n");
41: switch(n)
42: {
43: case 1:
44: printf("\n\nInsert data-\n1. Beginning\n2. End\n3. In Middle");
45: printf("\nEnter which operation you want to perform: ");
46: scanf("%d",&m);
47: printf("\nEnter the data to be insered: ");
48: scanf("%s",&x);
49: switch(m)
50: {
51: case 1:
52: head=insert_b(head,x);
53: break;
54: case 2:
55: insert_e(head,x);
56: break;
57: case 3:
58: insert_m(head,x);
59: break;
60: default:
61: printf("Invalid option entered..!!!");
62: break;
63: }
64: displayforward(head);
65: break;
66: case 2:
67: printf("\n\nDelete data-\n1. Beginning\n2. End\n3. In Middle");
68: printf("\nEnter which operation you want to perform: ");
69: scanf("%d",&m);
70: switch(m)
71: {
72: case 1:
73: head=delete_b(head);
74: break;
75: case 2:
76: delete_e(head);
77: break;
78: case 3:
79: delete_m(head);
80: break;
81: default:
82: printf("Invalid option entered..!!!");
83: break;
84: }
85: displayforward(head);
86: break;
87: case 3:
88: displayforward(head);
89: break;
90: case 4:
91: displaybackward(head);
92: break;
93: default:
94: printf("Invalid option entered...!!!");
95: break;
96: }
97: printf("\nDo you want to continue???(0/1): ");
98: scanf("%d",&choice);
99: }while(choice==1);
100: }
101: dnode *creat()
102: {
103: dnode *head,*p,*q;
104: char x[10];
105: int i=0;
106: head=NULL;
107: printf("\nEnter the string: \n");
108: scanf("%s",&x);
109: while(x[i]!='\0')
110: {
111: if(head==NULL)
112: {
113: head=(dnode*)malloc(sizeof(dnode));
114: head->next=head->prev=NULL;
115: head->data=x[i];
116: q=head;
117: }
118: else
119: {
120: p=(dnode*)malloc(sizeof(dnode));
121: p->prev=q;
122: q->next=p;
123: p->next=NULL;
124: q=p;
125: p->data=x[i];
126: }
127: i++;
128: }
129: return head;
130: }
131: dnode *insert_b(dnode *head,char x)
132: {
133: dnode *p;
134: p=(dnode*)malloc(sizeof(dnode));
135: p->data=x;
136: if(head==NULL)
137: {
138: head=p;
139: head->next=head->prev=NULL;
140: }
141: else
142: {
143: p->next=head;
144: head->prev=p;
145: p->prev=NULL;
146: head=p;
147: }
148: return head;
149: }
150: dnode *insert_e(dnode *head,char x)
151: {
152: dnode *p,*q;
153: p=(dnode*)malloc(sizeof(dnode));
154: p->data=x;
155: if(head==NULL)
156: {
157: head=p;
158: head->next=head->prev=NULL;
159: }
160: else
161: {
162: for(q=head;q->next!=NULL;q=q->next);
163: q->next=p;
164: p->prev=q;
165: p->next=NULL;
166: }
167: return head;
168: }
169: dnode *insert_m(dnode *head,char x)
170: {
171: dnode *p,*q;
172: char y;
173: p=(dnode*)malloc(sizeof(dnode));
174: p->data=x;
175: p->next=NULL;
176: printf("\nAfter which number you want to insert: ");
177: y=getchar();
178: for(q=head;q!=NULL && q->data!=y;q=q->next);
179: if(q->data==y)
180: {
181: p->next=q->next;
182: p->prev=q;
183: p->next->prev=p;
184: p->prev->next=p;
185: }
186: else
187: printf("\nData not found...!!!");
188: return head;
189: }
190: dnode *delete_b(dnode *head)
191: {
192: dnode *p;
193: if(head==NULL)
194: {
195: printf("\nUnderflow.............Empty Linked List");
196: return head;
197: }
198: p=head;
199: if(head->next==NULL)
200: {
201: free (p);
202: head=NULL;
203: }
204: else
205: {
206: head=head->next;
207: head->prev=NULL;
208: free (p);
209: }
210: return head;
211: }
212: dnode *delete_e(dnode *head)
213: {
214: dnode *p;
215: if(head==NULL)
216: {
217: printf("\nUnderflow.............Empty Linked List");
218: return head;
219: }
220: if(head->next==NULL)
221: {
222: p=head;
223: free (p);
224: head=NULL;
225: }
226: else
227: {
228: for(p=head;p->next!=NULL;p=p->next);
229: p->prev->next=NULL;
230: free (p);
231: }
232: return head;
233: }
234: dnode *delete_m(dnode *head)
235: {
236: dnode *p,*q;
237: char y;
238: if(head==NULL)
239: {
240: printf("\nUnderflow.............Empty Linked List");
241: return head;
242: }
243: printf("\nEnter the data to be deleted: ");
244: y=getchar();
245: for(p=head;p!=NULL && p->data!=y;p=p->next);
246: if(p->data!=y)
247: {
248: printf("Underflow...Data not found..!!!!");
249: return head;
250: }
251: if(p==head)
252: {
253: head=head->next;
254: if(head!=NULL)
255: head->prev=NULL;
256: free (p);
257: }
258: else
259: {
260: p->prev->next=p->next;
261: p->next->prev=p->prev;
262: free (p);
263: }
264: return head;
265: }
266: void displayforward(dnode *head)
267: {
268: dnode *p;
269: printf("\n");
270: if(head!=NULL)
271: {
272: for(p=head;p!=NULL;p=p->next)
273: printf("%c ",p->data);
274: }
275: }
276: void displaybackward(dnode *head)
277: {
278: dnode *p;
279: printf("\n");
280: if(head!=NULL)
281: {
282: for(p=head;p->next!=NULL;p=p->next);
283: for(;p!=NULL;p=p->prev)
284: printf("%c",p->data);
285: }
286: }
287: -----------------OUTPUT---------------
288: Enter the string:
289: pictpune
290: p i c t p u n e
291: _________________________________________________________
292: 1. Insert
293: 2. Delete
294: 3. Display Forward
295: 4. Diplay Backward
296: Enter Your choice: 1
297: Insert data-
298: 1. Beginning
299: 2. End
300: 3. In Middle
301: Enter which operation you want to perform: 1
302: Enter the data to be insered: a
303: a p i c t p u n e
304: Do you want to continue???(0/1): 1
305: _________________________________________________________
306: 1. Insert
307: 2. Delete
308: 3. Display Forward
309: 4. Diplay Backward
310: Enter Your choice: 2
311: Delete data-
312: 1. Beginning
313: 2. End
314: 3. In Middle
315: Enter which operation you want to perform: 1
316: p i c t p u n e
317: Do you want to continue???(0/1): 1
318: _________________________________________________________
319: 1. Insert
320: 2. Delete
321: 3. Display Forward
322: 4. Diplay Backward
323: Enter Your choice: 3
324: p i c t p u n e
325: Do you want to continue???(0/1): 1
326: _________________________________________________________
327: 1. Insert
328: 2. Delete
329: 3. Display Forward
330: 4. Diplay Backward
331: Enter Your choice: 4
332: enuptcip
333: Do you want to continue???(0/1): 0
No comments:
Post a Comment