-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.c
198 lines (187 loc) · 3.55 KB
/
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
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
int data;
struct node *next; // pointing to next node
};
struct node *start = NULL;
struct node *list(){
struct node *new, *p;
int n;
printf("Enter -1 to stop.\nEnter the elements:\n");
scanf("%d", &n);
while(n != -1)
{
new = (struct node *)malloc(sizeof(struct node)); // malloc returns a pointer pointing to new space created by it
new->data = n;
if (start == NULL)
{
start = new;
p = start;
new->next = NULL;
}
else{
p->next = new;
p = new;
new->next = NULL;
}
scanf("%d",&n);
}
return start;
}
struct node *display(){
struct node *q;
q = start;
while(q != NULL)
{
printf("\t%d", q->data);
q = q->next;
}
}
struct node *insert_beg(int n){
printf("Element inserted at start of list");
struct node *new_node;
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = n;
new_node->next = start;
start = new_node;
return start;
}
struct node *insert_end(int n){
printf("\nElement inserted at end of list");
struct node *new_node, *p;
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = n;
new_node->next = NULL;
p = start;
while(p->next != NULL)
{
p = p->next;
}
p->next = new_node;
return start;
}
struct node *insert_after(int n){
int b;
printf("\nEnter element after which u want to insert : ");
scanf("%d",&b);
struct node *new_node, *prev, *p;
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = n;
p = start;
prev = p;
while(prev->data != b){
prev = p;
p = p->next;
if (p == NULL)
{
printf("\nElement not found!");
}
}
prev->next = new_node;
new_node->next = p;
return start;
}
struct node *insert_before(int n){
int b;
printf("\nElement before which u want to insert : ");
scanf("%d",&b);
struct node *new_node, *prev, *p;
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = n;
p = start;
prev = p;
while(p->data != b){
prev = p;
p = p->next;
if (p == NULL)
{
printf("Element not found!");
}
}
prev->next = new_node;
new_node->next = p;
return start;
}
struct node *del_first(){
struct node *p;
p = start;
start = start->next;
printf("\nDeleting first element...");
free(p);
return start;
}
struct node *del_last(){
struct node *p, *q;
p = start;
q = p;
while(p->next != NULL){
q = p;
p = p->next;
}
printf("\nDeleting last element...");
free(p);
q->next = NULL;
return start;
}
struct node *del_after(){
struct node *prev, *p;
p = start;
prev = p;
printf("\nElement after which u want to delete : ");
int n;
scanf("%d", &n);
while(prev->data != n){
prev = p;
p = p->next;
}
prev->next = p->next;
free(p);
return start;
}
struct node *sort(){
printf("\n\nSorted List-->");
struct node *p, *q;
int temp;
p = start;
while(p->next != NULL){
q = p->next;
while(q != NULL){
if (p->data > q->data){
temp = p->data;
p->data = q->data;
q->data = temp;
}
q = q->next;
}
p = p->next;
}
return start;
}
int main()
{
int n;
list();
printf("Displaying elements in list\n");
display();
printf("\nEnter element to insert: ");
scanf("%d", &n);
insert_beg(n);
display();
insert_end(n);
display();
insert_after(n);
display();
insert_before(n);
display();
del_first();
display();
del_last();
display();
del_after();
display();
sort();
display();
return 0;
}