-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathlist.cpp
187 lines (154 loc) · 3.4 KB
/
list.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
// Program to Insert node at nth position in a linked list
// Time Complexity - O(N)
#include <iostream>
using namespace std;
struct Node
{
//To store the element
int data;
//To store the address of next element
Node *next;
};
//Pointer to head Node initialize to NULL
Node *head = NULL;
int size = 0;
void insertAtNthNode(int x, int n)
{
if (n < 1 || n > size + 1)
{
cout << "Position is not valid.\n";
return;
}
// Increase size
++size;
//Extra space for new Node
Node *temp = (Node *)malloc(sizeof(Node));
//Store the value in the Node
temp->data = x;
//Initialize address as NULL
temp->next = NULL;
//If Insertion taking place at Beginning
if (n == 1)
{
temp->next = head;
head = temp; //It Point Head Node
return;
}
//To connect link with (n-1)th Node
Node *temp1 = head;
//Iterate to get (n-1)th Node address
for (int i = 0; i < n - 2; i++)
temp1 = temp1->next;
//Store new address
temp->next = temp1->next;
temp1->next = temp;
}
//Fxn to delete N'th Node
void Delete(int n)
{
if (n < 1 || n > size + 1)
{
cout << "Position is not valid.\n";
return;
}
// Decrease the Size
--size;
Node *temp1 = head;
if (n == 1)
{
//Head node is deleted and
//Now head is pointed to next element
head = temp1->next;
free(temp1);
return;
}
for (int i = 0; i < n - 2; i++)
temp1 = temp1->next;
//Pointer who point to N'th node
Node *temp2 = temp1->next;
temp1->next = temp2->next;
//delete N'th node
free(temp2);
}
void Update(int x, int n)
{
if (n < 1 || n > size + 1)
{
cout << "Position is not valid.\n";
return;
}
Node* temp = head;
// Iterate n-1 times
while (--n)
temp = temp->next;
//Update its value
temp->data = x;
}
//function to Reverse linked list
void Reverse()
{
Node *current, *prev, *next;
current = head;
prev = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
void PrintList()
{
//Initialize to Head Node
Node *temp = head;
cout << "\nList is : ";
//Print until Last node
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << "\n";
}
int main()
{
int n, x, op;
while (true)
{
cout << "\n"
<< "Enter 1 for Insertion\n"
<< "2 for Delete a node\n"
<< "3 for Updating Value\n"
<< "4 for Priniting List\n"
<< "5 for Reverse given list\n"
<< "Any Number for Quit\n";
cin >> op;
if (op == 1)
{
cout << "Enter Number and its position : ";
cin >> x >> n;
insertAtNthNode(x, n);
}
else if (op == 2)
{
cout << "Enter the position of Node : ";
cin >> n;
Delete(n);
}
else if (op == 3)
{
cout << "Enter Number and its position : ";
cin >> x >> n;
Update(x, n);
}
else if (op == 4)
PrintList();
else if (op == 5)
Reverse();
else
break;
}
return 0;
}