-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.cpp
More file actions
144 lines (120 loc) · 2.5 KB
/
container.cpp
File metadata and controls
144 lines (120 loc) · 2.5 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
#include "container.h"
#include <stddef.h>
#include <iostream>
template<class type>
Container<type>::Container(){
first = last = NULL;
count = 0;
}
template<class type>
Container<type>::Container(const type &new_element){
Cell<type> *newCell = new Cell<type>(new_element);
first = last = newCell;
count = 1;
}
template<class type>
Container<type>::~Container(){
count = 0;
Cell<type> *temp;
while(first != NULL){
temp = first;
first = first->getNext();
delete temp;
}
first = last = NULL;
}
template<class type>
void Container<type>::Enqueue(const type &new_element){
count++;
Cell<type> *newCell = new Cell<type>(new_element);
if(first != NULL){
newCell->setPrev(last);
last->setNext(newCell);
last = newCell;
}else{
first = last = newCell;
}
}
template<class type>
type Container<type>::Dequeue(){
count--;
if(first != NULL){
Cell<type> *second = first->getNext();
Cell<type> *firstCell = first;
type content = first->getContent();
second->setPrev(NULL);
first = second;
delete firstCell;
return content;
}else{
return 0;
}
}
template<class type>
void Container<type>::Push(const type &new_element){
count++;
Cell<type> *newCell = new Cell<type>(new_element);
if(first != NULL){
newCell->setNext(first);
first->setPrev(newCell);
first = newCell;
}else{
first = last = newCell;
}
}
template<class type>
type Container<type>::Pop(){
Dequeue();
}
template<class type>
void Container<type>::Clear(){
count = 0;
std::cout << " clearing";
Cell<type> *temp;
while(first != NULL){
temp = first;
first = first->getNext();
delete temp;
}
first = last = NULL;
}
template<class type>
int Container<type>::Size()const{ return count; }
template<class type>
bool Container<type>::IsEmpty()const{
if(first == NULL) return true;
else return false;
}
template <class type>
void Container<type>::Print(){
if(first == NULL){
std::cout<< "Empty list";
return;
}
Cell<type> *temp = first;
while(temp != NULL){
std::cout << temp->getContent() << " ";
temp = temp->getNext();
}
}
template <class type>
bool Container<type>::operator==(const Container &a){
Cell<type> *b = a.first;
while(first != NULL && a.first != NULL){
if(first->getContent() == b->getContent()){
first = first->getNext();
b = b->getNext();
}else{
return false;
}
}
return true;
}
template<class type>
type &Container<type>::operator[](const int n){
Cell<type> *temp = first;
for(int i = 0; i < n; i++){
temp = temp->getNext();
}
return temp->getContent();
}