-
Notifications
You must be signed in to change notification settings - Fork 0
/
llspecific.c
195 lines (175 loc) · 2.62 KB
/
llspecific.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
#include<stdio.h>
#include<stdlib.h>
struct link
{
int data;
struct link *next;
};
typedef struct link *node;
node head= NULL;
void insert(int el)
{
//at head
node ptr = (node)malloc(sizeof(struct link));
ptr->data = el;
if(head == NULL)
{
head = ptr;
ptr ->next = NULL;
}
else
{
ptr->next = head;
head = ptr;
}
}
int isEmpty()
{
if(head==NULL)
return 1;
else
return 0;
}
void insertafter(int el, int key)
{
int flag =0 ;
node temp = (node)malloc(sizeof(struct link));
node ptr = (node)malloc(sizeof(struct link));
ptr->data = el;
temp = head;
do
{
if(temp->data == key)
{
flag =1;
ptr->next = temp->next;
temp->next = ptr;
}
temp = temp->next;
}while(temp!=NULL);
if(flag==0)
{
printf("\nKey not found!!\n");
return;
}
}
void delete()
{
if(isEmpty())
{
printf("\nList is empty!\n");
return;
}
node ptr = (node)malloc(sizeof(struct link));
ptr = head;
while((ptr->next)->next !=NULL){
ptr = ptr->next;
}
ptr->next = NULL;
}
void deleteafter(int key)
{
if(isEmpty())
{
printf("\nList is empty!\n");
return;
}
int flag =0 ;
node temp = (node)malloc(sizeof(struct link));
temp = head;
while(temp!=NULL)
{
if(temp->data == key)
{
flag =1;
temp->next = (temp->next)->next;
}
temp = temp->next;
}
if(flag==0)
{
printf("\nKey not found!!\n");
return;
}
}
void disp()
{
if(isEmpty())
{
printf("\nList is empty!\n");
return;
}
node ptr = (node)malloc(sizeof(struct link));
ptr = head;
printf("\nLinked list - \n");
while(ptr!=NULL)
{
printf("%d -> ", ptr->data);
ptr = ptr->next;
}
free(ptr);
printf("\n");
}
int main()
{
int op;
do
{
printf("\nMenu \n1.Insert at head \n2.Insert after \n3.Delete after \n4.Display \n5.Exit");
printf("\nEnter option - ");
scanf("%d", &op);
switch(op)
{
case 1:
{
char ch = 'y';
int el;
while(ch == 'y')
{
printf("\nEnter element - ");
scanf("%d", &el);
insert(el);
printf("\nInsert more? ");
scanf(" %c", &ch);
}
}break;
case 2:
{
char ch = 'y';
int el; int key;
while(ch == 'y')
{
printf("\nEnter key - ");
scanf("%d", &key);
printf("\nEnter element - ");
scanf("%d", &el);
insertafter(el, key);
printf("\nInsert more? ");
scanf(" %c", &ch);
}
}break;
case 3:
{
char ch = 'y';
int key;
while(ch=='y')
{
printf("\nEnter key - ");
scanf("%d", &key);
deleteafter(key);
printf("\nDelete more? ");
scanf(" %c", &ch);
}
}break;
case 4:
{
disp();
}break;
case 5:
{
return 0;
}
}
}while(op!=5);
return 0;
}