-
Notifications
You must be signed in to change notification settings - Fork 0
/
singlyLinkedList.c
208 lines (208 loc) · 4.22 KB
/
singlyLinkedList.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
#include<stdio.h>
#include<stdlib.h>
int choice,value;
struct node{
int data;
struct node *next;
};
struct node *start,*temp;
int pos,i;
void InsertAtBegin()
{
printf("Enter value to insert : \n");
scanf("%d",&value);
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
if(start==NULL)
{
newnode->next=NULL;
newnode->data=value;
start=newnode;
}
else
{
newnode->next=start;
newnode->data=value;
start=newnode;
}
}
void Display()
{
if(start==NULL)
printf("The list is empty..");
else{
printf("The elements are : \n");
struct node *temp;
for(temp=start;temp!=NULL;temp=temp->next)
printf("%d\t",temp->data);
}
}
void DeleteAtBegin()
{
struct node *temp;
if(start==NULL)
printf("The list is empty..");
else if(start->next==NULL)
{
temp=start;
start=NULL;
printf("Deleted : %d",temp->data);
free(temp);
}
else{
temp=start;
start=start->next;
printf("Deleted : %d",temp->data);
free(temp);
}
}
void insertAtEnd()
{
printf("Enter value to insert : \n");
scanf("%d",&value);
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
if(start==NULL)
{
newnode->next=NULL;
newnode->data=value;
start=newnode;
}
else{
struct node *p;
p=start;
while(p->next!=NULL)
{
p=p->next;
}
p->next=newnode;
newnode->data=value;
newnode->next=NULL;
}
}
void deleteAtEnd()
{
struct node *p;
if(start==NULL)
printf("There is nothing to delete ...");
else if(start->next==NULL)
{
temp=start;
start=NULL;
printf("Deleted : %d",temp->data);
free(temp);
}
else{
p=start->next;
temp=start;
while(p->next!=NULL)
{
temp=temp->next;
p=p->next;
}
temp->next=NULL;
printf("Deleted : %d",p->data);
free(p);
}
}
void insertAtParticular()
{
struct node *newNode;
newNode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data to insert : ");
scanf("%d",&value);
printf("Enter position : ");
scanf("%d",&pos);
if(start==NULL)
{
newNode->next=NULL;
newNode->data=value;
start=newNode;
}
else
{
struct node *p;
p=start;
for(i=1;i<pos-1;i++)
{
p=p->next;
}
newNode->data=value;
newNode->next=p->next;
p->next=newNode;
}
}
void DeleteAtParticular()
{
struct node *p;
printf("Enter position : ");
scanf("%d",&pos);
if(start==NULL)
printf("The list is empty..");
else if(pos==1&&start->next==NULL)
{
temp=start;
start=NULL;
printf("Deleted :%d");
free(temp);
}
else if(pos==1)
{
temp=start;
start=start->next;
printf("Deleted :%d",temp->data);
free(temp);
}
else{
struct node *p;
p=start;
for(i=1;i<pos-1;i++)
{
p=p->next;
}
temp=p->next;
p->next=temp->next;
printf("Deleted :%d",temp->data);
free(temp);
}
}
void main()
{
printf("MENU\n1.insert at begning\n2.Delete At Begining\n3.insert at end\n4.delete at end\n5.insert At particular\n6.Delete at particular\n10.EXIT\n");
do
{
printf("\nEnter choice : \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
InsertAtBegin();
Display();
break;
case 2:
DeleteAtBegin();
Display();
break;
case 3:
insertAtEnd();
Display();
break;
case 4:
deleteAtEnd();
Display();
break;
case 5:
insertAtParticular();
Display();
break;
case 6:
DeleteAtParticular();
Display();
break;
case 10:
break;
default:
break;
}
} while (choice!=10);
}