-
Notifications
You must be signed in to change notification settings - Fork 173
/
linkedlist.c
211 lines (164 loc) · 3.73 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
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
int calSize(struct Node *node)
{
int size = 0;
while (node != NULL)
{
node = node->next;
size++;
}
return size;
}
void inserPosition(int pos, int data, struct Node **head)
{
int size = calSize(*head);
if (pos < 1 || pos > size)
{
printf("Can't insert, %d is not a valid position\n", pos);
}
else
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp = *head;
newNode->data = data;
newNode->next = NULL;
while (--pos)
{
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
void traversal(struct Node *ptr)
{
while (ptr != NULL)
{
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
}
void insertStart(struct Node **head, int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void insertLast(struct Node **head, int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL)
{
*head = newNode;
return;
}
struct Node *temp = *head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
void deleteStart(struct Node **head)
{
struct Node *temp = *head;
if (*head == NULL) // If the linkedlist is empty;
{
printf("Linked List Empty, nothing to delete\n");
return;
}
*head = (*head)->next;
printf("Deleted: %d\n", temp->data);
free(temp);
}
void deleteEnd(struct Node **head)
{
struct Node *temp = *head;
struct Node *previous;
if (*head == NULL) // If the linkedlist is empty;
{
printf("Linked List Empty, nothing to delete\n");
return;
}
if (temp->next == NULL) // If the linkedlist have only one node
{
printf("Deleted: %d\n", (*head)->data);
*head = NULL;
return;
}
while (temp->next != NULL)
{
previous = temp;
temp = temp->next;
}
previous->next = NULL;
printf("Deleted: %d\n", temp->next);
free(temp);
}
void deletePosition(struct Node **head, int pos)
{
struct Node *temp = *head;
struct Node *previous;
int size = calSize(*head);
if (pos < 1 || pos > size)
{
printf("Enter valid position\n");
return;
}
if (pos == 1)
{
printf("Deleted: %d\n", temp->data);
*head = (*head)->next;
free(temp);
return;
}
while (--pos)
{
previous = temp;
temp = temp->next;
}
previous->next = temp->next;
printf("Deleted: %d\n", temp->data);
free(temp);
}
int main()
{
struct Node *head = NULL;
struct Node *second;
struct Node *third;
struct Node *fourth;
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
fourth = (struct Node *)malloc(sizeof(struct Node));
head->data = 7;
head->next = second;
second->data = 11;
second->next = third;
third->data = 41;
third->next = fourth;
fourth->data = 66;
fourth->next = NULL;
traversal(head);
insertStart(&head, 10);
insertStart(&head, 30);
insertStart(&head, 70);
printf("NewLIST\n");
traversal(head);
insertLast(&head, 1);
insertLast(&head, 2);
insertLast(&head, 3);
insertLast(&head, 4);
printf("NewLIST\n");
traversal(head);
return 0;
}