1: /*Doubly linked list*/
2: #include<stdio.h>
3: #include<conio.h>
4: #include<alloc.h>
5: struct node
6: {
7: int data;
8: struct node*left;
9: struct node*right;
10: }*n;
11: struct node*head,*ne,*last,*p,*t;
12: char opt;
13: int no,ch;
14: void create(struct node*n);
15: void insert(struct node*n);
16: void infir(struct node*n);
17: void inlast(struct node*n);
18: void inmid(struct node*n);
19: void delfir(struct node*n);
20: void del(struct node*n);
21: void delast(struct node*n);
22: void delmid(struct node*n);
23: void display(struct node*n);
24: void main()
25: {
26: clrscr();
27: printf("\n\t\t\tDOUBLY LINKED LIST\t\t\t\n");
28: while(1)
29: {
30: printf("\n1.CREATION\n2.INSERTION\n3.DELETION\n4.DISPLAY\n5.EXIT\n");
31: printf("\nEnter ur choice");
32: scanf("%d",&ch);
33: switch(ch)
34: {
35: case 1:
36: create(n);
37: break;
38: case 2:
39: insert(n);
40: break;
41: case 3:
42: del(n);
43: break;
44: case 4:
45: display(n);
46: break;
47: case 5:
48: exit(1);
49: break;
50: default:
51: printf("\nEnter a valid case\n");
52: break;
53: }
54: }
55: }
56: void create(struct node*n)
57: {
58: ne=(struct node*)malloc(sizeof(struct node));
59: printf("\nEnter the data:");
60: scanf("%d",&(ne->data));
61: head=ne;
62: ne->left=NULL;
63: ne->right=NULL;
64: printf("\nDo u wish to create another?:(y/n):");
65: scanf("%c",&opt);
66: //fflush(stdin);
67: opt=getchar();
68: while(opt=='y')
69: {
70: if(head->right==NULL)
71: {
72: ne=(struct node*)malloc(sizeof(struct node));
73: printf("\nEnter the data:");
74: scanf("%d",&(ne->data));
75: ne->left=head;
76: ne->right=NULL;
77: head->right=ne;
78: }
79: else
80: {
81: p=head;
82: while(p->right!=NULL)
83: {
84: p=p->right;
85: }
86: ne=(struct node*)malloc(sizeof(struct node));
87: printf("\nEnter the data:");
88: scanf("%d",&(ne->data));
89: ne->right=NULL;
90: ne->left=p;
91: p->right=ne;
92: }
93: printf("\nDo u wish to create another(y/n)?:");
94: scanf("%c",&opt);
95: opt=getchar();
96: }
97: }
98: void insert(struct node*n)
99: {
100: printf("\nDo u want to insert?(y/n):");
101: scanf("%c",&opt);
102: opt=getchar();
103: while(opt=='y')
104: {
105: printf("\n\t\tINSERTION\t\t\n");
106: printf("\n1.FRONT\n2.MIDDLE\n3.END\n");
107: printf("\nEnter ur choice:");
108: scanf("%d",&ch);
109: switch(ch)
110: {
111: case 1:
112: infir(n);
113: break;
114: case 2:
115: inmid(n);
116: break;
117: case 3:
118: inlast(n);
119: break;
120: }
121: }
122: }
123: void infir(struct node*n)
124: {
125: printf("\nDo u wish to insert?(y/n)?:");
126: scanf("%c",&opt);
127: opt=getchar();
128: while(opt=='y')
129: {
130: p=head;
131: ne=(struct node*)malloc(sizeof(struct node));
132: printf("\nEnter the data:");
133: scanf("%d",&(ne->data));
134: ne->right=p;
135: ne->left=NULL;
136: printf("\nThe data %d of the node allocated has been inserted at the front\n",ne->data);
137: printf("\nDo u wish to insert(y/n)?:");
138: scanf("%c",&opt);
139: opt=getchar();
140: }
141: }
142: void inlast(struct node*n)
143: {
144: printf("\nDo u wish to insert(y/n)?:");
145: scanf("%c",&opt);
146: opt=getchar();
147: while(opt=='y')
148: {
149: ne=(struct node*)malloc(sizeof(struct node));
150: printf("\nEnter the data:");
151: scanf("%d",&(ne->data));
152: p=head;
153: while(p->right!=NULL)
154: {
155: p=p->right;
156: }
157: p->right=ne;
158: ne->left=p;
159: ne->right=NULL;
160: printf("\nThe data %d of the node allocated has been inserted at the end\n",ne->data);
161: printf("\nDo u wish to insert(y/n)?:");
162: scanf("%c",&opt);
163: opt=getchar();
164: }
165: }
166: void inmid(struct node*n)
167: {
168: printf("\nDo u wish to insert(y/n)?:");
169: scanf("%c",&opt);
170: opt=getchar();
171: while(opt=='y')
172: {
173: p=head;
174: printf("\nEnter the data of the node after which u want to insert:");
175: scanf("%d",&no);
176: while(p->data!=no)
177: {
178: p=p->right;
179: }
180: ne=(struct node*)malloc(sizeof(struct node));
181: printf("\nEnter the data:");
182: scanf("%d",&(ne->data));
183: ne->right=p->right;
184: ne->left=p;
185: p->right->left=ne;
186: p->right=ne;
187: printf("\nThe data %d of the allocated node has been inserted at the middle\n");
188: printf("\nDo u wish to insert(y/n)?:");
189: scanf("%c",&opt);
190: opt=getchar();
191: }
192: }
193: void delfir(struct node*n)
194: {
195: t=head;
196: p=head;
197: head=head->right;
198: head->left=NULL;
199: free(t);
200: printf("\nThe deleted node at the front holds the data %d\n",t->data);
201: free(t);
202: }
203: void delast(struct node*n)
204: {
205: p=head;
206: while(p->right->right!=NULL)
207: {
208: p=p->right;
209: }
210: t=p;
211: p->right=NULL;
212: printf("\nThe deleted node at the end holds the data %d\n",t->data);
213: free(t);
214: return;
215: }
216: void delmid(struct node*n)
217: {
218: printf("\nEnter the data of the node after which you want to delete?:");
219: scanf("%d",&(no));
220: p=head;
221: while(p->right->data!=no)
222: {
223: p=p->right;
224: }
225: t=p;
226: p->left->right=p->right;
227: p->right->left=p->left;
228: printf("\nThe deleted node is at mid position holding the data %d\n",t->data);
229: free(t);
230: return;
231: }
232: void del(struct node*n)
233: {
234: printf("\n\t\tDELETION\t\t\n");
235: if(head==NULL)
236: {
237: printf("\nNO Nodes\n");
238: return;
239: }
240: else
241: {
242: printf("\n1.FRONT\n2.MIDDLE\n3.END\n");
243: printf("\nEnter the choice:");
244: scanf("%d",&ch);
245: switch(ch)
246: {
247: case 1:
248: delfir(n);
249: break;
250: case 2:
251: delmid(n);
252: break;
253: case 3:
254: delast(n);
255: break;
256: }
257: }
258: }
259: void display(struct node *n)
260: {
261: if(head==NULL)
262: {
263: printf("\nNO Nodes\n");
264: return;
265: }
266: printf("\nThe items in the list are\n");
267: t=head;
268: do
269: {
270: printf("|%d|--->",t->data);
271: t=t->right;
272: }
273: while(t->right!=NULL);
274: printf("|%d|--->",t->data);
275: printf("|NULL|");
276: }
Double Linked List
Subscribe to:
Posts (Atom)
No comments:
Post a Comment