-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.cpp
More file actions
86 lines (77 loc) · 2.95 KB
/
list.cpp
File metadata and controls
86 lines (77 loc) · 2.95 KB
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
/* Aaron Parks
* Prof. Carol Zander
* CSS 343 : Winter 2010
* Lab 4v4 - MOVIE Store */
#include "list.h"
//------------------------------------------------------------------------------
// Constructor
// - Initializes head to NULL.
//------------------------------------------------------------------------------
List::List() { head = NULL; }
//------------------------------------------------------------------------------
// insert
// - Will create a new ListNode object and assign the data point to the
// parameter Transaction object. Will insert new ListNode at the front of
// the list and reassign it as the head of the list.
//------------------------------------------------------------------------------
bool List::insert(Transaction* toBeInserted)
{
ListNode* newNode = new ListNode;
newNode->next = NULL;
newNode->data = toBeInserted;
//patch newNode in as new head
if (isEmpty())
head = newNode;
else
{
newNode->next = head;
head = newNode;
}
return true;
}
//------------------------------------------------------------------------------
// isEmpty
// - Return true if head is NULL (thus there is no list) or returns false if
// root is not NULL (thus there is at least one node in list).
//------------------------------------------------------------------------------
bool List::isEmpty() const { return head == NULL; }
//------------------------------------------------------------------------------
// makeEmpty
// - Will delete all data in a List and deleting the 'Trans' data object and
// and the ListNode of the previous pointer. The current pointer points to
// the ListNode following the ListNode being pointer to by current int order.
//------------------------------------------------------------------------------
void List::makeEmpty()
{
if (!isEmpty())
{
ListNode* previous = head;
ListNode* current = head;
while (current != NULL)
{
current = previous->next;
delete previous->data;
delete previous;
previous = current;
}
}
}
//------------------------------------------------------------------------------
// displayHistory & displayHistory (overloaded)
// - Will traverse history linked list and display information about the
// 'Transaction' object pointed to by the ListNode pointer. Information will
// displayed when stack frames resolve, meaning entire list will be traversed
// then information will be display in reverse order, last node first.
//------------------------------------------------------------------------------
void List::displayHistory() const
{
if (!isEmpty())
displayHistory(head);
}
/******************************************************************************/
void List::displayHistory(ListNode* current) const
{
if (current->next != NULL)
displayHistory(current->next);
current->data->display();
}