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",te.id,te.name,te.sal);
38: }
39: struct dclink
40: {
41: struct dclink*prev;
42: EMP data;
43: struct dclink*next;
44: };
45: typedef struct dclink LINK; //Just Alias Name
46: LINK*head=NULL; //global pointer
47: void addnode()
48: {
49: char ch;
50: LINK*th1,*th2;
51: if(head==NULL)
52: {
53: head=(LINK*)malloc(sizeof(LINK));
54: head->prev=head;
55: head->data=getdata();
56: head->next=head;
57: printf("\nDo You want to continue Y?:");
58: fflush(stdin);
59: ch=getchar();
60: if(ch!='Y'&&ch!='y')
61: return;
62: }
63: th1=head;
64: while(th1->next!=head)
65: th1=th1->next;
66: do
67: {
68: th2=(LINK*)malloc(sizeof(LINK));
69: th2->prev=th1;
70: th2->data=getdata();
71: th2->next=head;
72: th1->next=th2;
73: head->prev=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 displaynodeFtoL()
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!=head);
97: printf("\n");
98: system("PAUSE");
99: return;
100: }
101: void displaynodeLtoF()
102: {
103: LINK*th;
104: if(head==NULL)
105: {
106: printf("LIST IS EMPTY\n");
107: system("PAUSE");
108: return;
109: }
110: th=head->prev;
111: do
112: {
113: showdata(th->data);
114: th=th->prev;
115: }while(th!=head->prev);
116: printf("\n");
117: system("PAUSE");
118: return;
119: }
120: int nodecount()
121: {
122: int count=0;
123: LINK*th;
124: if(head==NULL)
125: return count; //default value is 0
126: th=head;
127: do
128: {
129: ++count;
130: th=th->next;
131: }while(th!=head);
132: return count;
133: }
134: void insertnode()
135: {
136: LINK*th1,*th2;
137: int i,lp,nl;
138: if(head==NULL)
139: {
140: printf("LIST IS EMPTY\n");
141: system("PAUSE");
142: return;
143: }
144: printf("\nEnter Link position: ");
145: scanf("%d",&lp);
146: nl=nodecount();
147: if(lp<1||lp>nl)
148: {
149: printf("invalid link position\n");
150: system("PAUSE");
151: return;
152: }
153: if(lp==1)
154: {
155: th1=(LINK*)malloc(sizeof(LINK));
156: th1->prev=head->prev;
157: th1->data=getdata();
158: th1->next=head;
159: head->prev->next=th1;
160: head->prev=th1;
161: head=th1;
162: return;
163: }
164: th1=head;
165: for(i=1;i<lp-1; i++)
166: th1=th1->next;
167: th2=(LINK*)malloc(sizeof(LINK));
168: th2->prev=th1;
169: th2->data=getdata();
170: th2->next=th1->next;
171: th1->next=th2;
172: th2->next->prev=th2;
173: th1=th2=NULL;
174: return;
175: }
176: void deletenode()
177: {
178: LINK*th1,*th2;
179: int i,lp,nl;
180: if(head==NULL)
181: {
182: printf("LIST IS EMPTY\n");
183: system("PAUSE");
184: return;
185: }
186: printf("\nEnter Link position: ");
187: scanf("%d",&lp);
188: nl=nodecount();
189: if(lp<1||lp>nl)
190: {
191: printf("invalid link position\n");
192: system("PAUSE");
193: return;
194: }
195: if(nl==1)
196: {
197: free(head);
198: head=NULL;
199: printf("node id deleted\n");
200: system("PAUSE");
201: return;
202: }
203: if(lp==1)
204: {
205: th1=head;
206: head=head->next;
207: head->prev=th1->prev;
208: head->prev->next=head;
209: th1->next=th1->prev=NULL;
210: free(th1);
211: th1=NULL;
212: return;
213: }
214: th1=head;
215: for(i=1; i<lp-1; i++)
216: th1=th1->next;
217: th2=th1->next;
218: th1->next=th2->next;
219: th2->next->prev=th1;
220: th2->prev=th2->next=NULL;
221: free(th2);
222: th1=th2=NULL;
223: return;
224: }
225: void updatenode()
226: {
227: LINK*th1;
228: int nl,lp,i;
229: if(head==NULL)
230: {
231: printf("LIST IS EMPTY\n");
232: system("PAUSE");
233: return;
234: }
235: printf("Enter link position: ");
236: scanf("%d",&lp);
237: nl=nodecount();
238: if(lp<1||lp>nl)
239: {
240: printf("invalid node position:");
241: system("PAUSE");
242: return;
243: }
244: if(lp==1)
245: {
246: head->data=updatedata();
247: printf("Node is updated\n");
248: system("PAUSE");
249: return;
250: }
251: th1=head;
252: for(i=1; i<lp; i++)
253: th1=th1->next;
254: th1->data=updatedata();
255: printf("node is updated\n");
256: system("PAUSE");
257: return;
258: }
259: void reversenode()
260: {
261: LINK*temp=NULL;
262: LINK*current=head;
263: int nl;
264: if(head==NULL)
265: {
266: printf("LIST IS EMPTY\n");
267: system("PAUSE");
268: return;
269: }
270: nl=nodecount();
271: if(nl==1)
272: {
273: printf("We can't do reverse\n");
274: system("PAUSE");
275: return;
276: }
277: while(current!=head)
278: {
279: temp=current->prev;
280: current->prev=current->next;
281: current->next=temp;
282: current=current->prev;
283: }
284: head=temp->prev;
285: return;
286: }
287: int main()
288: {
289: int option;
290: while(1)
291: {
292: system("CLS");
293: printf("\n1 FOR ADD NODE:");
294: printf("\n2 FOR DISPLAY F-->L NODE:");
295: printf("\n3 FOR DISPLAY L--->F NODE:");
296: printf("\n4 FOR NODE COUNT:");
297: printf("\n5 FOR INSERT NODE:");
298: printf("\n6 FOR DELETE NODE:");
299: printf("\n7 FOR UPDATE NODE:");
300: printf("\n8 FOR REVERSE NODE:");
301: printf("\n9 FOR EXIT: ");
302: scanf("%d",&option);
303: switch(option)
304: {
305: case 1: addnode();
306: break;
307: case 2: displaynodeFtoL();
308: break;
309: case 3: displaynodeLtoF();
310: break;
311: case 4:
312: printf("NODE COUNT:%d\n",nodecount());
313: system("PAUSE");
314: break;
315: case 5: insertnode();
316: break;
317: case 6: deletenode();
318: break;
319: case 7: updatenode();
320: break;
321: case 8:reversenode();
322: break;
323: case 9: free(head);
324: return EXIT_SUCCESS;
325: default: printf("invalid option");
326: system("PAUSE");
327: break;
328: }
329: }
330: }
doubly circular linked list in c
Labels:
Data Structure
Location:
Hyderabad, Telangana, India
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment