singly linked list implementation in c

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

0 comments:

Post a Comment