-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinked_list.c
165 lines (159 loc) · 3.17 KB
/
linked_list.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
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
struct list
{
int data;
struct list *next;
};
struct list *head = 0, *newnode, *temp;
struct list *prev, *curr, *nextnode;
int choice = 1, count = 0;
void newlist()
{
while (choice)
{
newnode = (struct list *)malloc(sizeof(struct list));
printf("enter data");
scanf("%d", &newnode->data);
newnode->next = 0;
if (head == 0)
{
head = temp = newnode;
}
else
{
temp->next = newnode;
temp = newnode;
}
printf("do you wannt to continue(0,1)");
scanf("%d", &choice);
count++;
}
}
void traversal()
{
temp = head;
printf("count is %d \n", count);
while (temp != 0)
{
printf("%d \n", temp->data);
temp = temp->next;
}
}
void reversal()
{
prev = 0;
curr = nextnode = head;
while (curr != 0)
{
nextnode = nextnode->next;
curr->next = prev;
prev = curr;
curr = nextnode;
}
head = prev;
temp = head;
printf("linked list after reversing \n");
while (temp != 0)
{
printf("%d \n", temp->data);
temp = temp->next;
}
temp = head;
}
void insertion()
{
int posi, value, i = 1;
printf("enter the position where you want to insert the node");
scanf("%d", &posi);
newnode = (struct list *)malloc(sizeof(struct list));
printf("enter the value you want to insert \n");
scanf("%d", &value);
newnode->data = value;
if (posi == 1)
{
head = newnode;
newnode->next = temp;
}
else if (posi < 1 && posi > count + 1)
{
printf("invalid input");
}
else if (posi == count)
{
while (temp->next != 0)
{
printf("%d \n", temp->data);
temp = temp->next;
}
temp->next = newnode;
newnode->next = 0;
}
else
{
while (i < posi)
{
temp = temp->next;
i++;
}
newnode->next = temp->next;
temp->next = newnode;
}
traversal();
}
void deletenode()
{
int pos, i = 1;
if (head == 0)
{
printf("underflow");
}
printf("enter the position you want to delete");
scanf("%d", &pos);
if (pos == 1)
{
temp = head;
head = temp->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);
}
}