-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList_Double.cpp
More file actions
191 lines (148 loc) · 4.07 KB
/
Copy pathLinkedList_Double.cpp
File metadata and controls
191 lines (148 loc) · 4.07 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
187
188
189
190
191
#include <iostream>
#include <cstddef>
using std::cout;
using std::endl;
/* definition of the list node class */
class Node
{
friend class LinkedList;
private:
int value;
Node *pNext;
Node *pPrev;
public:
/* Constructors with No Arguments */
Node(void)
: pNext(NULL), pPrev(NULL)
{ }
/* Constructors with a given value */
Node(int val)
: value(val), pNext(NULL), pPrev(NULL)
{ }
/* Constructors with a given value and a link of the next node */
Node(int val, Node* previous, Node* next)
: value(val), pPrev(previous), pNext(next)
{}
/* Getters */
int getValue(void)
{ return value; }
Node* getNext(void)
{ return pNext; }
Node* getPrevious(void)
{ return pPrev; }
};
/* definition of the linked list class */
class LinkedList
{
private:
/* pointer of head node */
Node *pHead;
/* pointer of tail node */
Node *pTail;
public:
/* Constructors with No Arguments */
LinkedList(void);
/* Constructors with a given value of a list node */
LinkedList(int val);
/* Destructor */
~LinkedList(void);
/* Traversing the list and printing the value of each node */
void traverse_and_print();
void traverse_and_printBackwards();
void push_back(int val);
};
LinkedList::LinkedList()
{
/* Initialize the head and tail node */
pHead = pTail = NULL;
}
LinkedList::LinkedList(int val)
{
/* Create a new node, acting as both the head and tail node */
pHead = new Node(val);
pTail = pHead;
}
LinkedList::~LinkedList()
{
}
void LinkedList::traverse_and_print()
{
Node *p = pHead;
/* The list is empty? */
if (pHead == NULL) {
cout << "The list is empty" << endl;
return;
}
cout << "LinkedList: ";
/* A basic way of traversing a linked list */
while (p != NULL) { /* while there are some more nodes left */
/* output the value */
cout << p->value;
/* The pointer moves along to the next one */
p = p->pNext;
}
cout << endl;
}
void LinkedList::traverse_and_printBackwards()
{
Node *p = pTail;
/* The list is empty? */
if (pHead == NULL) {
cout << "The list is empty" << endl;
return;
}
cout << "LinkedList: ";
while(p != NULL)
{
cout << p->value;
p = p->pPrev;
}
cout << endl;
}
//#####################################################################
/*Adds a new node to the end of the current list*/
void LinkedList::push_back(int val){
/*Your code here*/
if(this->pHead != 0)//If list is not empty
{
Node *currNode = this->pTail;
currNode->pNext = this->pTail = new Node(val,currNode,NULL);
}
else
{
this->pHead = this->pTail = new Node(val);
}
}
//#####################################################################
int main(int argc, const char * argv[])
{
/* Create an empty list */
LinkedList list1;
cout << "Created an empty list named list1." << endl;
/* output the result */
cout << "list1:" << endl;
list1.traverse_and_print();
/* Create a list with only one node */
LinkedList list2(10);
cout << "Created a list named list2 with only one node." << endl;
/* output the result */
cout << "list2:" << endl;
list2.traverse_and_print();
//for testing pushback on a non-empty list
for (int i = 0 ; i < 10; i++){
list2.push_back(i);
}
cout<<"\nPushback f->b traversal List2"<<endl;
list2.traverse_and_print();
cout<<"\nPushback b->f traversal List2"<<endl;
list2.traverse_and_printBackwards();
//for testing pushback on an originally empty list
for (int i = 0 ; i < 10; i++){
list1.push_back(i);
}
cout<<"\nPushback f->b traversal List1"<<endl;
list1.traverse_and_print();
cout<<"\nPushback b->f traversal List1"<<endl;
list1.traverse_and_printBackwards();
return 0;
}