Skip to content

Commit

Permalink
PA3 completely finished
Browse files Browse the repository at this point in the history
  • Loading branch information
nate-browne committed Jul 7, 2019
1 parent edb51ab commit cb4f2c1
Show file tree
Hide file tree
Showing 12 changed files with 625 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.swp
.DS_Store
.vscode
*.dSYM
*.dSYM
bin/*
bin
Empty file removed PAs/PA3/Makefile
Empty file.
65 changes: 65 additions & 0 deletions PAs/PA3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# HW3: Stacks and Queues
## Due: TBD

### Overview
In this PA, you'll be implementing a stack and a queue ADT as discussed in class.
This PA will use the object-oriented principle of [*composition*](https://en.wikipedia.org/wiki/Object_composition) to create these two classes.

You will need to copy over your `vector.hpp` from PA2 for use in both `queue.hpp` and `stack.hpp`, but once you've done that, this PA should be pretty close to done.

### *Note: If you use the `queue`, `vector`, or `stack` header files from the STL, you **will** get 0 points!*

Your implementations should go in `queue.hpp` and `stack.hpp`, under the `cse12_ds` namespace (this has been set up for you).

#### Compilation and Testing
To compile, use the provided Makefile. If you just want to build the program, run `make build`. If you want to build and run, use `make prog`.

Like before, you are responsible for testing your program fully. We provide a driver for you to use, but you still have to verify correctness to ensure a good grade.

#### Turnin
*Instructions coming soon*

### The Code
You will be re-implementing the [queue](http://www.cplusplus.com/reference/queue/queue/?kw=queue) and [stack](http://www.cplusplus.com/reference/stack/stack/?kw=stack) classes from the STL, with nearly the same functionality. You do not have to support all of the constructors or the `swap` functions, though.

#### The Functions, Described
*Do note that you don't have to implement them in this order*

* Stack
1. `stack(void)`
* This is a constructor for the stack.
2. `~stack(void)`
* This is a destructor for the stack.
3. `bool empty(void)`
* Returns `true` if the stack is empty, `false` otherwise.
4. `size_t size(void)`
* Returns the number of elements in the stack.
5. `T & top(void)`
* Returns the element at the top of the stack.
6. `void push(const T & item)`
* Adds a new element to the top of the stack.
7. `void pop(void)`
* Removes (but does not return) the element at the top of the stack.
* Queue
1. `queue(void)`
* This is a constructor for the queue.
2. `~queue(void)`
* This is a destructor for the queue.
3. `bool empty(void)`
* Returns `true` if the queue is empty, `false` otherwise.
4. `size_t size(void)`
* Returns the number of elements in the queue.
5. `T & front(void)`
* Returns the element at the front of the queue.
6. `T & back(void)`
* Returns the element at the back of the queue.
7. `void push(const T & item)`
* Adds a new element to the end of the queue.
8. `void pop(void)`
* Removes (but does not return) the element at the front of the queue.
#### Implementation Tips
1. If your vector class is correct, you should hardly have any code to write here.
2. Remember the differences between LIFO and FIFO, and how those acronyms relate to stacks and queues.
3. It is possible to implement the queue and stack such that their constructor and destructor classes don't do anything. How does this relate to memory management, and how might you achieve this?

# Good luck! Start Early, Test Often, and Finish Early!
145 changes: 145 additions & 0 deletions PAs/PA3/src/driver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#include <cstdio>
#include <cstdlib>

#include <string>
#include <iostream>
#include <exception>

#include "queue.hpp"
#include "stack.hpp"

#define QUEUE_STR "Please enter a command: (e(m)pty, (s)ize, (f)ront, (b)ack, (p)ush, p(o)p): ";
#define STACK_STR "Please enter a command: (e(m)pty, (s)ize, (t)op, (p)ush, p(o)p): ";

void clrbuf(char c) {
while(c != '\n') c = fgetc(stdin);
}

int main(void) {

char dec;
cse12_ds::queue<std::string> *queue = nullptr;
cse12_ds::stack<std::string> *stack = nullptr;
std::cout << "(q)ueue or (s)tack? ";
dec = fgetc(stdin);
clrbuf(dec);

switch(dec) {
case 'q':
queue = new cse12_ds::queue<std::string>();
break;
case 's':
stack = new cse12_ds::stack<std::string>();
break;
default:
std::cerr << "Invalid option: " << dec << "." << std::endl;
exit(1);
}

while(true) {
char command = '\0';
if(queue) {
std::cout << QUEUE_STR;
} else {
std::cout << STACK_STR;
}

if((command = fgetc(stdin)) == EOF) {
break;
}
clrbuf(command);

switch(command) {
case 'm': {
if(queue) {
if(queue->empty()) {
std::cout << "Queue is empty" << std::endl;
} else {
std::cout << "Queue is not empty" << std::endl;
}
} else {
if(stack->empty()) {
std::cout << "Stack is empty" << std::endl;
} else {
std::cout << "Stack is not empty" << std::endl;
}
}
break;
}
case 's': {
if(queue) {
std::cout << "Number of elements in queue is: " << queue->size() << std::endl;
} else {
std::cout << "Number of elements in stack is: " << stack->size() << std::endl;
}
break;
}
case 'f': {
if(queue) {
if(queue->empty())
std::cout << "No element at front" << std::endl;
else
std::cout << "Element at front is " << queue->front() << std::endl;
}
break;
}
case 'b': {
if(queue) {
if(queue->empty())
std::cout << "No element at back" << std::endl;
else
std::cout << "Element at back is " << queue->back() << std::endl;
}
break;
}
case 'p': {
char buffer[BUFSIZ], *nl;
std::cout << "Please enter string to insert: ";
fgets(buffer, BUFSIZ, stdin);
nl = strchr(buffer, '\n');
if(nl) *nl = '\0';
std::string elem(buffer);
if(queue) {
queue->push(buffer);
} else {
stack->push(buffer);
}
break;
}
case 'o':
if(queue) {
if(queue->empty())
std::cout << "No element to remove." << std::endl;
else {
std::string item = queue->front();
queue->pop();
std::cout << "Element removed is " << item << std::endl;
}
} else {
if(stack->empty())
std::cout << "No element to remove." << std::endl;
else {
std::string item = stack->top();
stack->pop();
std::cout << "Element popped is " << item << std::endl;
}
}
break;
case 't': {
if(stack) {
if(stack->empty())
std::cout << "No item to top." << std::endl;
else {
std::string item = stack->top();
std::cout << "Item topped is " << item << std::endl;
}
}
break;
}
}
}

delete queue;
delete stack;
return EXIT_SUCCESS;
}
14 changes: 14 additions & 0 deletions PAs/PA3/src/queue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef QUEUE_HPP
#define QUEUE_HPP

#include "vector.hpp"

namespace cse12_ds {
template <class T>
class queue {
private:
public:
};
}

#endif
14 changes: 14 additions & 0 deletions PAs/PA3/src/stack.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef STACK_HPP
#define STACK_HPP

#include "vector.hpp"

namespace cse12_ds {
template <class T>
class stack {
private:
public:
};
}

#endif
3 changes: 2 additions & 1 deletion Solutions/PA2/src/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <exception>

#define DEFAULT_SIZE 10
#define ERR_STR "Accessing element out of range of vector"

namespace cse12_ds {

Expand Down Expand Up @@ -45,7 +46,7 @@ namespace cse12_ds {
}

T & at(unsigned int pos) {
if(pos < 0 || pos >= this->size()) throw std::out_of_range("Accessing element out of range of vector");
if(pos < 0 || pos >= this->size()) throw std::out_of_range(ERR_STR);
return this->arr[pos];
}

Expand Down
32 changes: 32 additions & 0 deletions Solutions/PA3/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CXXFLAGS=
LDFLAGS=
LINKFLAGS=-std=c++11 -Wall -pedantic -Wextra

ifeq ($(type),opt)
CXXFLAGS=-O3 -Wall -pedantic -Wextra -std=c++11 -c
LDFLAGS=
else
CXXFLAGS=-Wall -pedantic -Wextra -std=c++11 -c
LDFLAGS=-g
endif

new:
make clean
make prog

prog: build
bin/driver

build: clean setup driver.o
g++ $(LINKFLAGS) -o driver $(LDFLAGS) bin/driver.o
mv driver bin/

driver.o: src/driver.cpp src/queue.hpp src/stack.hpp src/vector.hpp
g++ $(CXXFLAGS) $(LDFLAGS) src/driver.cpp
mv *.o bin/

setup:
mkdir bin

clean:
rm -rf bin/
Loading

0 comments on commit cb4f2c1

Please sign in to comment.