-
Notifications
You must be signed in to change notification settings - Fork 0
/
linklistpractice1.c
187 lines (173 loc) · 2.53 KB
/
linklistpractice1.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
int count=0;
struct node *START=NULL;
struct node *createnode();
void insertnode();
void deletenode();
void view();
void insertatfirst();
void deleteatlast();
int menu();
int getlength();
struct node *createnode()
{
struct node *n;
n=malloc(sizeof(struct node));
return(n);
}
void insertnode()
{
struct node *temp,*t;
temp=createnode();
printf("Enter the data : ");
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 view()
{
struct node *t;
if(START==NULL)
printf("\nList is Empty\n");
else
{
t=START;
while(t!=NULL)
{
printf("%d",t->info);
t=t->link;
}
}
}
void deletenode()
{
if(START==NULL)
printf("\nList is Empty\n");
else
{
struct node *q;
q=START;
START=START->link;
free(q);
}
}
void insertatfirst()
{
struct node *newnode;
newnode=createnode();
printf("Enter the data : ");
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("\nList 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("/nList is Empty\n");
else
{
t=START;
while(t!=NULL)
{
count+=1;
t=t->link;
}
return(count);
}
}
int menu()
{
int m;
printf("\n1. Insert a node in LINK LIST\n");
printf("\n2. View node in LINK LIST\n");
printf("\n3. Delete node in LINK LIST\n");
printf("\n4. Insert node at first in LINK LIST\n");
printf("\n5. Delete node at last in LINK LIST\n");
printf("\n6. Number of nodes in LINK LIST\n");
printf("\n7. Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&m);
return(m);
}
int main()
{
int k;
while(1)
{
switch(menu())
{
case 1:
insertnode();
break;
case 2:
view();
break;
case 3:
deletenode();
break;
case 4:
insertatfirst();
break;
case 5:
deleteatlast();
break;
case 6:
k=getlength();
printf("Number of nodes : %d\n",k);
break;
case 7:
exit(0);
break;
default:
printf("\nThis choice does not exist\n");
}
}
}