Files
  • main.cpp
main.cpp
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <iostream>
using namespace std;

/********************************************
 * Author: Fernando Zamora
 * Class: Node
 * Purpose: Node within the list
 * ******************************************/
 
template <class T>
class Node
{
  
  public:
    
    T Data;
    Node<T>* Next;
    
    Node()
    {
      Next = NULL;
    }
    
    Node(T data)
    {
      Data = data;
      Next = NULL;
    }

};

/************************************************
 * Class: LinkedList 
 * Author: Fernando Zamora
 * Purpose: Implements a linked list but does not
 * expose internal operations of list
 *
 * **********************************************/
 template <class T>
class LinkedList
{
  
  private:
  
  Node<T>* m_head = NULL;
  Node<T>* m_current = NULL;
 
  //Recursive algorithm to delete from DeleteTail
  //it is a brute force approach
  //It can be done better by tracking the tail insted 
  //of walking from beginning to end each time
  //
  //A doubly linked list would be more efficient in keeping
  //track of the tail and then pointing to previous
  //that way it can walk backwards.
  void DeleteTail()
  {
    
    Node<T>* current = m_head;
    Node<T>* previous = current;
    
    if(current == NULL)
    {
      return;
    }
    
    if(current->Next == NULL)
    {
      
      cout << "Deleting: " << current->Data << endl;
      delete current;
      current = NULL;
      m_head = NULL;
      return;
    }
    
    while(current->Next != NULL)
    {
      previous = current;
      current = current->Next;
    }
    
    if(previous != NULL)
    {
      cout << "Deleting: " << current->Data << endl;
      delete current;
      previous->Next = NULL;
    }
    
    DeleteTail();
    
  }
  
  public:
  
    ~LinkedList()
    {
      cout << "Deleting list..." << endl;
      DeleteTail();
    }
    
    //Add data to the list  
    void Add(T data)
    {
      if(m_head == NULL)
      {
        m_head = new Node<T>(data);
      }
      else
      {
        Node<T>* current = m_head;
        while(current->Next != NULL)
        {
          current = current->Next;
        }
        current->Next = new Node<T>(data);
      }
    }
    
    //Gets the head of the list
    //Returns -1 if no head is present
    T Head()
    {
      int data = -1;
      if(m_head != NULL)
        data = m_head->Data;
        
      return data;  
    }

    //Gets an item by its index    
    T Get(int index)
    {
      int currentIndex = 0;
      Node<T> *current = m_head;
      T data;
      
      while(currentIndex != index && current != NULL)
      {
        current = current->Next;
        currentIndex++;
      }
      
      if(current != NULL && currentIndex == index)
      {
        data = current->Data;
      }
      
      if(current == NULL )
      {
        throw -777;
      }
      
      return data;
    }
    
    //Finds data item within list
    //Returns: index of item or -1 if not found
    int Find(T searchFor)
    {
      
      Node<T>* current = m_head;
      int index = 0;
      
      while(current != NULL){
        
        if(current->Data == searchFor)
        {
          return index;
        }
        current = current->Next;
        index++;
      }
      
      return -1; //Did not find it
    }

    //Gets first item in list
    T First()
    {
      m_current = m_head;
      return m_head->Data;
    }

    //For iterating over list    
    T Next()
    {
      T data;
      if(m_current != NULL){
        m_current = m_current->Next;
        return m_current->Data;
      }
      
      if(m_current != NULL){
        return m_current->Data;
      }
      
      return data;
    }
    
    //To determine if there is another item to iterate to
    bool HasNext()
    {
      return (m_current != NULL && m_current->Next != NULL);
    }
}; //End of LinkedList class declaration

void TestWithIntegers(){
    
    LinkedList<int> list;
    
    cout << "Testing with integers... " << endl;
    cout << endl;
    
    list.Add(1);
    list.Add(2);
    list.Add(3);
    
    cout << list.Get(0) << endl;
    cout << list.Get(1) << endl;
    cout << list.Get(2) << endl;
    
    cout << endl;
    cout << "Iterator: " << endl;
    cout <<   "First:  " << list.First() << endl;
    
    while(list.HasNext())
    {
      cout << "Next:   " << list.Next() << endl;
    }
    
    cout << "Should find 0 at -1: result: " << list.Find(0) << endl;
    cout << "Should find 1 at 0:  result: " << list.Find(1) << endl;
    cout << "Should find 2 at 1:  result: " << list.Find(2) << endl;
    cout << "Should find 3 at 2:  result: " << list.Find(3) << endl;
    cout << endl;
    cout << endl;
}


void TestWithStrings(){
    
    LinkedList<string> list;
    
    cout << endl;
    cout << "Testing with strings... " << endl;
    cout << endl;
    
    list.Add("aa");
    list.Add("bb");
    list.Add("cc");
    
    cout << list.Get(0) << endl;
    cout << list.Get(1) << endl;
    cout << list.Get(2) << endl;
    
    cout << endl;
    cout << "Iterator: " << endl;
    cout <<   "First:  " << list.First() << endl;
    
    while(list.HasNext())
    {
      cout << "Next:   " << list.Next() << endl;
    }

    cout << "Should find zz at -1: result: " << list.Find("zz") << endl;
    cout << "Should find aa at 0: result: " << list.Find("aa") << endl;
    cout << "Should find bb at 1: result: " << list.Find("bb") << endl;
    cout << "Should find cc at 2: result: " << list.Find("cc") << endl;
    cout << endl;
    cout << endl;
}

int main() 
{
  TestWithIntegers();
  TestWithStrings();
}
gcc version 4.6.3