forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
List.h
89 lines (70 loc) · 3.06 KB
/
List.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
/*
* Filename: List.h
* Description: List class definition
* also includes the prototype for non-member function printList()
*/
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include "ListNode.h"
#include "ListItr.h"
using namespace std;
// When reading in ListItr.h first, it starts reading in this file
// before declaring that ListItr is a class. This file then include
// ListItr.h, but beacuse of the #ifndef LISTITR_H statement, the code
// in that file is not read. Thus, in this case, this List.h file
// will be read in, and will not know that ListItr is a class, which
// will cause compilation problems later on in this file. Got it?
// Isn't C++ fun???
class ListItr;
class List {
public:
// The default constructor.
// It should initialize all private data members
// and set up the basic list structure with the dummy head and tail nodes.
List();
// The copy constructor.
// It should create a **new** list of ListNodes whose contents
// are the same values as the ListNodes in `source`.
List(const List& source);
// The destructor.
// It should empty the list and reclaim the memory allocated in the constructor for head and tail.
~List();
// The copy assignment operator.
// It should copy the contents of every ListNode in `source`
// into an **existing** list (the reference to the calling List object itself).
List& operator=(const List& source);
// Returns true if empty, else false
bool isEmpty() const;
// Removes (deletes) all items except the dummy head/tail.
// The list should be a working empty list after this.
void makeEmpty();
// Returns an iterator that points to the first ListNode **after** the dummy head node (even on an empty list!)
ListItr first();
// Returns an iterator that points to the last ListNode **before** the dummy tail node (even on an empty list!)
ListItr last();
// Inserts x after current iterator position
void insertAfter(int x, ListItr position);
// Inserts x before current iterator position
void insertBefore(int x, ListItr position);
// Inserts x at tail of list
void insertAtTail(int x);
// Returns an iterator that points to the first occurrence of x.
// When the parameter is not in the list, return a ListItr object that points to the dummy tail node.
// This makes sense because you can test the return from find() to see if isPastEnd() is true.
ListItr find(int x);
// Removes the first occurrence of x
void remove(int x);
// Returns the number of elements in the list
int size() const;
private:
ListNode* head; // Dummy node representing the beginning of the list
ListNode* tail; // Dummy node representing the end of the list
int count; // Number of elements in the list
};
// printList: non-member function prototype
// prints list forwards (head -> tail) when forward is true
// or backwards (tail -> head) when forward is false
// You **must** use your ListItr class to implement this function!
void printList(List& source, bool forward);
#endif