This repository has been archived by the owner on Mar 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_list.cpp
82 lines (64 loc) · 1.82 KB
/
node_list.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
#include <cstddef>
#include <iostream>
using namespace std;
class node
{
public:
int* content; // content of the node
node* next_node; // pointer to the next node
node() // constructor
{
content = new int; // dynamic memory allocation for int
}
~node() // destructor
{
delete content;
content = NULL;
}
};
class node_list
{
public:
node* head; // the starting point of the list, points to the very first node of the list
node* tail; // unnecessary pointer that always equals nullptr
node* current_node; // pointer to the current node
node_list() // constructor
{
head = new node();
tail = new node();
current_node = new node();
(*head).next_node = NULL; // setting the next node pointer to nullptr
(*tail).next_node = NULL; // optional
}
~node_list() // destructor
{
delete head;
delete tail;
delete current_node;
head = NULL;
tail = NULL;
current_node = NULL;
}
void add(int content) // add a node function
{
node* new_node = new node; // creating a new node
*((*new_node).content) = content; // setting the given value
(*new_node).next_node = tail; // optional
(*current_node).next_node = new_node; // setting the previous node attribute
if((*head).next_node == NULL)
(*head).next_node = new_node; // setting the head pointer
current_node = new_node; // changing to the next node
}
int get(int index) // get a node function
{
node* needed_node; // allocating an auxiliar node
needed_node = (*head).next_node; // setting the starting point
for(int i = 0; i < index; i++) // going through all the nodes until one is that we need
needed_node = (*needed_node).next_node;
return *((*needed_node).content); // return the asked node
}
};
int main()
{
return 0;
}