forked from shubhidua/Hacktoberfest-20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.c
219 lines (191 loc) · 5.42 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
207
208
209
210
211
212
213
214
215
216
217
218
219
#include<stdio.h>
#include<stdlib.h>
struct LL
{
int data;
struct LL *next;
};
// Function to return number of nodes in linked list
int ListTraverse (struct LL **head) {
struct LL *current = *head;
int count = 0;
while(current){
count++;
current = current -> next;
}
return count;
}
// Function to print linked list
void ListPrint (struct LL **head) {
struct LL *current = *head;
while(current){
printf("%d -> ", current -> data);
current = current -> next;
}
printf("NULL \n");
}
// Function to insert node at the beginning of the list
void InsertStart (struct LL **head, int data) {
struct LL *temp, *newNode;
temp = *head;
newNode = (struct LL *)malloc(sizeof(struct LL));
newNode -> data = data;
newNode -> next = temp;
*head = newNode;
ListPrint(head);
}
// Function to insert node at the last position of the list
void InsertEnd (struct LL **head, int data) {
struct LL *p, *newNode;
newNode = (struct LL *)malloc(sizeof(struct LL));
newNode -> data = data;
newNode -> next = NULL;
if (*head == NULL) {
*head = newNode;
printf("The linked list is not created yet, therefore inserting at first position, \n");
}
else {
int count=0;
p = *head;
while (p -> next!=NULL)
{
p = p->next;
count++;
}
p -> next = newNode;
}
ListPrint(head);
}
// Function to insert node at the specific position of the list
void InsertPosition (struct LL **head, int data , int position) {
struct LL *p, *q, *newNode;
int k=1;
newNode = (struct LL *)malloc(sizeof(struct LL));
newNode -> data = data;
p=*head;
if (*head == NULL) {
*head = newNode;
printf("The linked list is not created yet, therefore inserting at first position, \n");
}
else if (position == 1) {
newNode->next = p;
*head = newNode;
}
else {
while (p != NULL && k<position) {
q = p;
p = p->next;
k++;
}
if (k != position)
printf("Given position exceeds the total number of nodes, therefore inserting at the end. \n");
q ->next = newNode;
newNode->next = p;
}
ListPrint(head);
}
int main() {
struct LL *head = NULL;
int opt = 0, count = 0;
char c='N';
int n=0,f=0,d=0,p=0;
loop:;
printf("\n");
printf("1. Append nodes. \n");
printf("2. Insert new node at start. \n");
printf("3. Insert new node at end. \n");
printf("4. Insert new node at position. \n");
printf("5. Traverse List. \n");
printf("6. Print list. \n\n");
printf("Choose your option : ");
scanf("%d",&opt);
switch (opt)
{
case 1 :
printf("Enter no. of nodes : ");
scanf("%d",&n);
for (int i = 1; i <= n; i++)
{
printf("Enter the data of node %d : ",i);
scanf("%d",&d);
InsertEnd(&head,d);
}
printf("\n");
printf("Want to choose more?(Y/N) : ");
scanf(" %c",&c);
// clear screen function in linux
system("clear"); // Use < system("cls"); > in windows
if (c=='y' || c=='Y')
goto loop;
break;
case 2 :
printf("Enter the data of node 1 : ");
scanf("%d",&f);
InsertStart(&head,f);
printf("\n");
printf("Want to choose more?(Y/N) : ");
scanf(" %c",&c);
// clear screen function in linux
system("clear"); // Use < system("cls"); > in windows
if (c=='y' || c=='Y')
goto loop;
break;
case 3 :
printf("Enter the data of last node : ");
scanf("%d",&d);
InsertEnd(&head,d);
printf("\n");
printf("Want to choose more?(Y/N) : ");
scanf(" %c",&c);
// clear screen function in linux
system("clear"); // Use < system("cls"); > in windows
if (c=='y' || c=='Y')
goto loop;
break;
case 4 :
printf("Enter the position : ");
scanf("%d", &p);
printf("Enter the data of node %d : ",p);
scanf("%d",&d);
InsertPosition(&head,d,p);
printf("\n");
printf("Want to choose more?(Y/N) : ");
scanf(" %c",&c);
// clear screen function in linux
system("clear"); // Use < system("cls"); > in windows
if (c=='y' || c=='Y')
goto loop;
break;
case 5 :
count = ListTraverse(&head);
printf("Total number of nodes in Linked List : %d\n", count);
printf("\n");
printf("Want to choose more?(Y/N) : ");
scanf(" %c",&c);
// clear screen function in linux
system("clear"); // Use < system("cls"); > in windows
if (c=='y' || c=='Y')
goto loop;
case 6 :
ListPrint(&head);
printf("\n");
printf("Want to choose more?(Y/N) : ");
scanf(" %c",&c);
// clear screen function in linux
system("clear"); // Use < system("cls"); > in windows
if (c=='y' || c=='Y')
goto loop;
break;
default:
printf("Wrong option \n");
printf("\n");
printf("Want to choose more?(Y/N) : ");
scanf(" %c",&c);
// clear screen function in linux
system("clear"); // Use < system("cls"); > in windows
if (c=='y' || c=='Y')
goto loop;
break;
}
return 0;
}