Circular Linked List

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

No comments:

Post a Comment

Code Of The day - Suggest An Output For The Snippet

  #include<stdio.h>   
  int main()   
  {   
   float a=3.15529;   
   printf("%2.1f\n", a);   
   return 0;   
  }   
· A Code Archive - code1archive