-
Notifications
You must be signed in to change notification settings - Fork 0
/
singly linked list.c
212 lines (209 loc) · 5.07 KB
/
singly linked list.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node* head=NULL,*start=NULL,*previous=NULL,*temp=NULL;
int addpos()
{
int data;
printf("\nEnter position to be node added :");
scanf("%d",&data);//scanf always end with a newline
//printf("\n %d",data);
return data;
}
int getpos()
{
int data;
printf("\nEnter position to be deleted in node :");
scanf("%d",&data);//scanf always end with a newline
//printf("\n %d",data);
return data;
}
int getdel()
{
int data;
printf("\nEnter data to be deleted in node :");
scanf("%d",&data);//scanf always end with a newline
//printf("\n %d",data);
return data;
}
int getdata()
{
int data;
printf("\nEnter data to be added in node :");
scanf("%d",&data);//scanf always end with a newline
//printf("\n %d",data);
return data;
}
int getoption()
{
int option;
printf("\nAppend an element to list : 1");
printf("\nAdd a node at specific position : 2");
printf("\nDelete an element from list by data : 3");
printf("\nDelete element by position : 4");
printf("\nDisplay list : 5");
printf("\nFor exiting the program : 6");
printf("\n\nChoose option : ");
scanf("%d",&option);
return option;
}
void display_list(struct node *start)
{
printf("\n");
while(start!=NULL)
{
printf("%d",start->data);
if(start->next!=NULL)
printf("->");
start=start->next;
}
}
void addnode(struct node *new_node)
{
start=head;
if(head==NULL)
{
head=new_node;
head->next=NULL;
return;
}
while (start->next!=NULL)
{
start=start->next;
}
if(start->next == NULL)
{
start->next=new_node;
new_node->next=NULL;
return;
}
}
void delnode(struct node *delete,int key)
{
if(delete != NULL && delete->data == key)//check whether key is present in head
{
head = delete->next; // Changed head
free(delete); // free old head
return;
}
while(delete != NULL && delete->data != key)//traversing to find the key
{
previous = delete;//store previous node as previous
delete = delete->next;//store next node to delete
}
if(delete==NULL)
{
printf("\nData is not found in the list\n");
return;
}
previous->next = delete->next;//unlink thenode to be deleted
free(delete);//freeing memory not used
}
void position(struct node *posdel,int key)
{
int i=0;
if(posdel!=NULL && key==0)//if position is head node
{
head=head->next;
free(posdel);
return;
}
while(i<(key-1) && posdel!=NULL )
{
posdel=posdel->next;
i++;
}
if(posdel==NULL)
return ;
temp=posdel->next->next;
posdel->next=temp;
}
void posadd(struct node* posnode,int key)
{
start=head;
int i=0;
if(start!=NULL && key==0)//if position is head node
{
posnode->next=head;
head=posnode;
return;
}
while(i<(key-1) && start!=NULL)
{
start=start->next;
i++;
}
if(start==NULL)
{
return ;
printf("\nData is not present in list");
}
temp=start->next;
start->next=posnode;
posnode->next=temp;
}
int main()
{
int data;
int pos;
while(1)
{
switch (getoption())
{
case 1:
data =getdata();
struct node *new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=data;
new_node->next=NULL;
addnode(new_node);
break;
case 2:
data =getdata();
pos =addpos();
struct node *posnode=(struct node*)malloc(sizeof(struct node));
posnode->data=data;
posnode->next=NULL;
posadd(posnode,pos);
break;
case 3:
if(head==NULL)
{
printf("\nlist is empty,try creating a new list\n");
break;
}
data=getdel();
struct node* delete=(struct node*)malloc(sizeof(struct node));
start=head;
delete=start;
delnode(delete,data);
break;
case 4:
if(head==NULL)
{
printf("\nlist is empty,try creating a new list\n");
break;
}
data=getpos();
struct node* posdel=(struct node*)malloc(sizeof(struct node));
posdel=head;
position(posdel,data);
case 5:
if(head==NULL)
{
printf("\nlist is empty,try creating a new list\n");
break;
}
printf("\n");
display_list(head);
break;
case 6:
exit(0);
default :
break;
}
}
}