-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeletingNODE.cpp
More file actions
59 lines (52 loc) · 1.24 KB
/
Copy pathdeletingNODE.cpp
File metadata and controls
59 lines (52 loc) · 1.24 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
#include <iostream>
using namespace std;
class Node {
private:
int value;
// you can include more data items here
Node* next;
public:
void loaddata(int v, Node* p);
int getvalue() { return value; }
Node* getnext() { return next; }
void setvalue(int v) { value = v; }
void setnext(Node* p) { next =p; }
};
void displaylist();
Node* listpointer;
int main() {
Node *temp, *temp2, *previous;
int i;
listpointer = NULL;
// insert three nodes in the list
for (i = 0; i < 3; i++) {
temp = new Node;
temp->loaddata(i * 10, listpointer);
listpointer = temp;
}
displaylist();
cout << endl;
// noewe remove the second node
temp = listpointer->getnext(); //the second Node
temp2 = temp->getnext();
previous = listpointer;
previous->setnext(temp2);
delete temp; // delete the node to free memory
displaylist();
cout << endl;
}
void displaylist() {
Node* current;
int currentvalue;
current = listpointer;
while (current != NULL) {
currentvalue = current->getvalue();
cout << currentvalue << ", ";
current = current->getnext();
}
}
//--------methods for the Node ------------
void Node::loaddata(int v, Node* p) {
value = v;
next = p;
}