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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
#ifndef H_circularLinkedList
#define H_circularLinkedList
#include <iostream>
#include <cassert>
using namespace std;
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template <class Type>
class circularLinkedList
{
public:
const circularLinkedList<Type>& operator=
(const circularLinkedList<Type>&);
//Overloads the assignment operator.
void initializeList();
//Initializes the list to an empty state.
//Postcondition: first = nullptr, last = nullptr,
// count = 0
bool isEmptyList();
//Function to determine whether the list is empty.
//Postcondition: Returns true if the list is empty;
// otherwise, returns false.
void print() const;
int length();
//Function to return the number of nodes in the
//list.
//Postcondition: The value of count is returned.
void destroyList();
//Function to delete all the nodes from the list.
//Postcondition: first = nullptr, last = nullptr,
// count = 0
Type front();
//Function to return the first element of the list.
//Precondition: The list must exist and must not be
//empty.
//Postcondition: If the list is empty, then the
// program terminates; otherwise,
// the first element of the list is
// returned.
Type back();
//Function to return the last element of the
//list.
//Precondition: The list must exist and must not
//be empty.
//Postcondition: If the list is empty, then the
// program terminates; otherwise,
// the last element of the list is
// returned.
bool search(const Type& searchItem);
//Function to determine whether searchItem is in
//the list.
//Postcondition: Returns true if searchItem is found
// in the list; otherwise, it returns
// false.
void insertNode(const Type& newitem);
void deleteNode(const Type& deleteItem);
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing
// deleteItem is deleted from the
// list, first points to the first
// node, and last points to the last
// node of the updated list.
circularLinkedList();
//Default constructor
//Initializes the list to an empty state.
//Postcondition: first = nullptr, last = nullptr,
// count = 0
circularLinkedList(const circularLinkedList<Type>& otherList);
//Copy constructor
~circularLinkedList();
//Destructor
//Deletes all the nodes from the list.
//Postcondition: The list object is destroyed.
protected:
int count; //variable to store the number of
//elements in the list
nodeType<Type> *first; //pointer to the first node of
//the list
nodeType<Type> *last; //pointer to the last node of
//the list
private:
void copyList(const circularLinkedList<Type>& otherList);
//Function to make a copy of otherList.
//Postcondition: A copy of otherList is created
// and assigned to this list.
};
template <class Type>
bool circularLinkedList<Type>::isEmptyList()
{
if(first == nullptr and last == nullptr and this->count == 0)
{
return true;
}
else return false;
}
template <class Type>
circularLinkedList<Type>::circularLinkedList() // default constructor
{
first = nullptr;
last = nullptr;
count = 0;
}
template <class Type>
void circularLinkedList<Type>::destroyList()
{
this->first = nullptr;
this->first->info = NULL;
}
template <class Type>
void circularLinkedList<Type>::initializeList()
{
destroyList(); //if the list has any nodes, delete them
}
template <class Type>
int circularLinkedList<Type>::length()
{
nodeType<Type> *current;
current->link = this->first;
current->info = this->first->info;
while(current->link != nullptr)
{
++count;
current->link = current->link->link;
current->info = current->link->info;
}
return count;
// function to find the length of the list
} // end length
template <class Type>
Type circularLinkedList<Type>::front()
{
assert(first != nullptr);
return first->link->info; //return the info of the first node
}//end front
template <class Type>
Type circularLinkedList<Type>::back()
{
assert(first != nullptr);
return first->info; //return the info of the first node
}//end back
template <class Type>
bool circularLinkedList<Type>::search(const Type& searchItem)
{
nodeType<Type> *current;
current->link = first->link;
current->info = first->info;
//overload = operator so info == info.
//current needs to point to the first node, then the next, then the next, until it reaches the end.
//current is redefined as current-> link, then evaluates current->link's info.
for(int i = 0; i < count; i++)
{
if(current->info == searchItem)
{
return true;
}
if(current->link != nullptr)
{
current->link = current->link->link;
current->info = current->link->info;
}
else return false;
}
// function to search the list for a given item
}//end search
template <class Type>
void circularLinkedList<Type>::insertNode(const Type& newitem)
{
// function to insert an item into the list
}//end insertNode
template <class Type>
void circularLinkedList<Type>::deleteNode(const Type& deleteItem)
{
// function to delete an item from the list
} //end deleteNode
//Overloading the stream insertion operator
template <class Type>
void circularLinkedList<Type>::print() const
{
for(int i = 0; i < this->count; i++)
{
nodeType<Type> *current;
current->link = this->first;
current->info = this->first->info;
cout << current->info;
if(current->link != nullptr)
{
current->link = current->link->link;
current->info = current->link->link->info;
}
}
}
template <class Type>
circularLinkedList<Type>::~circularLinkedList() // destructor
{
destroyList();
}//end destructor
template <class Type>
void circularLinkedList<Type>::copyList
(const circularLinkedList<Type>& otherList)
{
// function to copy the list
}//end copyList
//copy constructor
template<class Type>
circularLinkedList<Type>::circularLinkedList
(const circularLinkedList<Type>& otherList)
{
first = nullptr;
copyList(otherList);
}//end copy constructor
//overload the assignment operator
template <class Type>
const circularLinkedList<Type>& circularLinkedList<Type>::operator=
(const circularLinkedList<Type>& otherList)
{
if (this != &otherList) //avoid self-copy
{
copyList(otherList);
}//end else
return *this;
}
#endif
|