-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edb51ab
commit cb4f2c1
Showing
12 changed files
with
625 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
*.swp | ||
.DS_Store | ||
.vscode | ||
*.dSYM | ||
*.dSYM | ||
bin/* | ||
bin |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
Oops, something went wrong.