-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.cpp
144 lines (138 loc) · 3.09 KB
/
linkedlist.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
#include <iostream>
// node definition
struct Node {
int data;
Node *next;
};
// linked list definition -- ye
/* class LinkedList {
public:
LinkedList(Node *head) {
};
Insert();
Remove();
private:
int length;
}; */
// linked list definition -- blog
class List {
private:
Node *head, *tail;
public:
List() {
head = NULL;
tail = NULL;
}
void CreateNode(int value) {
// check empty -> head pointer is equal to NULL
// create both head and tail node
Node *temp = new Node;
temp -> data = value;
temp -> next = NULL;
if (head == NULL) {
head = temp;
tail = temp;
temp = NULL;
}
// a linked list is created already, insert this node at the end of
// the linked list including 2 steps
else {
tail -> next = temp;
tail = temp;
}
}
void display() {
Node * temp = head;
if (temp == NULL) {
std::cout << "empty linked list" << std::endl;
}
while (temp) {
std::cout << temp -> data << " ";
temp = temp -> next;
}
std::cout << std::endl;
}
void insert(int value, int position) {
// insert at the start including 2 steps
Node *temp = new Node;
temp -> data = value;
temp -> next = NULL;
if (position == 1) {
temp -> next = head;
head = temp;
}
// insert at the end
else if (position == -1) {
tail -> next = temp;
tail = temp;
}
// insert at a particular position including 2 steps
// search from the head node and loop search
else {
Node *pre = new Node;
Node *cur = new Node;
cur = head;
for (int i =1; i < position; i++) {
pre = cur;
cur = cur -> next;
//std::cout << "check value: " << cur -> data << std::endl;
}
pre -> next = temp;
temp -> next = cur;
}
}
void deletion(int position) {
// deletion at the start including 3 steps
if (position == 1) {
Node *temp = new Node;
temp = head;
head = head -> next;
delete temp;
}
// deletion at the end
else if (position == -1) {
Node *pre = new Node;
Node *cur = new Node;
cur = head;
while (cur -> next) {
pre = cur;
cur = cur -> next;
}
pre -> next = NULL;
tail = pre;
delete cur;
}
// deletion at a particular position
else {
Node *pre = new Node;
Node *cur = new Node;
cur = head;
for (int i = 1; i < position; i++) {
pre = cur;
cur = cur -> next;
}
pre -> next = cur -> next;
delete cur;
}
}
};
int main() {
List a;
a.CreateNode(3);
a.CreateNode(4);
a.CreateNode(5);
a.CreateNode(6);
a.display();
a.insert(8, 3);
a.display();
a.deletion(1);
a.display();
a.deletion(2);
a.display();
a.deletion(1);
a.deletion(-1);
a.display();
a.deletion(1);
a.display();
return 0;
}