-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.cpp
More file actions
186 lines (181 loc) · 4.04 KB
/
List.cpp
File metadata and controls
186 lines (181 loc) · 4.04 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
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
#include<iostream>
using namespace std;
#include"Node.cpp"
class List {
Node *headNode;
Node *currentNode;
Node *lastCurrentNode;
int size;
public:
List()
{
headNode = 0;
currentNode = 0;
lastCurrentNode = 0;
size = 0;
}
void insert(int n) //n=100
{
if (currentNode != 0) //if list is not empty so current node will be not empty
{
if (currentNode->getNextNode() == 0) //adding node at the end of the list
{
Node *newNode = new Node();
lastCurrentNode = currentNode;
currentNode->setNextNode(newNode);
currentNode = newNode;
currentNode->setValue(n);
currentNode->setNextNode(0);
size++;
}
else //when node will be added between two existing node
{
Node *newNode = new Node();
newNode->setNextNode(currentNode->getNextNode());
currentNode->setNextNode(newNode);
lastCurrentNode = currentNode;
currentNode = newNode;
currentNode->setValue(n);
size++;
}
}
else
{
Node *newNode = new Node();
headNode = newNode;
currentNode = newNode;
currentNode->setValue(n);
currentNode->setNextNode(0);
size++;
cout << "first element is added in the list" << endl;
}
}
void insertAtBegin(int n)
{
Node *newNode = new Node();
headNode = newNode;
currentNode = newNode;
currentNode->setValue(n);
currentNode->setNextNode(0);
size++;
}
void insertAtEnd(int n)
{
if (currentNode != 0) //if list is not empty so current node will be not empty
{
if (currentNode->getNextNode() == 0) //adding node at the end of the list
{
Node *newNode = new Node();
lastCurrentNode = currentNode;
currentNode->setNextNode(newNode);
currentNode = newNode;
currentNode->setValue(n);
currentNode->setNextNode(0);
size++;
}
}
}
void insertBtweenNodes(int n)
{
if (currentNode != 0)
{
{
Node *newNode = new Node();
newNode->setNextNode(currentNode->getNextNode());
currentNode->setNextNode(newNode);
lastCurrentNode = currentNode;
currentNode = newNode;
currentNode->setValue(n);
size++;
}
}
}
void DeleteNode(int n)
{
if (headNode->getValue() == n) {
Node *newNode = new Node();
if (headNode->getNextNode() == NULL)
{
cout << "this list cant be deleted" << endl;
}
else {
Node *newNode = new Node();//delet head node
newNode = headNode;
headNode = headNode->getNextNode();
delete headNode;
}
}
}
void update(int n, int num)
{
int i = 0;
if (headNode == NULL)
{
cout << "there is no element in list" << endl;
return;
}
currentNode = headNode;
while (currentNode->getNextNode() != 0)
{
if (currentNode->getValue() == num)
{
currentNode->setValue(n);
}
currentNode = currentNode->getNextNode();
i++;
}
cout << "the list is updated";
}
void display()
{
Node *newNode = new Node();
newNode = headNode;
while (newNode != NULL)
{
cout << newNode->getValue()<<endl;
newNode = newNode->getNextNode();
}
}
bool search(int n)
{
currentNode = headNode;
while (currentNode != NULL)
{
if (currentNode->getValue() == n)
{
return true;
currentNode = currentNode->getNextNode();
}
return false;
}
}
void sumLinkedList()
{
int sum = 0;
currentNode = headNode;
while (currentNode != NULL)
{
sum = sum + currentNode->getValue();
currentNode = currentNode->getNextNode();
}
//Node *newNode=
}
void start()
{
lastCurrentNode = 0;
currentNode = headNode;
}
void move()
{
lastCurrentNode = currentNode;
currentNode = currentNode->getNextNode();
}
int get()
{
return currentNode->getValue();
}
int getSize()
{
return size;
}
};