-
Notifications
You must be signed in to change notification settings - Fork 2
/
LinkedLists1.c
207 lines (194 loc) · 6.02 KB
/
LinkedLists1.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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
int data;
struct node *next;
};
struct node *start;
/* Function to create a linked list!!*/
struct node *create_ll(struct node *start){
struct node *ptr,*new_node;
int num;
printf("\nEnter the value you wwanna insert and enter -1 to exit\t");
scanf("%d",&num);
while(num != -1){
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = num;
if(start == NULL){
start = new_node;
new_node->next = NULL;
}
else{
ptr = start;
while(ptr->next !=NULL){
ptr = ptr->next;
}
ptr->next = new_node;
new_node->next = NULL;
}
printf("\nEnter the number you wanna insert in the linked list\t");
scanf("%d",&num);
}
return start;
}
struct node *display(struct node *start){
struct node *ptr;
ptr = start;
printf("\nThe elements of the linked list are:\t");
while(ptr != NULL){
printf("%d\t",ptr->data);
ptr = ptr->next;
}
return start;
}
/* Function to insert a node at the begnning*/
struct node *insert_begin(struct node *start){
struct node *ptr,*new_node;
int num;
printf("\nEnter the number you wanna insert in the beginning of the list\t");
scanf("%d",&num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = num;
new_node->next = start;
start = new_node;
return start;
}
/*Function to insert a node at the nd*/
struct node *insert_end(struct node *start){
struct node *ptr,*new_node;
int num;
printf("\nEnter the number you wanna insert in the end of the list\t");
scanf("%d",&num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = num;
new_node->next = NULL;
ptr = start;
while(ptr->next != NULL){
ptr = ptr->next;
}
ptr->next =new_node;
/*
Here it is done as ptr->next = new_node, because we are changimg the pointing location(next) of the ptr node;
*/
return start;
}
struct node *insert_before(struct node *start){
struct node *ptr,*new_node,*preptr;
new_node = (struct node *)malloc(sizeof(struct node));
int val;
int num;
printf("Enter the value of the element before which you wanna add the new element");
scanf("%d",&val);
printf("Enter the value of the new element to be enetered");
scanf("%d",&num);
ptr = start;
new_node->data = num;
while(ptr->data != val){
preptr = ptr;
ptr = ptr->next;
}
preptr->next = new_node;
new_node->next = ptr;
return start;
}
struct node *insert_after(struct node *start){
struct node *ptr,*new_node,*postptr;
new_node = (struct node *)malloc(sizeof(struct node));
int val;
int num;
printf("Enter the value of the element after which you wanna add the new element");
scanf("%d",&val);
printf("Enter the value of the new element to be enetered");
scanf("%d",&num);
ptr = start;
new_node->data = num;
while(ptr->data != val){
ptr = ptr->next;
postptr = ptr->next;
}
ptr->next = new_node;
new_node->next = postptr;
return start;
}
struct node *delete_begin(struct node *start){
struct node *ptr;
ptr = start;
start = ptr->next;
free(ptr);
return start;
}
struct node *delete_end(struct node *start){
struct node *ptr,*preptr;
ptr = start;
while(ptr->next != NULL){
preptr = ptr;
ptr = ptr->next;
}
preptr->next = NULL;
free(ptr);
return start;
}
struct node *delete(struct node *start){
struct node *ptr,*preptr,*postptr;
ptr = start;
int num;
printf("Enter the element you wanna delete");
scanf("%d",&num);
//Added this statement after getting stuck in the lab test, doesn't worked initially if there are only two statements as the first onw will have no initial pointer.
if(ptr->data == num){
start = delete_begin(start);
return start;
}
else{
while(ptr->data != num){
preptr = ptr;
ptr = ptr->next;
postptr = ptr->next;
}
}
preptr->next = postptr; //Learn as to why i wrote like that. This is because, we are like from postptr to preptr, consider it like an arrow.
free(ptr);
return start;
}
void main(){
int option;
do{
printf("\n-------------------------------------------------------------------------------------------");
printf("\n***************Menu*****************");
printf("\n1.Create a linked list");
printf("\n2.Display the linked list");
printf("\n3.Insert a number in the beginning");
printf("\n4.Insert a number in the end");
printf("\n5.Insert an elemnet before a given element");
printf("\n6.Insert an element afer a given element");
printf("\n7.Delete an element from the beginning");
printf("\n8.Delete an element from the end");
printf("\n9.Delete the entered element");
printf("\n10. Press 12 to exit");
printf("\nEnter your choice\t");
scanf("%d",&option);
switch(option){
case 1: start = create_ll(start);
break;
case 2: start = display(start);
break;
case 3: start = insert_begin(start);
break;
case 4: start = insert_end(start);
break;
case 5: start = insert_before(start);
break;
case 6: start = insert_after(start);
break;
case 7: start= delete_begin(start);
break;
case 8: start = delete_end(start);
break;
case 9: start = delete(start);
break;
default: printf("\n Please neter a valid input");
}
}while(option!=10);
}