-
Notifications
You must be signed in to change notification settings - Fork 34
/
simple_linked_list.c
216 lines (213 loc) · 4.9 KB
/
simple_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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL; //Head now points to NULL and will remain so as long as it's an empty Linked List.
void create() //Function to create a linked list, taking number of nodes as input.
{
int n;
printf("Enter how many nodes you want \n");
scanf("%d", &n);
struct node *temp;
for (int i = 0; i < n; i++)
{
printf("Enter data %d :", (i + 1));
temp = (struct node *)malloc(sizeof(struct node *));
scanf("%d", &temp->data);
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
struct node *ptr;
ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
}
}
int length() //Funtion that returns the lenght of the linked list.
{
int count = 0;
struct node *ptr;
ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
count++;
}
return count;
}
void display() //Funtion to display all the nodes in the linked list.
{
struct node *ptr;
ptr = head;
printf("%d -> ", ptr->data);
do
{
ptr = ptr->next;
if (ptr->next == NULL)
{
printf("%d", ptr->data);
}
else
{
printf("%d -> ", ptr->data);
}
} while (ptr->next != NULL);
}
void append() //Function to add a new node to the end of the linked list.
{
printf("Enter data :");
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node *));
scanf("%d", &temp->data);
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
struct node *ptr;
ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
}
void insert() //Function to insert a node at a specified location.
{
int loc, i = 2;
printf("\nEnter your location\n");
scanf("%d", &loc);
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node *));
scanf("%d", &temp->data);
temp->next = NULL;
if (loc == 1)
{
temp->next = head;
head = temp;
}
else
{
struct node *ptr;
ptr = head;
while (i < loc && ptr->next != NULL)
{
ptr = ptr->next;
i++;
}
if (ptr->next == NULL)
{
ptr->next = temp;
}
else
{
temp->next = ptr->next;
ptr->next = temp;
}
}
}
void delete () //Funtion to delete a node at a specified location.
{
int loc, i = 1;
struct node *ptr, *q;
ptr = head;
printf("\nEnter your location to be deleted\n");
scanf("%d", &loc);
if (head == NULL)
{
printf("The List is empty\n");
}
else
{
if (loc == 1)
{
head = ptr->next;
ptr->next = NULL;
free(ptr);
}
else
{
while (i < loc - 1 && ptr->next != NULL)
{
ptr = ptr->next;
i++;
}
q = ptr->next;
ptr->next = q->next;
q->next = NULL;
free(q);
}
}
}
void reverse_linked_list() //Funtion to Reverse the linked list.
{
struct node *p, *q;
int len, temp, k;
len = length();
p = head;
q = head;
int i = 0;
int j = len;
while (i < j)
{
k = 0;
while (k < j)
{
q = q->next;
k++;
}
temp = p->data;
p->data = q->data;
q->data = temp;
i++;
j--;
p = p->next;
q = head;
}
}
void main()
{
int d;
do
{
printf("\nEnter your option\n1. Create\n2. Display\n3. Insert\n4. Delete\n5. Reverse\n6. Append\n7. Exit\n");
scanf("%d", &d);
switch (d)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert();
break;
case 4:
delete ();
break;
case 5:
reverse_linked_list();
break;
case 6:
append();
break;
default:
break;
}
} while (d != 7);
}