-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseveralNODESatback.cpp
More file actions
60 lines (54 loc) · 1.26 KB
/
Copy pathseveralNODESatback.cpp
File metadata and controls
60 lines (54 loc) · 1.26 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
#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, *lastnode;
int number;
listpointer = NULL;
// set up the first Node
cout << "Enter the first integer value ";
cin >> number;
listpointer = new Node;
listpointer->loaddata(number, NULL);
lastnode = listpointer;
// insert the other nodes
while(number >= 0) {
cout << "Enter an integer value (-1 to stop) ";
cin >> number;
if (number >= 0) {
temp = new Node;
temp->loaddata(number, NULL);
lastnode->setnext(temp);
lastnode = temp;
}
}
displaylist();
}
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;
}