MP3
Linked Lists
List-given.hpp
1 template <class T>
2 List<T>::List() : head_(NULL), tail_(NULL), length_(0) { /* nothing */ }
3 
4 template <class T>
5 int List<T>::size() const {
6  return length_;
7 }
8 
9 template <class T>
10 List<T>::List(List<T> const& other) {
11  _copy(other);
12 }
13 
14 template <class T>
16  if (this != &rhs) {
17  _destory();
18  _copy(rhs);
19  }
20  return *this;
21 }
22 
23 template <class T>
24 bool List<T>::empty() const {
25  return head_ == NULL && tail_ == NULL && length_ == 0;
26 }
27 
28 template <class T>
29 void List<T>::_copy(List<T> const& other) {
30  // set up the default, empty list
31  head_ = NULL;
32  tail_ = NULL;
33  length_ = 0;
34 
35  // if we have things to copy
36  if (!other.empty()) {
37  ListNode* curr = other.head_;
38  ListNode* prev = NULL;
39  // iterate down the parameter list
40  while (curr != NULL) {
41  ListNode* node = new ListNode(curr->data);
42 
43  // set the head of the new list
44  if (head_ == NULL)
45  head_ = node;
46 
47  // correct pointer of the previous node if it exists
48  if (prev != NULL)
49  prev->next = node;
50 
51  node->prev = prev;
52 
53  prev = node;
54  curr = curr->next;
55  }
56 
57  // prev will contain our new tail---set it up accordingly
58  tail_ = prev;
59  tail_->next = NULL;
60 
61  length_ = other.length_;
62  }
63 }
64 
65 template <class T>
66 void List<T>::print(ostream& os) const {
67  os << "<";
68  ListNode* curr = head_;
69  while (curr != NULL) {
70  os << " " << curr->data;
71  curr = curr->next;
72  }
73  os << " >";
74 }
75 
76 // overloaded operator<<
77 template <class T>
78 ostream& operator<<(ostream& os, const List<T>& list) {
79  list.print(os);
80  return os;
81 }
82 
83 // ListNode constructors
84 template <class T>
85 List<T>::ListNode::ListNode() : next(NULL), prev(NULL), data(T())
86 { /* nothing */ }
87 
88 template <class T>
89 List<T>::ListNode::ListNode(const T& ndata) : next(NULL), prev(NULL), data(ndata)
90 { /* nothing */ }
91 
92 template <class T>
93 template <class Iter>
94 List<T>::List(const Iter& begin_iterator, const Iter& end_iterator)
95  : head_(NULL), tail_(NULL), length_(0)
96 {
97  for (Iter i = begin_iterator; i != end_iterator; ++i)
98  insertBack(*i);
99 }
ListNode * tail_
The tail of the list.
Definition: List.h:223
List< T > & operator=(const List< T > &rhs)
Overloaded assignment operator for Lists.
Definition: List-given.hpp:15
void _copy(const List< T > &other)
Copies the given list into the current list.
Definition: List-given.hpp:29
bool empty() const
Determines if the current List is empty.
Definition: List-given.hpp:24
int length_
The length of the current List.
Definition: List.h:228
int size() const
Gets the size of the List.
Definition: List-given.hpp:5
const T data
The data contained in this node.
Definition: List.h:60
List: This is a templated linked list class (meaning it contains data of templated type T...
Definition: List.h:21
The ListNode class is private to the List class via the principle of encapsulation—the end user does...
Definition: List.h:28
void print(ostream &os) const
Used to print the list.
Definition: List-given.hpp:66
ListNode * head_
The head of the List.
Definition: List.h:218
ListNode * next
A pointer to the next node in the list.
Definition: List.h:48
void insertBack(const T &ndata)
Inserts a new node at the back of the List.
Definition: List.hpp:62
ListNode()
Default ListNode constructor.
Definition: List-given.hpp:85
ListNode * prev
A pointer to the previous node in the list.
Definition: List.h:54
List()
Default List constructor.
Definition: List-given.hpp:2