-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.c
206 lines (191 loc) · 4.52 KB
/
linkedlist.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
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Node{
int info;
struct Node *next;
};
struct Node *head = NULL;
// Create a Node of linkedlist
struct Node * CreateNode()
{
struct Node *newNode;
int value;
printf("Enter the value to be inserted : ");
scanf("%d", &value);
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->info = value;
newNode->next = NULL;
return(newNode);
}
struct Node * CreateLL(struct Node * head)
{
int value, ch = 0;
struct Node *ptr, *qtr;
printf("Lets Create Linked List\n ");
do{
ptr = CreateNode();
if(head == NULL)
head = ptr;
else{
qtr = head;
while(qtr->next != NULL)
qtr = qtr->next;
qtr->next = ptr;
}
printf("Do you want to add more values (0/1)");
scanf("%d", &ch);
}while(ch != 0);
return(head);
}
struct Node * displayLL(struct Node *head)
{
struct Node *ptr;
ptr = head;
if(head == NULL)
printf("LIST is EMPTY");
else {
while(ptr != NULL)
{
printf(" %d ", ptr->info);
ptr = ptr->next;
}
}
return(head);
}
// Insert a node at front
struct Node * insertAtBeg(struct Node * head)
{
struct Node *newNode;
newNode = CreateNode();
newNode->next = head;
head = newNode;
return(head);
}
// Insert Node at the end.
struct Node * insertAtEnd(struct Node *head)
{
struct Node *newNode, *ptr;
newNode = CreateNode();
ptr = head;
while(ptr->next != NULL)
ptr = ptr->next;
ptr->next = newNode;
return(head);
}
struct Node *InsertBetween(struct Node * head, int pos)
{
struct Node *ptr, *newNode;
int count =1;
ptr = head;
newNode = CreateNode();
while(count < (pos-1))
{
ptr = ptr->next;
count++;
}
newNode->next = ptr->next;
ptr->next = newNode;
return(head);
}
// function to update info field.
struct Node* upDate(struct Node*head, int old, int snew)
{
struct Node *ptr;
ptr = head;
while (ptr->next!= NULL)
{
if(ptr->info == old)
ptr->info = snew;
ptr = ptr->next;
}
return(head);
}
struct Node * DeleteatFirst(struct Node * head)
{
head = head->next;
return(head);
}
struct Node * DeleteatEnd(struct Node* head)
{
struct Node *ptr = head;
while(ptr->next->next != NULL)
ptr = ptr->next;
ptr->next = NULL;
return(head);
}
struct Node * Deletebetween(struct Node* head, int pos)
{
struct Node *ptr, *newNode;
int count =1;
ptr = head;
while(count < (pos-1))
{
ptr = ptr->next;
count++;
}
newNode = ptr->next;
ptr->next = newNode->next;
return(head);
}
int main()
{
int ch1;
int pos, old, snew;
printf("\n-----PROGRAM TO CREATE LINKED LIST-------\n");
do{
printf("\nEnter your choice: \n");
printf("\n1......Create List");
printf("\n2......Display List");
printf("\n3......Insert at beginning");
printf("\n4......Insert at End");
printf("\n5..... Insert in between");
printf("\n6..... Update the list Node");
printf("\n7......Delete at First");
printf("\n8......Delete at End");
printf("\n9......Delete in between");
printf("\n0......Exit");
scanf("%d", &ch1);
switch (ch1)
{
case 1:
head = CreateLL(head);
break;
case 2:
head = displayLL(head);
break;
case 3:
head = insertAtBeg(head);
break;
case 4:
head = insertAtEnd(head);
break;
case 5:
printf("Enter the position to insert new node:");
scanf("%d", &pos);
head = InsertBetween(head, pos);
break;
case 6:
printf("Enter the node value to be updated:");
scanf("%d", &old);
printf("Enter the value to update:");
scanf("%d", &snew);
head = upDate(head, old, snew);
break;
case 7:
head = DeleteatFirst(head);
break;
case 8:
head = DeleteatEnd(head);
break;
case 9:
printf("Enter the position to delete node:");
scanf("%d", &pos);
head = Deletebetween(head, pos);
break;
case 0:
printf("\n-----BYE-BYE------\n");
exit(0);
}
}while(1);
}