-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyLinkedList.c
196 lines (193 loc) · 4.54 KB
/
SinglyLinkedList.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
#include <stdio.h>
#include <stdlib.h>
struct LList
{
int data;
struct LList *next;
}*head = NULL;
int count(void);
void beforeHead(int );
void afterTail(int );
void insertInLinkedList(int , int );
void display(void);
void deleteNode(int );
void deleteLinkedList(void);
int main(int argc, const char * argv[]) {
int data , ch , position;
while(1)
{
printf("Enter your choice\n1.insert node in the beginning\n2.insert node at the end\n3.insert node by position\n4.display\n5.Delete a node by Position\n6.Delete Full list\n7.Exit\n");
scanf("%d",&ch);
switch (ch) {
case 1:
printf("Enter node data\n");
scanf("%d",&data);
beforeHead(data);
break;
case 2:
printf("Enter node data\n");
scanf("%d",&data);
afterTail(data);
break;
case 3:
printf("Enter position of the new node\n");
scanf("%d",&position);
printf("Enter node data\n");
scanf("%d",&data);
insertInLinkedList(data, position);
break;
case 4:
display();
break;
case 5:
printf("ENter the position of the node to delete\n");
scanf("%d",&position);
deleteNode(position);
break;
case 6:
deleteLinkedList();
break;
case 7:exit(1);
break;
default:printf("Invalid choice\n");
break;
}
}
return 0;
}
int count()
{
int count=0;
struct LList *temp = head;
while(temp){
count++;
temp = temp->next;
}
return count;
}
void beforeHead(int data)
{
struct LList *newNode = (struct LList*)malloc(sizeof(struct LList));
newNode->data = data;
newNode->next = NULL;
if(!newNode){
printf("Memory error\n");
return;
}
if(head == NULL)
head = newNode;
else{
newNode->next = head;
head = newNode;
}
}
void afterTail(int data)
{
struct LList *newNode = (struct LList*)malloc(sizeof(struct LList)),*temp = head;
newNode->data = data;
newNode->next = NULL;
if(!newNode){
printf("Memory error\n");
return;
}
if(head == NULL)
head = newNode;
else{
while(temp->next)
temp = temp->next;
temp->next = newNode;
}
}
void insertInLinkedList(int data , int position)
{
struct LList *p, *newNode , *c;
newNode = (struct LList*)malloc(sizeof(struct LList));
if(!newNode){
printf("Memory error\n");
return;
}
newNode->data = data;
newNode->next = NULL;
p = head;
if(position == 1){
newNode->next = p;
head = newNode;
}
else{
if(position > count()+1)
{
printf("Entered position of newNode is Invalid\nCurrent size of List = %d\n",count());
return;
}
else
{
position--;
while(position--){ //number of loops = position
c = p;
p = p->next;
}
newNode->next = p;
c->next = newNode;
}
}
}
void display()
{
struct LList *temp = head;
if(head == NULL)
{
printf("List is empty\n");
return;
}
else{
while (temp) {
printf("%d ",temp->data);
temp = temp->next;
}
}
printf("\n");
}
void deleteNode(int position)
{
struct LList *p = head,*c;
if(head == NULL)
{
printf("List is Empty\n");
return;
}
else{
if(position == 1){
head = p->next;
free(p);
return;
}
else{
if(position > count())
{
printf("Entered position of newNode is Invalid\nCurrent size of List = %d\n",count());
return;
}
else
{
position--;
while(position--){ //number of loops = position
c = p;
p = p->next;
}
c->next = p->next;
free(p);
// free(c);
}
}
}
}
void deleteLinkedList()
{
struct LList *iterator = head , *temp;
while (iterator) {
temp = iterator->next;
free(iterator);
iterator = temp;
}
head = NULL;
}