forked from CSUF-CPSC-131-Fall2019/Data-Structures-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackLinkedList.hpp
More file actions
37 lines (30 loc) · 873 Bytes
/
StackLinkedList.hpp
File metadata and controls
37 lines (30 loc) · 873 Bytes
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
#pragma once
#include "SinglyLinkedList.hpp"
#include <stdexcept>
template<typename T>
class StackLinkedList {
private:
SinglyLinkedList<T> list;
public:
StackLinkedList() = default;
void push( const T& x); // Inserts x on top of stack
void pop(); // Removes item at top of stack
T& peek(); // Returns but does not remove item at top of stack
bool empty(); // Returns true if stack has no items
};
template<typename T>
void StackLinkedList<T>::push( const T& newItem ) {
list.prepend(newItem); // Insert as list head (top of stack)
}
template<typename T>
void StackLinkedList<T>::pop() {
list.removeAfter(nullptr); // Remove list head
}
template<typename T>
T& StackLinkedList<T>::peek() {
return list.front();
}
template<typename T>
bool StackLinkedList<T>::empty() {
return list.empty();
}