-
Notifications
You must be signed in to change notification settings - Fork 0
/
linklist5.c
240 lines (217 loc) · 3.65 KB
/
linklist5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include<stdio.h>
#include<stdlib.h>
struct node
{ //structure of node
int info;
struct node *link;
};
int count=0;
struct node *START = NULL; //start pointer to control the link list
struct node * createnode();
void insertnode(); //insert at last
void deletenode(); //delete at first
void viewlist();
int menu();
void insertatfirst(); //insert at first
void insertafterposition(); //insert after any position
int getlength();
void deleteatlast();
struct node * createnode() //create node dynamically
{
struct node *n;
n = malloc(sizeof(struct node));
return(n);
}
void insertnode()
{
struct node *temp,*t;
temp = createnode();
printf("Enter the data in node \n");
scanf("%d",&temp->info);
temp->link=NULL;
if(START==NULL)
{
START = temp;
}
else
{
t = START;
while(t->link!=NULL)
{
t = t->link;
}
t->link=temp;
}
}
void deletenode()
{
if(START==NULL)
printf("List is empty\n");
else
{
struct node *q;
q=START;
START=START->link;
free(q);
}
}
void viewlist()
{
struct node *t;
if(START==NULL)
printf("No List is there\n");
else
{
t=START;
while(t!=NULL)
{
printf("%d",t->info);
t=t->link;
}
}
}
void insertatfirst()
{
struct node *newnode;
newnode=createnode();
printf("Enter the data in node\n");
scanf("%d",&newnode->info);
if(START==NULL)
START=newnode;
else
{
newnode->link=START;
START=newnode;
}
}
void deleteatlast()
{
struct node *q;
struct node *t;
if(START==NULL)
printf("List is empty\n");
else
{
q=START;
t=START;
while(t->link!=NULL)
{
q=t;
t=t->link;
}
if(t==START)
{
START=NULL;
}
else{
q->link=NULL;
free(t);
}
}
}
int getlength()
{
struct node *t;
if(t==NULL)
printf("List is EMPTY\n");
else
{
t=START;
while(t!=NULL)
{
count+=1;
t=t->link;
}
return(count);
}
}
void insertafterposition()
{
int position;
struct node *newnode,*t;
if(START==NULL)
printf("List is empty\n");
else
{
printf("Enter the position of node after which you want to insert node\n");
scanf("%d",&position);
if(position>getlength())
{
printf("Enter correct position\n");
insertafterposition();
}
else
{
int i=1;
newnode=createnode();
printf("Enter data\n");
scanf("%d",&newnode->info);
newnode->link=NULL;
if(START==NULL)
START=newnode;
else
{
t=START;
while(i<position)
{
t=t->link;
i++;
}
newnode->link=t->link;
t->link=newnode;
}
}
}
}
int menu()
{
int ch;
printf("\t\t\t 1. Insert NODE at last in LINK LIST\n");
printf("\t\t\t 2. View LIST in LINK LIST\n");
printf("\t\t\t 3. Delete NODE at first in LINK LIST\n");
printf("\t\t\t 4. Insert NODE at first in LINK LIST\n");
printf("\t\t\t 5. To see the length\n");
printf("\t\t\t 6. Insert after a node in LINK LIST\n");
printf("\t\t\t 7. Delete last node in LINK LIST\n");
printf("\t\t\t 8. Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
return(ch);
}
int main()
{
int k;
while(1)
{
switch(menu())
{
case 1:
insertnode();
break;
case 2:
viewlist();
break;
case 3:
deletenode();
break;
case 4:
insertatfirst();
break;
case 5:
k=getlength();
printf("The length of link list is %d",k);
break;
case 6:
insertafterposition();
break;
case 7:
deleteatlast();
break;
case 8:
exit(0);
break;
default:
printf("\nThis choice doesn't exist.\n");
}
}
}