forked from codingCapricorn/Hactoberfest-2020-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList-cpp.cpp
210 lines (200 loc) · 4.02 KB
/
linkedList-cpp.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *head = nullptr;
node *createNode(int value)
{
struct node *temp;
temp = new (struct node);
if (temp == nullptr)
{
cout << "Memory not allocated " << endl;
return 0;
}
else
{
temp->data = value;
temp->next = nullptr;
return temp;
}
}
void Display()
{
struct node *temp;
if (head == nullptr)
{
cout << "The List is Empty" << endl;
return;
}
temp = head;
cout << "Elements of list are: " << endl;
while (temp != nullptr)
{
cout << temp->data << "->";
temp = temp->next;
}
cout << "nullptr" << endl;
}
void Insert()
{
int value, pos, count = 0;
cout << "Enter the value to be inserted: ";
cin >> value;
struct node *temp, *s, *ptr;
temp = createNode(value);
cout << "Enter the position at which node to be inserted: ";
cin >> pos;
int i;
s = head;
while (s != nullptr)
{
s = s->next;
count++;
}
if (pos == 1)
{
if (head == nullptr)
{
head = temp;
head->next = nullptr;
}
else
{
ptr = head;
head = temp;
head->next = ptr;
}
}
else if (pos > 1 && pos <= count)
{
s = head;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else if (pos == count + 1)
{
s = head;
while (s->next != nullptr)
{
s = s->next;
}
temp->next = nullptr;
s->next = temp;
}
else
{
cout << "Position out of range" << endl;
}
Display();
}
void Delete()
{
int pos, i, count = 0;
if (head == nullptr)
{
cout << "List is empty" << endl;
return;
}
cout << "Enter the position of value to be deleted: ";
cin >> pos;
struct node *s, *ptr;
s = head;
if (pos == 1)
{
head = s->next;
}
else
{
while (s != nullptr)
{
s = s->next;
count++;
}
if (pos > 0 && pos <= count)
{
s = head;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout << "Position out of range" << endl;
}
}
free(s);
cout << "Element Deleted" << endl;
Display();
}
void Reverse()
{
struct node *ptr1, *ptr2, *ptr3;
if (head == nullptr)
{
cout << "List is empty" << endl;
return;
}
if (head->next == nullptr)
{
return;
}
ptr1 = head;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = nullptr;
ptr2->next = ptr1;
while (ptr3 != nullptr)
{
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->next;
ptr2->next = ptr1;
}
head = ptr2;
Display();
}
int main()
{
int choice;
while (true)
{
cout << "1.Insert Node at a position" << endl;
cout << "2.Delete Linked List" << endl;
cout << "3.Reverse Linked List " << endl;
cout << "4.Exit " << endl;
cout << "Enter your choice : ";
cin >> choice;
switch (choice)
{
case 1:
Insert();
cout << endl;
break;
case 2:
Delete();
cout << endl;
break;
case 3:
Reverse();
cout << endl;
break;
case 4:
cout << "Exiting..." << endl;
exit(0);
default:
cout << "Wrong choice" << endl;
}
}
}