1: #include <stdio.h>
2: #include <stdlib.h>
3: #include <malloc.h>
4: #include <dos.h>
5: typedef struct
6: {
7: int id;
8: char name[36];
9: int sal;
10: }EMP; //Just Alias Name
11: EMP getdata()
12: {
13: EMP te;
14: printf("\nENTER ID: ");
15: scanf("%d",&te.id);
16: printf("ENTER NAME: ");
17: fflush(stdin);
18: gets(te.name);
19: printf("ENTER SALARY: ");
20: scanf("%d",&te.sal);
21: return te;
22: }
23: EMP updatedata()
24: {
25: EMP te;
26: printf("\nENTER NEW ID: ");
27: scanf("%d",&te.id);
28: printf("ENTER NEW NAME: ");
29: fflush(stdin);
30: gets(te.name);
31: printf("ENTER NEW SALARY: ");
32: scanf("%d",&te.sal);
33: return te;
34: }
35: void showdata(EMP te)
36: {
37: printf("\nID:%d NAME:%5s SAL:%3d",
38: te.id,te.name,te.sal);
39: }
40: struct dlink
41: {
42: struct dlink*prev;
43: EMP data;
44: struct dlink*next;
45: };
46: typedef struct dlink LINK; //Just Alias Name
47: LINK*head=NULL; //global pointer
48: void addnode()
49: {
50: char ch;
51: LINK*th1,*th2;
52: if(head==NULL)
53: {
54: head=(LINK*)malloc(sizeof(LINK));
55: head->prev=NULL;
56: head->data=getdata();
57: head->next=NULL;
58: printf("\nDo You want to continue Y?:");
59: fflush(stdin);
60: ch=getchar();
61: if(ch!='Y'&&ch!='y')
62: return;
63: }
64: th1=head;
65: while(th1->next!=NULL)
66: th1=th1->next;
67: do
68: {
69: th2=(LINK*)malloc(sizeof(LINK));
70: th2->prev=th1;
71: th2->data=getdata();
72: th2->next=NULL;
73: th1->next=th2;
74: th1=th2;
75: printf("\nDo You want to continue Y?:");
76: fflush(stdin);
77: ch=getchar();
78: }while(ch=='Y'||ch=='y');
79: th1=th2=NULL;
80: return;
81: }
82: void displaynode()
83: {
84: LINK*th;
85: if(head==NULL)
86: {
87: printf("LIST IS EMPTY\n");
88: system("PAUSE");
89: return;
90: }
91: th=head;
92: do
93: {
94: showdata(th->data);
95: th=th->next;
96: }while(th!=NULL);
97: printf("\n");
98: system("PAUSE");
99: return;
100: }
101: int nodecount()
102: {
103: int count=0;
104: LINK*th;
105: if(head==NULL)
106: return count; //default value is 0
107: th=head;
108: do
109: {
110: ++count;
111: th=th->next;
112: }while(th!=NULL);
113: return count;
114: }
115: void insertnode()
116: {
117: LINK*th1,*th2;
118: int i,lp,nl;
119: if(head==NULL)
120: {
121: printf("LIST IS EMPTY\n");
122: system("PAUSE");
123: return;
124: }
125: printf("\nEnter Link position: ");
126: scanf("%d",&lp);
127: nl=nodecount();
128: if(lp<1||lp>nl)
129: {
130: printf("invalid link position\n");
131: system("PAUSE");
132: return;
133: }
134: if(lp==1)
135: {
136: th1=(LINK*)malloc(sizeof(LINK));
137: th1->prev=NULL;
138: th1->data=getdata();
139: th1->next=head;
140: head->prev=th1;
141: head=th1;
142: return;
143: }
144: th1=head;
145: for(i=1;i<lp-1; i++)
146: th1=th1->next;
147: th2=(LINK*)malloc(sizeof(LINK));
148: th2->prev=th1;
149: th2->data=getdata();
150: th2->next=th1->next;
151: th1->next=th2;
152: if(nl!=lp)
153: th2->next->prev=th2;
154: th1=th2=NULL;
155: return;
156: }
157: void deletenode()
158: {
159: LINK*th1,*th2;
160: int i,lp,nl;
161: if(head==NULL)
162: {
163: printf("LIST IS EMPTY\n");
164: system("PAUSE");
165: return;
166: }
167: printf("\nEnter Link position: ");
168: scanf("%d",&lp);
169: nl=nodecount();
170: if(lp<1||lp>nl)
171: {
172: printf("invalid link position\n");
173: system("PAUSE");
174: return;
175: }
176: if(nl==1)
177: {
178: free(head);
179: head=NULL;
180: printf("node id deleted\n");
181: system("PAUSE");
182: return;
183: }
184: if(lp==1)
185: {
186: th1=head;
187: head=head->next;
188: head->prev=NULL;
189: th1->next=NULL;
190: free(th1);
191: th1=NULL;
192: return;
193: }
194: th1=head;
195: for(i=1; i<lp-1; i++)
196: th1=th1->next;
197: th2=th1->next;
198: th1->next=th2->next;
199: if(nl!=lp) //if not tail node then only required
200: th2->next->prev=th1; //th2->next->prev=th2->prev;
201: th2->prev=NULL;
202: th2->next=NULL;
203: free(th2);
204: th1=th2=NULL;
205: return;
206: }
207: void updatenode()
208: {
209: LINK*th1;
210: int nl,lp,i;
211: if(head==NULL)
212: {
213: printf("LIST IS EMPTY\n");
214: system("PAUSE");
215: return;
216: }
217: printf("Enter link position: ");
218: scanf("%d",&lp);
219: nl=nodecount();
220: if(lp<1||lp>nl)
221: {
222: printf("invalid node position:");
223: system("PAUSE");
224: return;
225: }
226: if(lp==1)
227: {
228: head->data=updatedata();
229: printf("Node is updated\n");
230: system("PAUSE");
231: return;
232: }
233: th1=head;
234: for(i=1; i<lp; i++)
235: th1=th1->next;
236: th1->data=updatedata();
237: printf("node is updated\n");
238: system("PAUSE");
239: return;
240: }
241: void reversenode()
242: {
243: LINK*temp=NULL;
244: LINK*current=head;
245: int nl;
246: if(head==NULL)
247: {
248: printf("LIST IS EMPTY\n");
249: system("PAUSE");
250: return;
251: }
252: nl=nodecount();
253: if(nl==1)
254: {
255: printf("We can't do reverse\n");
256: system("PAUSE");
257: return;
258: }
259: while(current!=NULL)
260: {
261: temp=current->prev;
262: current->prev=current->next;
263: current->next=temp;
264: current=current->prev;
265: }
266: head=temp->prev;
267: return;
268: }
269: int main()
270: {
271: int option;
272: while(1)
273: {
274: system("CLS");
275: printf("\n1 FOR ADD NODE:");
276: printf("\n2 FOR DISPLAY NODE:");
277: printf("\n3 FOR NODE COUNT:");
278: printf("\n4 FOR INSERT NODE:");
279: printf("\n5 FOR DELETE NODE:");
280: printf("\n6 FOR UPDATE NODE:");
281: printf("\n7 FOR REVERSE NODE:");
282: printf("\n8 FOR EXIT: ");
283: scanf("%d",&option);
284: switch(option)
285: {
286: case 1: addnode();
287: break;
288: case 2: displaynode();
289: break;
290: case 3:
291: printf("NODE COUNT:%d\n",nodecount());
292: system("PAUSE");
293: break;
294: case 4: insertnode();
295: break;
296: case 5: deletenode();
297: break;
298: case 6: updatenode();
299: break;
300: case 7:reversenode();
301: break;
302: case 8: free(head);
303: return EXIT_SUCCESS;
304: default: printf("invalid option");
305: system("PAUSE");
306: break;
307: }
308: }
309: }
doubly linked list in c
Labels:
Data Structure
Location:
Hyderabad, Telangana, India
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment