Skip to content

Commit d1aaf30

Browse files
committed
Implemented DSListStack
1 parent 21f8122 commit d1aaf30

4 files changed

Lines changed: 249 additions & 79 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ add_executable(${EXE_NAME} ./main.cpp
3535
DSStack/dsstack.h
3636
DSDoublyLL/dsnode.h
3737
DSDoublyLL/dsdoublyll.h
38-
DSListStack/dsliststack.h)
38+
DSDoublyLL/dsliststack.h)

DSDoublyLL/dsliststack.h

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
#ifndef DSLISTSTACK_H
2+
#define DSLISTSTACK_H
3+
4+
#include "dsdoublyll.h"
5+
6+
7+
template <class T>
8+
class DSListStack
9+
{
10+
public:
11+
/**
12+
* @brief DSStack - default constructor
13+
*/
14+
DSListStack();
15+
/**
16+
* @brief DSStack - copy constructor
17+
* @param other - reference to target stack
18+
*/
19+
DSListStack(const DSListStack<T>& other);
20+
21+
/**
22+
* @brief ~DSStack - destructor
23+
*/
24+
~DSListStack();
25+
26+
/**
27+
* @brief Getter - numIndexes
28+
*/
29+
int size();
30+
31+
/**
32+
* @brief push - adds an element to the top of the stack
33+
* @param element - element to add to stack
34+
*/
35+
void push(T element);
36+
37+
/**
38+
* @brief pop - removes the top element of the stack
39+
*/
40+
void pop();
41+
42+
/**
43+
* @brief peek - returns the top element of the stack
44+
* @return top element of the stack
45+
*/
46+
T peek();
47+
48+
/**
49+
* @brief operator = : Sets this stack equal to the passed in stack
50+
* @param other - reference to target stack
51+
* @return reference to this stack
52+
*/
53+
DSListStack<T>& operator=(const DSListStack<T>& other);
54+
55+
/**
56+
* @brief operator + : returns a stack combining this and the given stack
57+
* @param other - reference to given stack
58+
* @return combined stack of this and given stack
59+
*/
60+
DSListStack<T> operator+(const DSListStack<T>& other) const;
61+
62+
/**
63+
* @brief operator += : combines this stack with the given stack
64+
* @param other - reference to the given stack
65+
* @return this stack, after it has been combined with the given
66+
*/
67+
DSListStack<T>& operator+=(const DSListStack<T>& other);
68+
69+
/**
70+
* @brief operator == : compares this stack against another stack
71+
* @param other - reference to target stack
72+
* @return true if the stacks have equal values
73+
*/
74+
bool operator==(const DSListStack<T>& other) const;
75+
76+
private:
77+
DSDoublyLL<T> data;
78+
};
79+
80+
/**
81+
* @brief DSStack - default constructor
82+
*/
83+
template <class T>
84+
DSListStack<T>::DSListStack(){}
85+
/**
86+
* @brief DSStack - copy constructor
87+
* @param other - reference to target stack
88+
*/
89+
template <class T>
90+
DSListStack<T>::DSListStack(const DSListStack<T>& other){
91+
data = other->data;
92+
}
93+
94+
/**
95+
* @brief ~DSStack - destructor
96+
*/
97+
template <class T>
98+
DSListStack<T>::~DSListStack(){}
99+
100+
/**
101+
* @brief Getter - numIndexes
102+
*/
103+
template <class T>
104+
int DSListStack<T>::size(){
105+
return data.size();
106+
}
107+
108+
/**
109+
* @brief push - adds an element to the top of the stack
110+
* @param element - element to add to stack
111+
*/
112+
template <class T>
113+
void DSListStack<T>::push(T element){
114+
data.pushBack(element);
115+
}
116+
117+
/**
118+
* @brief pop - removes the top element of the stack
119+
*/
120+
template <class T>
121+
void DSListStack<T>::pop(){
122+
data.popBack();
123+
}
124+
125+
/**
126+
* @brief peek - returns the top element of the stack
127+
* @return top element of the stack
128+
*/
129+
template <class T>
130+
T DSListStack<T>::peek(){
131+
return data[size() - 1];
132+
}
133+
134+
/**
135+
* @brief operator = : Sets this stack equal to the passed in stack
136+
* @param other - reference to target stack
137+
* @return reference to this stack
138+
*/
139+
template <class T>
140+
DSListStack<T>& DSListStack<T>::operator=(const DSListStack<T>& other){
141+
data = other.data;
142+
143+
return *this;
144+
}
145+
146+
/**
147+
* @brief operator + : returns a stack combining this and the given stack
148+
* @param other - reference to given stack
149+
* @return combined stack of this and given stack
150+
*/
151+
template <class T>
152+
DSListStack<T> DSListStack<T>::operator+(const DSListStack<T>& other) const{
153+
DSListStack<T> result;
154+
result.data = data + other.data;
155+
156+
return result;
157+
}
158+
159+
/**
160+
* @brief operator += : combines this stack with the given stack
161+
* @param other - reference to the given stack
162+
* @return this stack, after it has been combined with the given
163+
*/
164+
template <class T>
165+
DSListStack<T>& DSListStack<T>::operator+=(const DSListStack<T>& other){
166+
data += other.data;
167+
}
168+
169+
170+
/**
171+
* @brief operator == : compares this stack against another stack
172+
* @param other - reference to target stack
173+
* @return true if the stacks have equal values
174+
*/
175+
template <class T>
176+
bool DSListStack<T>::operator==(const DSListStack<T>& other) const{
177+
return data == other.data;
178+
}
179+
180+
#endif // DSLISTSTACK_H

DSListStack/dsliststack.h

Lines changed: 0 additions & 76 deletions
This file was deleted.

test.cpp

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "DSVector/dsvector.h"
99
#include "DSStack/dsstack.h"
1010
#include "DSDoublyLL/dsdoublyll.h"
11+
#include "DSDoublyLL/dsliststack.h"
1112

1213
/*
1314
* DSString
@@ -343,8 +344,6 @@ TEST_CASE("Data_Structures_Stack", "[Stack][Data_Structures_Test]"){
343344

344345
numStack1 = numStack1Copy;
345346
}
346-
347-
348347
}
349348

350349
/*
@@ -465,3 +464,70 @@ TEST_CASE("Data_Structures_Doubly_Linked_List", "[Doubly_Linked_List][Data_Struc
465464
numList3 = numList1 + numList2;
466465
}
467466
}
467+
468+
/*
469+
* DSListStack
470+
*/
471+
TEST_CASE("Data_Structures_List_Stack", "[Stack][Data_Structures_Test]"){
472+
const int intArray1[3] = {0, 1, 2};
473+
int arr1StartSize = 3;
474+
const int intArray2[3] = {3, 4, 5};
475+
int arr2StartSize = 3;
476+
const int intArray3[6] = {0, 1, 2, 3, 4, 5};
477+
int arr3StartSize = 6;
478+
479+
DSListStack<int> numStack1;
480+
DSListStack<int> numStack1Copy;
481+
DSListStack<int> numStack2;
482+
DSListStack<int> numStack3;
483+
484+
for(int element : intArray1){
485+
numStack1.push(element);
486+
numStack1Copy.push(element);
487+
}
488+
489+
for(int element : intArray2){
490+
numStack2.push(element);
491+
}
492+
493+
for(int element : intArray3){
494+
numStack3.push(element);
495+
}
496+
497+
SECTION("Operators"){
498+
499+
REQUIRE(numStack1 == numStack1Copy);
500+
501+
numStack1 = numStack2;
502+
503+
REQUIRE(numStack1 == numStack2);
504+
505+
numStack1 = numStack1Copy;
506+
}
507+
508+
SECTION("Getters"){
509+
REQUIRE(numStack1.size() == 3);
510+
}
511+
512+
SECTION("Peek and Pop"){
513+
REQUIRE(numStack1.peek() == 2);
514+
numStack1.pop();
515+
REQUIRE(numStack1.peek() == 1);
516+
numStack1.pop();
517+
REQUIRE(numStack1.peek() == 0);
518+
519+
numStack1 = numStack1Copy;
520+
}
521+
522+
SECTION("Push"){
523+
numStack1.push(3);
524+
REQUIRE(numStack1.size() == 4);
525+
REQUIRE(numStack1.peek() == 3);
526+
527+
numStack1.push(4);
528+
REQUIRE(numStack1.size() == (5));
529+
REQUIRE(numStack1.peek() == 4);
530+
531+
numStack1 = numStack1Copy;
532+
}
533+
}

0 commit comments

Comments
 (0)