Repository navigation
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
148 lines (140 loc) · 3.79 KB
/
Copy pathlinked_list.cpp
File metadata and controls
148 lines (140 loc) · 3.79 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
#include "iostream"
using namespace std;
struct node{
int val;
node* next;
};
// Initializing global variables
node *tail (NULL), *head (NULL), *temp, *iter;
// Print elements of the list
void print()
{
// iterator starting from head
iter = head;
// In case of an empty list
if(head == NULL) cout << endl;
else{
// Iterating over each element
while(iter->next != 0){
cout << iter->val << ' ';
iter = iter->next;
}
cout << iter->val;
cout << endl;
}
}
// To append an element
void append()
{
cout << "Element to be appended: ";
// Creating a new node
temp = new node;
// In case of an empty list
if(head == NULL){
head = temp;
tail = temp;
cin >> head->val;
}
// In case of a non-empty list
else{
tail->next = temp;
temp->next = NULL;
tail = temp;
cin >> tail->val;
}
}
// To pop the last element
void pop()
{
// In case of and empty list
if(head == NULL) cout << "No element in linked list to pop";
// In case of a single element list
else if(head == tail){
delete head;
delete tail;
head = tail = NULL;
}
// For a non-empty list
else{
iter = head;
// Iterating over each element
while(iter->next != tail) iter = iter->next;
iter->next = 0;
delete tail;
tail = iter;
}
}
// Insert an element in the list
void insert()
{
int pos;
cout << "Position you wanna insert: ";
// Taking position-to-be-inserted from user
cin >> pos;
// Creating a new node
temp = new node;
cout << "Element to be inserted: ";
// In case of inserting an element at the first position
if(pos == 0){
temp->next = head;
head = temp;
cin >> head->val;
}
// Inserting at another position
else{
iter = head;
// Iterating over each element
// Inserting an element at a position out of range becomes tail element
for(int i = 0; i < pos-1 && iter != tail; ++i) iter = iter->next;
temp->next = iter->next;
iter->next = temp;
if(temp->next == 0) tail = temp;
cin >> temp->val;
}
}
// Deleting an element given its position
void del()
{
int pos;
cout << "Enter the position of the element you wanna delete: ";
// Taking position-to-be-deleted from user
cin >> pos;
// In case of an empty list
if(pos == 0 && head == NULL) cout << "Position out of range. Try appending or inserting";
// Deleting the first element
else if(pos == 0){
temp = head->next;
delete head;
head = temp;
}
// Deleting other elements
else{
iter = head;
// Iterating over each element
// If the position is out of range the last element gets popped
for(int i = 0; i < pos-1 && iter->next != tail; ++i) iter = iter->next;
temp = iter->next;
iter->next = temp->next;
delete temp;
// If the last element been deleted, tail is repositioned
if(iter->next == 0) tail = iter;
}
}
int main()
{
cout << "An empty list has been intitalized\n";
cout << "Available operations - append, insert, pop, delete, exit\n";
string key;
while(true){
cout << "Operation to be performed: ";
cin >> key;
// Function calls
if(key == "append") append();
else if(key == "pop") pop();
else if(key == "insert") insert();
else if(key == "delete") del();
else if(key == "exit") return 0;
else cout << "Please check the keyword entered\n";
print();
}
}