-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedListAssignmentCpp.cpp
More file actions
126 lines (101 loc) · 2.66 KB
/
Copy pathDoublyLinkedListAssignmentCpp.cpp
File metadata and controls
126 lines (101 loc) · 2.66 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
//doubly linked list
#include <iostream>
using namespace std;
/**
* Creates a structure for the elements of the list (information and pointers to previous and next element)
*/
struct DListElem {
int info;
DListElem* next;
DListElem* prev;
};
/**
* Creates a structure for the pointers to the first and last element of the list
*/
struct DList {
DListElem* first;
DListElem* last;
};
/**
* Initializes the list
* @param dl The default name of the list
*/
void initializeDList (DList & dl) {
dl.first = nullptr;
dl.last = nullptr;
}
/**
* Checks if the list is empty
* @param dl The default name of the list
* @return returns if the list is empty or not
*/
bool isEmpty(DList dl){
if (dl.first == nullptr) return true;
return false;
}
/**
* Adds an element into the beginning of the list.
* @param dl The default name of the list
* @param val The value of the info of the new element
*/
void put (DList& dl, int val) {
DListElem* temp = new DListElem;
temp->info = val;
if (isEmpty(dl)){
dl.first = temp;
dl.last = temp;
}
else {
dl.first->prev = temp;
dl.first->prev->next = dl.first;
dl.first = temp;
}
}
/**
* Removes the last element in the list
* @param dl The default name of the list
* @param val The reference to the value of the element being removed
* @return Returns if it was possible to remove the last item (If the list was empty already or not)
*/
bool get (DList& dl, int &val){
if (isEmpty(dl)) return false;
if (dl.first == dl.last){val = dl.last-> info; delete dl.last; return true;}
DListElem* temporary = new DListElem;
dl.last->prev->next = nullptr;
temporary = dl.last->prev;
val = dl.last->info;
delete dl.last;
dl.last = temporary;
return true;
}
int main(int argc, char* argv[])
{
DList queue;
initializeDList(queue);
//insert 5 values
for (int i = 1; i <= 5; i++) {
cout << "put: " << 10 * i << endl;
put(queue, 10 * i);
}
//remove 3 values and print them to console
for (int j = 1; j <= 3; j++) {
int value;
if (get(queue, value))
cout << " get: " << value << endl;
}
// cout << "Laenge: " << dlistLength(queue) << endl;
//insert 5 values
for (int i = 6; i <= 10; i++) {
cout << "put: " << 10 * i << endl;
put(queue, 10 * i);
}
// cout << "Laenge: " << dlistLength(queue) << endl;
//remove all values and print them
while(!isEmpty(queue)) {
int value;
get(queue, value);
cout << " get: " << value << endl;
}
cin.sync(); cin.get();
return 0;
}