-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkedlist.cpp
159 lines (151 loc) · 3.18 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node* next;
};
class LinkedList
{
node *head, *tail;
public:
LinkedList()
{
head = NULL;
tail = NULL;
}
void insertNode(int value)
{
//By default, we add node to the last of the list
node* temp = new node;
temp->data = value;
temp->next = NULL;
if(head == NULL)
{
head = temp;
tail = temp;
//tail->next = NULL;
}
else
{
tail->next = temp;
tail = temp;
}
}
void insertNode(int value, int position)
{
if(head == NULL)
{
if(position > 1)
{
cout << "The list is empty, so adding the node to first position" << endl;
}
insertNode(value);
}
else
{
node* newNode = new node;
newNode->data = value;
node* prevNode = NULL;
node* currNode = head;
while(position > 1 && (tail->next != currNode))
{
prevNode = currNode;
currNode = currNode->next;
//cout << prevNode->data << " " << currNode->data<<endl;
position--;
}
if(position <= 0)//Insert new node at head
{
head = newNode;
newNode->next = currNode;
}
else
{
prevNode->next = newNode;
newNode->next = currNode;
if(prevNode == tail)
{
tail = newNode;
}
}
}
}
bool deleteNode(int position)
{
if (head == NULL || position < 1)
{
return false;
}
if(position == 1)
{
head = head->next;
return true;
}
node* prevNode = NULL;
node* currNode = head;
while(position > 1 && tail->next != currNode)
{
prevNode = currNode;
currNode = currNode->next;
position--;
}
if(tail == currNode)
{
tail = prevNode;
cout << "New tail: " << tail->data << endl;
}
else if(tail == prevNode)
{
cout << "No node found at that position" << endl;
return 0;
}
prevNode->next = currNode->next;
return true;
}
void reverse_list()
{
node* prevNode = NULL;
node* currNode = head;
tail = currNode;
while(currNode != NULL)
{
node* temp = currNode->next;
currNode->next = prevNode;
prevNode = currNode;
currNode = temp;
}
head = prevNode;
}
void display()
{
node *temp = head;
cout << "LinkedList: ";
while(temp != NULL)
{
cout << temp->data << "->";
temp = temp->next;
}
cout << "NULL" << endl;
}
};
int main()
{
LinkedList sample;
sample.insertNode(2);
sample.insertNode(5);
sample.insertNode(6,2);
sample.insertNode(12,4);
//sample.insertNode(143, 11);//Makes 143 the tail node
//sample.insertNode(34, -1);//Makes 34 the head node
sample.display();
sample.deleteNode(2);
sample.display();
//sample.deleteNode(3);
sample.display();
sample.deleteNode(5);
sample.display();
sample.reverse_list();
sample.display();
return 0;
}