-
Notifications
You must be signed in to change notification settings - Fork 2
/
ll.c
173 lines (168 loc) · 3.39 KB
/
ll.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
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
struct list
{
int data;
struct list *next;
};
struct list *head = 0;
int choice = 1, count = 0;
void newlist()
{
while (choice)
{
count++;
printf("Creating node %d\n", count);
struct list *newnode = (struct list *)malloc(sizeof(struct list));
printf("enter data");
scanf("%d", &newnode->data);
newnode->next = 0;
if (head == 0)
{
head = newnode;
}
else
{
struct list *temp = head;
while (temp->next)
{
temp = temp->next;
}
temp->next = newnode;
}
printf("do you wannt to continue(0,1)");
scanf("%d", &choice);
}
}
void traversal()
{
struct list *temp = head;
printf("count is %d \n", count);
while (temp != 0)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void reversal()
{
struct list *prev = 0;
struct list *curr = head;
struct list *nextnode = head;
while (curr != 0)
{
nextnode = nextnode->next;
curr->next = prev;
prev = curr;
curr = nextnode;
}
head = prev;
traversal();
}
void insertion()
{
int posi, value, i = 2;
struct list *temp;
printf("enter the position where you want to insert the node");
scanf("%d", &posi);
struct list *newnode = (struct list *)malloc(sizeof(struct list));
printf("enter the value you want to insert \n");
scanf("%d", &value);
newnode->data = value;
newnode->next = 0;
if (posi == 1)
{
newnode->next = head;
head = newnode;
count++;
}
else if (posi < 1 || posi > count + 1)
{
printf("invalid input");
return;
}
else if (posi == count)
{
temp = head;
while (temp->next != 0)
{
printf("%d \n", temp->data);
temp = temp->next;
}
temp->next = newnode;
newnode->next = 0;
count++;
}
else
{
temp = head;
while (i < posi)
{
temp = temp->next;
i++;
}
newnode->next = temp->next;
temp->next = newnode;
count++;
}
traversal();
}
void deletenode()
{
int pos, i = 1;
struct list *temp;
struct list *prev;
if (head == 0)
{
printf("underflow");
}
printf("enter the position you want to delete");
scanf("%d", &pos);
if (pos == 1)
{
temp = head;
head = head->next;
free(temp);
}
else
{
temp = head;
while (i < pos)
{
prev = temp;
temp = temp->next;
i++;
}
prev->next = temp->next;
free(temp);
}
traversal();
}
void main()
{
int n;
newlist();
choice = 1;
while (choice)
{
printf("enter the operattion you want to perform \n 1: for INSEERTION \n 2:for DELETION \n 3:for REVERSAL");
scanf("%d", &n);
switch (n)
{
case 1:
insertion();
break;
case 2:
deletenode();
break;
case 3:
reversal();
break;
}
printf("enter 1 to perform more operations and o for exit");
scanf("%d", &choice);
}
}