-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithoutHead.cpp
More file actions
188 lines (175 loc) · 3.19 KB
/
Copy pathWithoutHead.cpp
File metadata and controls
188 lines (175 loc) · 3.19 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include<stdio.h>
#include <stdlib.h>
struct LL
{
int value;
struct LL *node;
};
struct LL *init()
{
struct LL *p = (struct LL*)malloc(sizeof(struct LL));
p->value = 0;
p->node = NULL;
return p;
}
void print(struct LL *ll)
{
if(ll->node == NULL)
{
return;
}
printf("%d->",ll->value);
print(ll->node);
}
LL *insertBegin(struct LL *ll, int n)
{
if(ll->node == NULL)
{
ll->value = n;
ll->node = init();
return ll;
} else {
struct LL *temp = init();
temp->value = n;
temp->node = ll;
return temp;
}
}
void insertEnd(struct LL *ll, int n)
{
if(ll->node == NULL)
{
ll->value = n;
ll->node = init();
} else {
insertEnd(ll->node, n);
}
}
LL *find(struct LL *ll, int n, int k)
{
if(ll->node == NULL)
{
printf("Value Doesn't Exist\n");
return ll;
} else {
if(ll->value == k)
{
struct LL *temp = init();
temp->value = n;
temp->node = ll->node;
ll->node = temp;
return ll;
} else {
find(ll->node, n, k);
}
}
}
void insertAfter(struct LL *ll, int n, int k)
{
struct LL *temp = find(ll, n, k);
struct LL *next;
if(temp->node == NULL)
{
printf("Value Doesn't Exits\n");
} else {
next = ll;
while(next != NULL)
{
if(ll->value == k)
{
ll->node = temp;
free(temp);
break;
} else {
next = next->node;
}
}
}
}
void search(struct LL *ll, int n)
{
if(ll->node == NULL)
{
printf("Value Doesn't Exists\n");
return;
}
if(ll->value == n)
{
printf("Value Exists\n");
} else {
search(ll->node,n);
}
}
void ddelete(struct LL *ll, int n)
{
if(ll->node == NULL){
printf("Value Doesn't Exists\n");
return;
}
else {
if(ll->node->value == n){
ll->node = ll->node->node;
return;
} else {
ddelete(ll->node, n);
}
}
}
int total_nodes(struct LL *ll)
{
if(ll->node == NULL)
return 0;
return 1 + total_nodes(ll->node);
}
int main(int argc, char const *argv[])
{
int t,n,k;
struct LL *ll = init();
do{
printf("1.Insert back\n");
printf("2.Insert After\n");
printf("3.Insert Start\n");
printf("4.Delete\n");
printf("5.Total Nodes\n");
printf("6.Search\n");
printf("7.Print\n");
printf("8.Exit\n");
scanf("%d",&t);
switch(t)
{
case 1: printf("Enter Number to Add\n");
scanf("%d",&n);
insertEnd(ll, n);
break;
case 2: printf("Enter Number to Add\n");
scanf("%d",&n);
printf("Enter Number to Add After\n");
scanf("%d",&k);
insertAfter(ll, n, k);
break;
case 3: printf("Enter Number to Add\n");
scanf("%d",&n);
ll = insertBegin(ll, n);
break;
case 4: printf("Enter Number to Delete\n");
scanf("%d",&n);
ddelete(ll, n);
break;
case 7: printf("Linked List\n");
print(ll);
printf("\n");
break;
case 6: printf("Enter Number to Search\n");
scanf("%d",&n);
search(ll, n);
break;
case 5: printf("Total Nodes Present in Linked List is %d\n", total_nodes(ll));
break;
case 8: printf("Exiting..........\n");
break;
default: printf("Wrong Choice\n");
break;
}
}while(t!=8);
return 0;
}