-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathlinked_list.c
More file actions
166 lines (155 loc) · 3.72 KB
/
linked_list.c
File metadata and controls
166 lines (155 loc) · 3.72 KB
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
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *link;
}* node;
node head = NULL;
node create(){
node temp = (node)malloc(sizeof(struct node));
if (temp == NULL)
printf("\nOut of Memory Space");
else{
printf("\nEnter the data : ");
scanf("%d", &temp->data);
temp->link = NULL;
return temp;
}
}
void display(){
if (head == NULL)
printf("\nList is empty");
else{
node ptr = head;
printf("\nThe elements are : ");
while (ptr != NULL){
printf("%d ", ptr->data);
ptr = ptr->link;
}
}
}
void insert_begin(){
node temp = create();
if (head == NULL)
head = temp;
else{
temp->link = head;
head = temp;
}
}
void insert_end(){
node temp = create();
if (head == NULL)
head = temp;
else{
node ptr = head;
while (ptr->link != NULL)
ptr = ptr->link;
ptr->link = temp;
}
}
void insert_pos(){
int i, count;
node ptr, temp = create();
printf("\nEnter the position for new node to be inserted : ");
scanf("%d", &count);
if (count == 0){
temp->link = head;
head = temp;
}
else{
for (i = 0, ptr = head; i < count-1 ; i++){
if (ptr == NULL){
printf("\nPosition not found");
return;
}
ptr = ptr->link;
}
temp->link = ptr->link;
ptr->link = temp;
}
}
void delete_begin(){
node ptr = head;
if (ptr == NULL)
printf("\nList is Empty");
else{
printf("\nDeleted element is : %d", ptr->data);
head = head->link;
free(ptr);
}
}
void delete_end(){
node temp, ptr = head;
if (head == NULL)
printf("\nList is Empty");
else if (head->link == NULL){
printf("\nDeleted element is : %d", ptr->data);
head = NULL;
free(ptr);
}
else{
while (ptr->link != NULL){
temp = ptr;
ptr = ptr->link;
}
printf("\nThe deleted element is : %d", ptr->data);
temp->link = NULL;
free(ptr);
}
}
void delete_pos(){
int i, pos;
node temp, ptr = head;
if (head == NULL)
printf("\nList is Empty");
else{
printf("\nEnter the position of the node to be deleted : ");
scanf("%d", &pos);
if (pos == 0){
printf("Deleted element is : %d", ptr->data);
head = head->link;
free(ptr);
}
else{
for (i = 0; i < pos; i++){
temp = ptr;
ptr = ptr->link;
if (ptr == NULL){
printf("\nPosition not Found");
return;
}
}
printf("Deleted element is : %d", ptr->data);
temp->link = ptr->link;
free(ptr);
}
}
}
void main(){
int ch;
printf("\n\t\t\tMENU\n\n1.Display\n2.Insert at the beginning\n3.Insert at the end\n4.Insert at specified position\n5.Delete from beginning\n6.Delete from the end\n7.Delete from specified position\n0.Exit\n--------------------------------------");
while (1){
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch){
case 1: display();
break;
case 2: insert_begin();
break;
case 3: insert_end();
break;
case 4: insert_pos();
break;
case 5: delete_begin();
break;
case 6: delete_end();
break;
case 7: delete_pos();
break;
case 0: exit(0);
default:
printf("\nWrong Choice");
}
}
}