Single Linked List

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void display();  
4:  void creat();  
5:  void start();  
6:  void middle();  
7:  void end();  
8:  void delet();  
9:  void insert();  
10:  void dmiddle();  
11:  struct llist  
12:  {  
13:    int item;  
14:    struct llist *link;  
15:  };  
16:  struct llist *head,*n,*list,*dum;  
17:  int i,ch;  
18:  void main()  
19:  {  
20:    while(1)  
21:    {  
22:        clrscr();  
23:        printf("\t\t\t  LINKED LIST\n\n\n");  
24:        printf("Press 1 for Create\n   2 for INSERT\n   3 for DELETE\n   4 for DISPLAY\n   5 for EXIT\n\n\n   Your choice :");  
25:        scanf("%d",&ch);  
26:        switch(ch)  
27:        {  
28:          case 1:  creat();  
29:                 break;  
30:          case 2:     insert();  
31:                 break;  
32:          case 3:     delet();  
33:                 break;  
34:          case 4:     display();  
35:                 break;  
36:          case 5:     exit(0);  
37:          default:     printf("\n\nIncorrect Selection\n\nPress any key to continue...");  
38:          getch();  
39:        }  
40:    }  
41:  }  
42:  void insert()  
43:  {  
44:    i=1;  
45:    while(i)  
46:    {  
47:        clrscr();  
48:        printf("\t\t\t INSETION\n\n\n");  
49:        printf("Press 1 for Insert in the start\n   2 for Insert in the middle\n   3 for insert in the end\n   4 for display\n   5 for EXIT\n\n\n   Your choice :");  
50:        scanf("%d",&ch);  
51:        switch(ch)  
52:       {  
53:          case 1:     start();  
54:                 break;  
55:          case 2:     middle();  
56:                 break;  
57:          case 3:     end();  
58:                 break;  
59:          case 4:     display();  
60:                 break;  
61:          case 5:     i=0;  
62:                 break;  
63:          default:     printf("\n\nIncorrect Selection\n\nPress any key to continue...");  
64:          getch();  
65:       }  
66:    }  
67:  }  
68:  void delet()  
69:  {  
70:    i=1;  
71:    while(i)  
72:    {  
73:        clrscr();  
74:        printf("\t\t\t DELETION\n\n\n");  
75:        printf("Press 1 for delete at the start\n   2 for delete at the middle\n   3 for delete at the end\n   4 for display\n   5 for EXIT\n\n\n   Your choice :");  
76:        scanf("%d",&ch);  
77:        switch(ch)  
78:        {  
79:          case 1:     dum=head;  
80:                 head=head->link;  
81:                 printf("\nDeleted item : %d",dum->item);  
82:                 free(dum);  
83:                 break;  
84:          case 2:     dmiddle();  
85:                 break;  
86:          case 3:     dum=head;  
87:                 while(dum->link!=NULL)  
88:                 {  
89:                    list=dum;  
90:                    dum=dum->link;  
91:                 }  
92:                 printf("\nDeleted item : %d",dum->item);  
93:                 list->link=NULL;  
94:                 free(dum);  
95:                 break;  
96:          case 4:     display();  
97:                 break;  
98:          case 5:     i=0;  
99:                 break;  
100:          default:     printf("\n\nIncorrect Selection\n\nPress any key to continue...");  
101:                 getch();  
102:        }  
103:    }  
104:  }  
105:  void creat()  
106:  {  
107:    if(head==NULL)  
108:    {  
109:        n=(struct llist *)malloc(sizeof(struct llist));  
110:        printf("List was created succesfully \nEnter the data to insert :");  
111:        scanf("%d",&n->item);  
112:        n->link=NULL;  
113:        head=n;  
114:    }  
115:    else  
116:    {  
117:        printf("List was already created ");  
118:        getch();  
119:    }  
120:  }  
121:  void start()  
122:  {  
123:    n=(struct llist *)malloc(sizeof(struct llist));  
124:    printf("Enter the data to insert :");  
125:    scanf("%d",&n->item);  
126:    n->link=NULL;  
127:    n->link=head;  
128:    head=n;  
129:  }  
130:  void middle()  
131:  {  
132:    dum=head;  
133:    printf("Enter the no after which you wish to insert :");  
134:    scanf("%d",&i);  
135:    while((dum->item!=i)&&(dum!=NULL))  
136:        dum=dum->link;  
137:    if(dum!=NULL)  
138:    {  
139:        n=(struct llist*)malloc(sizeof(struct llist));  
140:        printf("Enter the data to insert :");  
141:        scanf("%d",&n->item);  
142:        n->link=dum->link;  
143:        dum->link=n;  
144:    }  
145:    else  
146:    {  
147:        printf("No such item.\nPress any key to continue....");  
148:        getch();  
149:    }  
150:  }  
151:  void dmiddle()  
152:  {  
153:    dum=head;  
154:    printf("Enter the no which you wish to delete :");  
155:    scanf("%d",&i);  
156:    while(dum->item!=i&&dum!=NULL)  
157:    {  
158:        list=dum;  
159:        dum=dum->link;  
160:    }  
161:    if(dum!=NULL)  
162:    {  
163:        printf("\nthe deleted data :%d",dum->item);  
164:        list->link=dum->link;  
165:        free(dum);  
166:    }  
167:    else  
168:    {  
169:        printf("No such item.\nPress any key to continue....");  
170:        getch();  
171:    }  
172:  }  
173:  void end()  
174:  {  
175:    n=(struct llist*)malloc(sizeof(struct llist));  
176:    printf("Enter the data to insert :");  
177:    scanf("%d",&n->item);  
178:    n->link=NULL;  
179:    dum=head;  
180:    while(dum->link!=NULL)  
181:        dum=dum->link;  
182:    dum->link=n;  
183:  }  
184:  void display()  
185:  {  
186:    dum=head;  
187:    clrscr();  
188:    printf("\t\t\t DISPLAY\n\n\n");  
189:    while(dum!=NULL)  
190:    {  
191:        printf("--->|%d|",dum->item);  
192:        dum=dum->link;  
193:    }  
194:    printf("--->|NULL|");  
195:    printf("\n\nPress any key to continue...");  
196:    getch();  
197:  }  

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