-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingle Linked List.cpp
208 lines (205 loc) Β· 4.66 KB
/
Single Linked List.cpp
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *start;
/*function definition*/
void insert_begin() /*fuction declaration*/
{
struct node *p;
int value;
p=(struct node *) malloc(sizeof(struct node *));
if(p==NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&value);
p->data=value;
p->next=start;
start=p;
}
}
void insert_last() /*fuction declaration*/
{
struct node *p,*temp;
int value;
p=(struct node*)malloc(sizeof(struct node));
if(p==NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&value);
p->data=value;
if(start==NULL)
{
p->next=NULL;
start=p;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
p->next=NULL;
}
}
}
void insert_locc() /*fuction declaration*/
{
int i,loc,value;
struct node *p, *temp;
p=(struct node *)malloc(sizeof(struct node));
if(p==NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&value);
p->data=value;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=start;
for(i=0;i<loc;i++)
{
temp=temp->next;
if(temp==NULL)
{
printf("\ncan't insert\n");
return;
}
}
p->next=temp->next;
temp->next=p;
}
}
void delete_begin() /*fuction declaration*/
{
struct node *p;
if(start==NULL)
{
printf("\nList is empty\n");
}
else
{
p=start;
start=p->next;
free(p);
}
}
void delete_last() /*fuction declaration*/
{
struct node *p,*p1;
if(start==NULL)
{
printf("\nlist is empty");
}
else if(start->next==NULL)
{
start=NULL;
free(start);
printf("\nOnly node of the list deleted ...\n");
}
else
{
p=start;
while(p->next!=NULL)
{
p1=p;
p=p->next;
}
p1->next=NULL;
free(p);
}
}
void delete_locc() /*fuction declaration*/
{
struct node *p,*p1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
p=start;
for(i=0;i<loc;i++)
{
p1=p;
p=p->next;
if(p==NULL)
{
printf("\nCan't delete");
return;
}
}
p1->next=p->next;
free(p);
printf("\nDeleted node %d ",loc+1);
}
void print() /*fuction declaration*/
{
struct node *p;
p=start;
if(p==NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values\n");
while (p!=NULL)
{
printf("\n%d",p->data);
p=p->next;
}
}
}
int main()
{
int ch=0;
while(ch!=8)
{
printf("\nEnter the operation to be performed\n");
printf("\n1.Insert in the begining\n2.Insert at last\n3.Insert at any specified position\n4.Delete from Beginning\n5.Delete from last\n6.Delete node after specified location\n7.Show\n8.Exit\n");
scanf("\n%d",&ch);
switch(ch)
{ /*function calls of all the operations */
case 1:
insert_begin();
break;
case 2:
insert_last();
break;
case 3:
insert_locc();
break;
case 4:
delete_begin();
break;
case 5:
delete_last();
break;
case 6:
delete_locc();
break;
case 7:
print();
break;
case 8:
exit(0);
break;
default:
printf("Enter valid option");
}
}
}