-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.cpp
113 lines (100 loc) · 1.94 KB
/
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
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
#include <iostream>
#include <string>
#include "list.h"
struct List::Node{
const Post& post;
Node * next;
Node(const Post &post, Node * next = NULL)
: post(post), next(next)
{
#if (OBJ_LIST_MESSAGES)
cout << "List::Node[" << this << "]: Node with post: " << post.getId() << " have been created!" << endl;
#endif
}
Node(const Node& node, Node * next = NULL)
: post(node.post), next(next)
{
#if (OBJ_LIST_MESSAGES)
cout << "List::Node[" << this << "]: Node with post: " << post.getId() << " have been created by copying!" << endl;
#endif
}
};
List::List()
: start(NULL), size(0)
{
#if (OBJ_LIST_MESSAGES)
cout << "I create a List object" << endl;
#endif
}
List::List(const List& list)
:start(NULL), size(0)
{
append(list);
#if (OBJ_LIST_MESSAGES)
cout << "I create a List object by copying" << endl;
#endif
}
const unsigned int List::get_size(){
return size;
}
void List::pushFront(Node * node){
Node * n = new Node(*node, start);
start = n;
size++;
}
void List::pushFront(const Post& post){
Node * node = new Node(post, start);
start = node;
size++;
}
const bool List::pop(const unsigned int index){
if (index >= size) return false;
if (index == 0){
Node * temp = start;
start = start->next;
delete temp;
}
else{
Node * temp = start;
Node * temp2 = NULL;
for (unsigned int s = 0; s <= index - 1; s++){
temp = temp->next;
temp2 = temp->next;
if (temp->next != NULL)
temp->next = temp->next->next;
else
temp->next = NULL;
}
delete temp2;
}
size--;
return true;
}
void List::append(const List& list){
Node * n = list.start;
while (n != NULL){
pushFront(n);
n = n->next;
}
}
void List::print(){
Node * n = start;
for (unsigned int i = 0; i < size; i++){
if (n != NULL){
n->post.print();
n = n->next;
}
}
}
List::~List(){
Node * t1, *t2;
t1 = start;
while (t1 != NULL){
t2 = t1;
t1 = t1->next;
delete t2;
}
#if (OBJ_LIST_MESSAGES)
cout << "I destroy a List object" << endl;
#endif
}