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