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
|
#include <iostream>
#include "linkedList.h"
// Purpose: effectively "empties" the list
// Postconditions: current size set to 0, head and tail ptrs set to NULL,
// and all dynamically allocated memory for nodes deallocated
template <typename T>
void LinkedList<T>::clear()
{
while(m_head != NULL)
removeAtHead();
m_size = 0;
}
// Purpose: determines whether x is in the list
// Parameters: x is data value to be found
// Returns: true if x is in the list otherwise, false
template <typename T>
bool LinkedList<T>::find(const T& x) const
{
Node<T> *thisNode;
thisNode = m_head;
while(thisNode != NULL)
{
if(thisNode -> m_data == x)
return true;
}
return false;
}
// Purpose: puts the data value x at the beginning of the list
// Parameters: x is data value to inserted
// Postconditions: current size of list incremented by 1, head pointer
// now points to a node with data value x, the new node's next ptr points
// to what was previously the first in the list, and its prev ptr points
// to NULL if x is the only value in the list, tail pointer also points
// to the new node
template <typename T>
void LinkedList<T>::insertAtHead(const T& x)
{
m_size++;
Node<T> *newNode = new Node<T>;
newNode -> m_data = x;
newNode -> m_next = m_head;
m_head = newNode;
}
// Purpose: puts the data value x at the end of the list
// Parameters: x is data value to inserted
// Postconditions: current size of list incremented by 1, tail pointer
// now points to a node with data value x, the new node's prev ptr points
// to what was previously the last in the list, and its next ptr points
// to NULL if x is the only value in the list, head pointer also points
// to the new node
template <typename T>
void LinkedList<T>::insertAtTail(const T& x)
{
m_size++;
Node<T> *newNode;
newNode -> m_prev = m_tail;
newNode -> m_data = x;
m_tail = newNode;
}
// Purpose: removes the data value at the beginning of the list
// Postconditions: current size of list decremented by 1, head pointer
// now points to whatever the removed node's next ptr was pointing at, and
// that node's prev ptr is NULL if list is now empty, tail pointer
// is set to NULL
// Exceptions: if the list is empty, then throw std::length_error
template <typename T>
void LinkedList<T>::removeAtHead()
{
if(m_size == 0)
throw std::length_error("List is empty");
m_size--;
m_head = m_head -> m_next;
delete m_head -> m_prev;
if(m_size == 0)
m_tail = NULL;
}
// Purpose: removes the data value at the end of the list
// Postconditions: current size of list decremented by 1, tail pointer
// now points to whatever the removed node's prev ptr was pointing at, and
// that node's next ptr is NULL if list is now empty, head pointer
// is set to NULL
// Exceptions: if the list is empty, then throw std::length_error
template <typename T>
void LinkedList<T>::removeAtTail()
{
if (m_size == 0)
{
throw std::length_error("List is empty");
}
m_size--;
m_tail = m_tail -> m_prev;
delete m_tail -> m_next;
if(m_size == 0)
m_head = NULL;
}
// Purpose: starting from the head of the list, removes the first node
// with data value x
// Parameters: x is data value whose first occurrence is to be removed
// Returns: true if an occurrence of x successfully removed otherwise,
// false
// Postconditions: if x was in the list, current size of list decremented
// by 1, and the (formerly) first occurrence of x has been removed
template <typename T>
bool LinkedList<T>::removeFirstOccurrence(const T& x)
{
Node<T> *newNode = m_head;
while(newNode != NULL)
{
if(newNode -> m_data == x)
{
if(newNode != m_head)
newNode -> m_prev -> m_next = newNode -> m_next;
if(newNode != m_tail)
newNode -> m_next -> m_prev = newNode -> m_prev;
m_size--;
delete newNode;
return true;
}
newNode = newNode -> m_next;
}
return false;
}
// Purpose: removes all nodes with data value x
// Parameters: x is data value to be removed
// Returns: # of occurrences of x that were removed (0 if none)
// Postconditions: all occurrence of x have been removed and current size
// of list updated accordingly
template <typename T>
unsigned int LinkedList<T>::removeAllOccurrences(const T& x)
{
int count;
while(removeFirstOccurrence(x))
count++;
return count;
}
// Purpose: performs a deep copy of the data from rhs into this linked list
// Parameters: rhs is linked list to be copied
// Returns: *this
// Postconditions: this list contains same data values (in the same order)
// as are in rhs any memory previously used by this list has been
// deallocated
template <typename T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& rhs)
{
if(*this == &rhs)
return *this;
clear();
Node<T> *rhsNode;
*rhsNode = rhs.getHeadPtr();
while(rhsNode != NULL)
{
insertAtTail(rhsNode -> m_data);
rhsNode = rhsNode -> m_next;
}
return *this;
}
// Purpose: determines whether this list is identical to rhs list in
// terms of data values and their order in the list
// Parameters: rhs is list to be compared to this list
// Returns: true if lists are identical otherwise, false
template <typename T>
bool LinkedList<T>::operator==(const LinkedList<T>& rhs) const
{
if(m_size != rhs.size())
return false;
Node<T> *rhsNode = rhs.getHeadPtr();
Node<T> *newNode = m_head;
while(newNode != NULL)
{
if(newNode -> m_data != rhsNode -> m_data)
return false;
}
return true;
}
|