-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedStack.h
125 lines (101 loc) · 2.96 KB
/
LinkedStack.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
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
#pragma once
#include "StackInterface.h"
#include "Node.h"
#include <cassert> // For assert
template<class ItemType>
class LinkedStack : public StackInterface<ItemType>
{
private:
Node<ItemType>* topPtr; // Pointer to first node in the chain;
// this node contains the stack's top
public:
// Constructors and destructor:
LinkedStack(); // Default constructor
LinkedStack(const LinkedStack<ItemType>& aStack);// Copy constructor
virtual ~LinkedStack(); // Destructor
// Stack operations:
bool isEmpty() const;
bool push(const ItemType& newItem);
bool pop();
ItemType peek() const;
}; // end LinkedStack
template<class ItemType>
LinkedStack<ItemType>::LinkedStack() : topPtr(nullptr)
{
} // end default constructor
template<class ItemType>
LinkedStack<ItemType>::LinkedStack(const LinkedStack<ItemType>& aStack)
{
// Point to nodes in original chain
Node<ItemType>* origChainPtr = aStack->topPtr;
if (origChainPtr == nullptr)
topPtr = nullptr; // Original bag is empty
else
{
// Copy first node
topPtr = new Node<ItemType>();
topPtr->setItem(origChainPtr->getItem());
// Point to last node in new chain
Node<ItemType>* newChainPtr = topPtr;
// Copy remaining nodes
while (origChainPtr != nullptr)
{
// Advance original-chain pointer
origChainPtr = origChainPtr->getNext();
// Get next item from original chain
ItemType nextItem = origChainPtr->getItem();
// Create a new node containing the next item
Node<ItemType>* newNodePtr = new Node<ItemType>(nextItem);
// Link new node to end of new chain
newChainPtr->setNext(newNodePtr);
// Advance pointer to new last node
newChainPtr = newChainPtr->getNext();
} // end while
newChainPtr->setNext(nullptr); // Flag end of chain
} // end if
} // end copy constructor
template<class ItemType>
LinkedStack<ItemType>::~LinkedStack()
{
// Pop until stack is empty
while (!isEmpty())
pop();
} // end destructor
template<class ItemType>
bool LinkedStack<ItemType>::isEmpty() const
{
return topPtr == nullptr;
} // end isEmpty
template<class ItemType>
bool LinkedStack<ItemType>::push(const ItemType& newItem)
{
Node<ItemType>* newNodePtr = new Node<ItemType>(newItem, topPtr);
topPtr = newNodePtr;
newNodePtr = nullptr;
return true;
} // end push
template<class ItemType>
bool LinkedStack<ItemType>::pop()
{
bool result = false;
if (!isEmpty())
{
// Stack is not empty; delete top
Node<ItemType>* nodeToDeletePtr = topPtr;
topPtr = topPtr->getNext();
// Return deleted node to system
nodeToDeletePtr->setNext(nullptr);
delete nodeToDeletePtr;
nodeToDeletePtr = nullptr;
result = true;
} // end if
return result;
} // end pop
template<class ItemType>
ItemType LinkedStack<ItemType>::peek() const
{
assert(!isEmpty()); // Enforce precondition
// Stack is not empty; return top
return topPtr->getItem();
} // end getTop
// End of implementation file.