-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLista_Enlazada.h
166 lines (162 loc) · 4.53 KB
/
Lista_Enlazada.h
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
#include "Iterador.h"
template<class S>
class linkedListType
{
public:
const linkedListType<S>& operator=(const linkedListType<S>&);
void initializelist();
bool isemptylist()const;
void print() const;
int length() const;
void destroyList();
S front() const;
S back() const;
virtual bool search(const S& searchItem) const = 0;
virtual void insertFirst(const S& newItem) = 0;
virtual void insertLast(const S& newItem) = 0;
virtual void deleteNode(const S& deleteItem) = 0;
linkedlistiterator<S> begin();
linkedlistiterator<S> end();
linkedListType();
linkedListType(const linkedListType<S>& otherList);
~linkedListType();
protected:
int count;
nodeType<S> *first;
nodeType<S> *last;
private:
void copyList(const linkedListType<S>& otherList);
};
template<class V>
bool linkedListType<V>::isemptylist()const
{
return (first == NULL);
}
template <class Y>
linkedListType<Y>::linkedListType() //default constructor
{
first = NULL;
last = NULL;
count = 0;
}
template <class H>
void linkedListType<H>::destroyList()
{
nodeType<H> *temp; //pointer to deallocate the memory
//occupied by the node
while (first != NULL) //while there are nodes in the list
{
temp = first; //set temp to the current node
first = first->link; //advance first to the next node
delete temp; //deallocate the memory occupied by temp
}
last = NULL; //initialize last to NULL; first has already
//been set to NULL by the while loop
count = 0;
}
template <class R>
void linkedListType<R>::initializelist()
{
destroyList(); //if the list has any nodes, delete them
}
template <class F>
void linkedListType<F>::print() const
{
nodeType<F> *current; //pointer to traverse the list
current = first; //set current point to the first node
while (current != NULL) //while more data to print
{
cout << current->info << " ";
current = current->link;
}
}//end print
template <class L>
int linkedListType<L>::length() const
{
return count;
}
template <class F>
F linkedListType<F>::front() const
{
assert(first != NULL);
return first->link; //return the info of the first node
}//end front
template <class W>
W linkedListType<W>::back() const
{
assert(last != NULL);
return last->link; //return the info of the last node
}//end back
template <class B>
linkedlistiterator<B> linkedListType<B>::begin()
{
linkedlistiterator<B> temp(first);
return temp;
}
template <class E>
linkedlistiterator<E> linkedListType<E>::end()
{
linkedlistiterator<E> temp(NULL);
return temp;
}
template <class C>
void linkedListType<C>::copyList
(const linkedListType<C>& otherList)
{
nodeType<C> *newNode; //pointer to create a node
nodeType<C> *current; //pointer to traverse the list
if (first != NULL) //if the list is nonempty, make it empty
destroyList();
if (otherList.first == NULL) //otherList is empty
{
first = NULL;
last = NULL;
count = 0;
}
else
{
current = otherList.first; //current points to the
//list to be copied
count = otherList.count;
//copy the first node
first = new nodeType<C>; //create the node
first->info = current->getdato(); //copy the info
first->link = NULL; //set the link field of the node to NULL
last = first; //make last point to the first node
current = current->link; //make current point to the next
// node
//copy the remaining list
while (current != NULL)
{
newNode = new nodeType<C>; //create a node
newNode->info = current->info; //copy the info
newNode->link = NULL;
//set the link of newNode to NULL
last->link = newNode; //attach newNode after last
last = newNode; //make last point to the actual last
//node
current = current->link; //make current point to the
//next node
}//end while
}//end else
}//end copyList
template <class D>
linkedListType<D>::~linkedListType() //destructor
{
destroyList();
}
template <class Z>
linkedListType<Z>::linkedListType(const linkedListType<Z>& otherList)
{
first = NULL;
copyList(otherList);
}//end copy constructor
template <class I>
const linkedListType<I>& linkedListType<I>::operator=(const linkedListType<I>& otherList)
{
if (this != &otherList) //avoid self-copy
{
copyList(otherList);
}//end else
return *this;
}