adding cs235
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
#include "Factory.h"
|
||||
#include "station.h"
|
||||
//You may add #include statments here
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
Unlike all other documents provided, you may modify this document slightly (but do not change its name)
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
createStation()
|
||||
|
||||
Creates and returns an object whose class extends StationInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "Station", you might say, "return new Station();".
|
||||
*/
|
||||
StationInterface* Factory::createStation()
|
||||
{
|
||||
return new station();
|
||||
}
|
||||
//=======================================================================================
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "StationInterface.h"
|
||||
#pragma once
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
createStation()
|
||||
|
||||
Creates and returns an object whose class extends StationInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "Station", you might say, "return new Station();".
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
static StationInterface* createStation();
|
||||
};
|
||||
|
||||
//=======================================================================================
|
||||
@@ -0,0 +1,35 @@
|
||||
CXXFLAGS= -Wall -g -std=c++0x
|
||||
OBJECTS=Factory.o station.o deque.o ordeque.o irdeque.o queue.o linkedlist.o node.o stack.o pwnd.o ignoreme.a
|
||||
EXE=main
|
||||
all: pwnd.o $(EXE)
|
||||
|
||||
$(EXE): $(OBJECTS)
|
||||
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
|
||||
main.o: main.cpp Factory.o ordeque.o irdeque.o deque.o queue.o station.o linkedlist.o node.o stack.o pwnd.o ignoreme.a
|
||||
Factory.o: Factory.cpp Factory.h
|
||||
station.o: station.cpp station.h
|
||||
linkedlist.o: linkedlist.cpp linkedlist.h
|
||||
stack.o: stack.cpp stack.h
|
||||
queue.o: queue.cpp queue.h
|
||||
deque.o: deque.cpp deque.h
|
||||
irdeque.o: irdeque.cpp irdeque.h
|
||||
ordeque.o: ordeque.cpp ordeque.h
|
||||
node.o: node.cpp node.h
|
||||
pwnd.o: pwnd.c
|
||||
|
||||
run: main
|
||||
./main
|
||||
|
||||
clean:
|
||||
@rm -vf *.o
|
||||
@rm -vf $(EXE)
|
||||
@rm -vf *.1
|
||||
@rm -vf *.0
|
||||
@rm -vf test
|
||||
@rm -rvf *.dSYM
|
||||
|
||||
drun: main
|
||||
gdb ./main
|
||||
|
||||
valgrind: $(EXE)
|
||||
valgrind --tool=memcheck --leak-check=yes ./$(EXE)
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
EXE=dont_run_me
|
||||
|
||||
g++ -o$EXE Student_Code/*.cpp ignoreme.a
|
||||
if (( $? )) ;
|
||||
then
|
||||
echo Compilation Failed;
|
||||
read -p "Press enter to exit";
|
||||
else
|
||||
chmod 755 $EXE;
|
||||
./$EXE;
|
||||
rm ./$EXE
|
||||
read -p "Press any key to exit..."
|
||||
fi;
|
||||
@@ -0,0 +1,160 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
class StationInterface
|
||||
{
|
||||
public:
|
||||
StationInterface(){}
|
||||
virtual ~StationInterface(){}
|
||||
|
||||
//Part 1--------------------------------------------------------------
|
||||
/**
|
||||
* Let another car arrive at the station and become the current car.
|
||||
* Do not allow a new car to arrive if any of the following conditions apply:
|
||||
* 1. There is already a current car
|
||||
* 2. The new car's ID already exists in any structure
|
||||
* 3. The new car's ID is negative
|
||||
*
|
||||
* @param car the ID of the car arriving at the station
|
||||
* @return true if the car successfully arrived; false otherwise
|
||||
*/
|
||||
virtual bool addToStation(int car) = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the current car.
|
||||
* Return -1 if there is no current car.
|
||||
*
|
||||
* @return the ID of the current car; -1 if there is no current car
|
||||
*/
|
||||
virtual int showCurrentCar() = 0;
|
||||
|
||||
/**
|
||||
* Removes the current car from the station.
|
||||
* Does nothing if there is no current car.
|
||||
*
|
||||
* @return true if the current car successfully left; false otherwise
|
||||
*/
|
||||
virtual bool removeFromStation() = 0;
|
||||
|
||||
//Part 2--------------------------------------------------------------
|
||||
/**
|
||||
* Adds the current car to the stack. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the stack is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the stack; false otherwise
|
||||
*/
|
||||
virtual bool addToStack() = 0;
|
||||
|
||||
/**
|
||||
* Removes the first car in the stack and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the stack already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the stack; false otherwise
|
||||
*/
|
||||
virtual bool removeFromStack() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the first car in the stack.
|
||||
*
|
||||
* @return the ID of the first car in the stack; -1 if the stack is empty
|
||||
*/
|
||||
virtual int showTopOfStack() = 0;
|
||||
|
||||
/**
|
||||
* Returns the number of cars in the stack.
|
||||
*
|
||||
* @return the number of cars in the stack
|
||||
*/
|
||||
virtual int showSizeOfStack() = 0;
|
||||
|
||||
//Part 3--------------------------------------------------------------
|
||||
/**
|
||||
* Adds the current car to the queue. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the queue is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the queue; false otherwise
|
||||
*/
|
||||
virtual bool addToQueue() = 0;
|
||||
|
||||
/**
|
||||
* Removes the first car in the queue and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the queue already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the queue; false otherwise
|
||||
*/
|
||||
virtual bool removeFromQueue() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the first car in the queue.
|
||||
*
|
||||
* @return the ID of the first car in the queue; -1 if the queue is empty
|
||||
*/
|
||||
virtual int showTopOfQueue() = 0;
|
||||
|
||||
/**
|
||||
* Returns the number of cars in the queue.
|
||||
*
|
||||
* @return the number of cars in the queue
|
||||
*/
|
||||
virtual int showSizeOfQueue() = 0;
|
||||
|
||||
//Part 4--------------------------------------------------------------
|
||||
/**
|
||||
* Adds the current car to the deque on the left side. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the deque is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the deque; false otherwise
|
||||
*/
|
||||
virtual bool addToDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Adds the current car to the deque on the right side. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the deque is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the deque; false otherwise
|
||||
*/
|
||||
virtual bool addToDequeRight() = 0;
|
||||
|
||||
/**
|
||||
* Removes the leftmost car in the deque and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the deque already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the deque; false otherwise
|
||||
*/
|
||||
virtual bool removeFromDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Removes the rightmost car in the deque and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the deque already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the deque; false otherwise
|
||||
*/
|
||||
virtual bool removeFromDequeRight() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the leftmost car in the deque.
|
||||
*
|
||||
* @return the ID of the leftmost car in the deque; -1 if the deque is empty
|
||||
*/
|
||||
virtual int showTopOfDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the rightmost car in the deque.
|
||||
*
|
||||
* @return the ID of the rightmost car in the deque; -1 if the deque is empty
|
||||
*/
|
||||
virtual int showTopOfDequeRight() = 0;
|
||||
|
||||
/**
|
||||
* Returns the number of cars in the deque.
|
||||
*
|
||||
* @return the number of cars in the deque
|
||||
*/
|
||||
virtual int showSizeOfDeque() = 0;
|
||||
};
|
||||
@@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
#include "StationInterface.h"
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
class StationInterfaceExtra : public StationInterface
|
||||
{
|
||||
public:
|
||||
StationInterfaceExtra(){}
|
||||
virtual ~StationInterfaceExtra(){}
|
||||
|
||||
//Input-Restricted Deque----------------------------------------------
|
||||
/**
|
||||
* Adds the current car to the IRDeque on the left side. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the IRDeque is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the IRDeque; false otherwise
|
||||
*/
|
||||
virtual bool addToIRDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Removes the leftmost car in the IRDeque and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the IRDeque already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the IRDeque; false otherwise
|
||||
*/
|
||||
virtual bool removeFromIRDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Removes the rightmost car in the IRDeque and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the IRDeque already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the IRDeque; false otherwise
|
||||
*/
|
||||
virtual bool removeFromIRDequeRight() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the leftmost car in the IRDeque.
|
||||
*
|
||||
* @return the ID of the leftmost car in the IRDeque; -1 if the IRDeque is empty
|
||||
*/
|
||||
virtual int showTopOfIRDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the rightmost car in the IRDeque.
|
||||
*
|
||||
* @return the ID of the rightmost car in the IRDeque; -1 if the IRDeque is empty
|
||||
*/
|
||||
virtual int showTopOfIRDequeRight() = 0;
|
||||
|
||||
/**
|
||||
* Returns the number of cars in the IRDeque.
|
||||
*
|
||||
* @return the number of cars in the IRDeque
|
||||
*/
|
||||
virtual int showSizeOfIRDeque() = 0;
|
||||
|
||||
//Output-Restricted Deque---------------------------------------------
|
||||
/**
|
||||
* Adds the current car to the ORDeque on the left side. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the ORDeque is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the ORDeque; false otherwise
|
||||
*/
|
||||
virtual bool addToORDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Adds the current car to the ORDeque on the right side. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the ORDeque is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the ORDeque; false otherwise
|
||||
*/
|
||||
virtual bool addToORDequeRight() = 0;
|
||||
|
||||
/**
|
||||
* Removes the leftmost car in the ORDeque and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the ORDeque already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the ORDeque; false otherwise
|
||||
*/
|
||||
virtual bool removeFromORDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the leftmost car in the ORDeque.
|
||||
*
|
||||
* @return the ID of the leftmost car in the ORDeque; -1 if the ORDeque is empty
|
||||
*/
|
||||
virtual int showTopOfORDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Returns the number of cars in the ORDeque.
|
||||
*
|
||||
* @return the number of cars in the ORDeque
|
||||
*/
|
||||
virtual int showSizeOfORDeque() = 0;
|
||||
};
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "deque.h"
|
||||
|
||||
bool deque::addToDequeLeft(int id) {
|
||||
if(in_deque(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertHead(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool deque::addToDequeRight(int id) {
|
||||
if(in_deque(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertTail(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
int deque::removeFromDequeLeft() {
|
||||
int id = l.at(l.size() - 1);
|
||||
l.remove(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
int deque::removeFromDequeRight() {
|
||||
int id = l.at(0);
|
||||
l.remove(id);
|
||||
return false;
|
||||
}
|
||||
|
||||
int deque::showTopOfDequeLeft() {
|
||||
if(l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
int left = l.at(l.size() - 1);
|
||||
return left;
|
||||
}
|
||||
|
||||
int deque::showTopOfDequeRight() {
|
||||
if(l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
int right = l.at(0);
|
||||
return right;
|
||||
}
|
||||
|
||||
int deque::showSizeOfDeque() {
|
||||
return l.size();
|
||||
}
|
||||
|
||||
bool deque::in_deque(int id) {
|
||||
node* node_ptr = l.head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->id == id) {
|
||||
return true;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef __DEQUE_H__
|
||||
#define __DEQUE_H__
|
||||
|
||||
#include "linkedlist.h"
|
||||
|
||||
class deque {
|
||||
public:
|
||||
linkedlist l;
|
||||
bool addToDequeLeft(int id);
|
||||
bool addToDequeRight(int id);
|
||||
int removeFromDequeLeft();
|
||||
int removeFromDequeRight();
|
||||
int showTopOfDequeLeft();
|
||||
int showTopOfDequeRight();
|
||||
int showSizeOfDeque();
|
||||
bool in_deque(int id);
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,52 @@
|
||||
#include "irdeque.h"
|
||||
|
||||
bool irdeque::addToIRDequeLeft(int id) {
|
||||
if(in_irdeque(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertHead(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool irdeque::removeFromIRDequeLeft() {
|
||||
int id = l.at(l.size() - 1);
|
||||
l.remove(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool irdeque::removeFromIRDequeRight() {
|
||||
int id = l.at(0);
|
||||
l.remove(id);
|
||||
return false;
|
||||
}
|
||||
|
||||
int irdeque::showTopOfIRDequeLeft() {
|
||||
if(l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
int left = l.at(l.size() - 1);
|
||||
return left;
|
||||
}
|
||||
|
||||
int irdeque::showTopOfIRDequeRight() {
|
||||
if(l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
int right = l.at(0);
|
||||
return right;
|
||||
}
|
||||
|
||||
int irdeque::showSizeOfIRDeque() {
|
||||
return l.size();
|
||||
}
|
||||
|
||||
bool irdeque::in_irdeque(int id) {
|
||||
node* node_ptr = l.head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->id == id) {
|
||||
return true;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef __IRDEQUE_H__
|
||||
#define __IRDEQUE_H__
|
||||
|
||||
#include "linkedlist.h"
|
||||
|
||||
class irdeque {
|
||||
public:
|
||||
linkedlist l;
|
||||
bool addToIRDequeLeft(int id);
|
||||
bool removeFromIRDequeLeft();
|
||||
bool removeFromIRDequeRight();
|
||||
int showTopOfIRDequeLeft();
|
||||
int showTopOfIRDequeRight();
|
||||
int showSizeOfIRDeque();
|
||||
bool in_irdeque(int id);
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,102 @@
|
||||
#include "linkedlist.h"
|
||||
|
||||
linkedlist::linkedlist(): head(NULL) {}
|
||||
linkedlist::~linkedlist(){clear();}
|
||||
|
||||
void linkedlist::insertHead(int id) {
|
||||
head = new node(id, head);
|
||||
}
|
||||
|
||||
void linkedlist::insertTail(int id) {
|
||||
if(head == NULL) {
|
||||
insertHead(id);
|
||||
return;
|
||||
}
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->next == NULL) {
|
||||
node_ptr->next = new node(id, NULL);
|
||||
break;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
}
|
||||
|
||||
void linkedlist::insertAfter(int id, int insertionNode) {
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->id == insertionNode) {
|
||||
node* temp = new node(id, NULL);
|
||||
temp->next = node_ptr->next;
|
||||
node_ptr->next = temp;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
}
|
||||
|
||||
void linkedlist::remove(int id) {
|
||||
node* node_ptr = head;
|
||||
if(node_ptr->id == id) {
|
||||
node* ptr = head;
|
||||
head = head->next;
|
||||
delete ptr;
|
||||
return;
|
||||
}
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->next == NULL)
|
||||
return;
|
||||
if(node_ptr->next->id == id) {
|
||||
node* ptr = node_ptr->next;
|
||||
node_ptr->next = node_ptr->next->next;
|
||||
delete ptr;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
}
|
||||
|
||||
void linkedlist::clear() {
|
||||
node* node_ptr = head;
|
||||
while(head) {
|
||||
node_ptr = head;
|
||||
head = head->next;
|
||||
delete node_ptr;
|
||||
}
|
||||
}
|
||||
|
||||
int linkedlist::at(int index) {
|
||||
if(index < 0) {
|
||||
return -1;
|
||||
}
|
||||
if(size() <= index) {
|
||||
return -1;
|
||||
}
|
||||
node* node_ptr = head;
|
||||
for(int i = 0; i < index; i++){
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
if(head == NULL)
|
||||
return -1;
|
||||
return node_ptr->id;
|
||||
}
|
||||
|
||||
int linkedlist::size() {
|
||||
int size_of_list = 0;
|
||||
if(head == NULL) {
|
||||
return 0;
|
||||
}
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
size_of_list++;
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return size_of_list;
|
||||
}
|
||||
|
||||
ostream & operator<<(ostream & os, linkedlist & f) {
|
||||
node* node_ptr = f.head;
|
||||
while(node_ptr != NULL) {
|
||||
os << node_ptr->id << ",";
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return os;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef __LINKEDLIST_H__
|
||||
#define __LINKEDLIST_H__
|
||||
#include <iostream>
|
||||
#include "node.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class linkedlist {
|
||||
public:
|
||||
linkedlist();
|
||||
~linkedlist();
|
||||
node* head;
|
||||
bool has_a(int value);
|
||||
bool is_it_zero(int value);
|
||||
void insertHead(int value);
|
||||
void insertTail(int value);
|
||||
void insertAfter(int value, int insertionNode);
|
||||
void remove(int value);
|
||||
void clear();
|
||||
int at(int index);
|
||||
int size();
|
||||
friend ostream & operator<<(ostream & os, linkedlist & f);
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,4 @@
|
||||
#include "station.h"
|
||||
|
||||
int main() {
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
#include "node.h"
|
||||
|
||||
node::node(const int id, node* next) : id(id), next(next) {}
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef __NODE_H__
|
||||
#define __NODE_H__
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class node {
|
||||
public:
|
||||
node(const int, node*);
|
||||
int id;
|
||||
node* next;
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "ordeque.h"
|
||||
|
||||
bool ordeque::addToORDequeLeft(int id) {
|
||||
if(in_ordeque(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertHead(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ordeque::addToORDequeRight(int id) {
|
||||
if(in_ordeque(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertTail(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ordeque::removeFromORDequeLeft() {
|
||||
int id = l.at(l.size() - 1);
|
||||
l.remove(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
int ordeque::showTopOfORDequeLeft() {
|
||||
if(l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
int left = l.at(l.size() - 1);
|
||||
return left;
|
||||
}
|
||||
|
||||
int ordeque::showSizeOfORDeque() {
|
||||
return l.size();
|
||||
}
|
||||
|
||||
bool ordeque::in_ordeque(int id) {
|
||||
node* node_ptr = l.head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->id == id) {
|
||||
return true;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef __ORDEQUE_H__
|
||||
#define __ORDEQUE_H__
|
||||
|
||||
#include "linkedlist.h"
|
||||
|
||||
class ordeque {
|
||||
public:
|
||||
linkedlist l;
|
||||
bool addToORDequeLeft(int id);
|
||||
bool addToORDequeRight(int id);
|
||||
bool removeFromORDequeLeft();
|
||||
int showTopOfORDequeLeft();
|
||||
int showSizeOfORDeque();
|
||||
bool in_ordeque(int id);
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int usleep(useconds_t usec) {
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include "queue.h"
|
||||
|
||||
bool queue::addToQueue(int id) {
|
||||
if(in_queue(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertTail(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool queue::removeFromQueue() {
|
||||
int id = l.at(0);
|
||||
l.remove(id);
|
||||
return false;
|
||||
}
|
||||
|
||||
int queue::showTopOfQueue() {
|
||||
if(l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
int top = l.at(0);
|
||||
return top;
|
||||
}
|
||||
|
||||
int queue::showSizeOfQueue() {
|
||||
return l.size();
|
||||
}
|
||||
|
||||
bool queue::in_queue(int id) {
|
||||
node* node_ptr = l.head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->id == id) {
|
||||
return true;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef __QUEUE_H__
|
||||
#define __QUEUE_H__
|
||||
|
||||
#include "linkedlist.h"
|
||||
|
||||
class queue {
|
||||
public:
|
||||
linkedlist l;
|
||||
bool addToQueue(int id);
|
||||
bool removeFromQueue();
|
||||
int showTopOfQueue();
|
||||
int showSizeOfQueue();
|
||||
bool in_queue(int id);
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,37 @@
|
||||
#include "stack.h"
|
||||
|
||||
bool stack::addToStack(int id) {
|
||||
if(in_stack(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertHead(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
int stack::removeFromStack() {
|
||||
int id = l.at(0);
|
||||
l.remove(id);
|
||||
return false;
|
||||
}
|
||||
|
||||
int stack::showTopOfStack() {
|
||||
if(l.size() == 0)
|
||||
return -1;
|
||||
int top = l.at(0);
|
||||
return top;
|
||||
}
|
||||
|
||||
int stack::showSizeOfStack() {
|
||||
return l.size();
|
||||
}
|
||||
|
||||
bool stack::in_stack(int id) {
|
||||
node* node_ptr = l.head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->id == id) {
|
||||
return true;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef __STACK_H__
|
||||
#define __STACK_H__
|
||||
|
||||
#include "linkedlist.h"
|
||||
|
||||
class stack {
|
||||
public:
|
||||
linkedlist l;
|
||||
node* head;
|
||||
bool addToStack(int id);
|
||||
int removeFromStack();
|
||||
int showTopOfStack();
|
||||
int showSizeOfStack();
|
||||
bool in_stack(int id);
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,249 @@
|
||||
#include "station.h"
|
||||
|
||||
bool station::addToStation(int car) {
|
||||
if(current_car == -1 and car > 0 and !s.in_stack(car) and !q.in_queue(car) and !d.in_deque(car) and !ir.in_irdeque(car) and !ord.in_ordeque(car)) {
|
||||
current_car = car;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int station::showCurrentCar() {
|
||||
return current_car;
|
||||
}
|
||||
|
||||
bool station::removeFromStation() {
|
||||
if(current_car != -1) {
|
||||
current_car = -1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool station::addToStack() {
|
||||
if(current_car == -1 or s.l.size() > 5) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
bool success = s.addToStack(current_car);
|
||||
if(success) {
|
||||
current_car = -1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool station::removeFromStack() {
|
||||
if(s.l.size() == 0 or current_car != -1) {
|
||||
return false;
|
||||
}
|
||||
current_car = s.removeFromStack();
|
||||
return true;
|
||||
}
|
||||
|
||||
int station::showTopOfStack() {
|
||||
if(s.l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
return s.showTopOfStack();
|
||||
}
|
||||
|
||||
int station::showSizeOfStack() {
|
||||
return s.l.size();
|
||||
}
|
||||
|
||||
bool station::addToQueue() {
|
||||
if(current_car == -1 or q.l.size() > 5) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
bool success = q.addToQueue(current_car);
|
||||
if(success) {
|
||||
current_car = -1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool station::removeFromQueue() {
|
||||
if(q.l.size() == 0 or current_car != -1) {
|
||||
return false;
|
||||
}
|
||||
current_car = q.removeFromQueue();
|
||||
return true;
|
||||
}
|
||||
|
||||
int station::showTopOfQueue() {
|
||||
if(q.l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
return q.showTopOfQueue();
|
||||
}
|
||||
|
||||
int station::showSizeOfQueue() {
|
||||
return q.showSizeOfQueue();
|
||||
}
|
||||
|
||||
bool station::addToDequeLeft() {
|
||||
if(current_car == -1 or d.l.size() > 5) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
bool success = d.addToDequeRight(current_car);
|
||||
if(success) {
|
||||
current_car = -1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool station::addToDequeRight() {
|
||||
if(current_car == -1 or d.l.size() > 5) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
bool success = d.addToDequeLeft(current_car);
|
||||
if(success) {
|
||||
current_car = -1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool station::removeFromDequeLeft() {
|
||||
if(d.l.size() == 0 or current_car != -1) {
|
||||
return false;
|
||||
}
|
||||
current_car = d.removeFromDequeLeft();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool station::removeFromDequeRight() {
|
||||
if(d.l.size() == 0 or current_car != -1) {
|
||||
return false;
|
||||
}
|
||||
current_car = d.removeFromDequeRight();
|
||||
return true;
|
||||
}
|
||||
|
||||
int station::showTopOfDequeLeft() {
|
||||
if(d.l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
return d.showTopOfDequeLeft();
|
||||
}
|
||||
|
||||
int station::showTopOfDequeRight() {
|
||||
if(d.l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
return d.showTopOfDequeRight();
|
||||
}
|
||||
|
||||
int station::showSizeOfDeque() {
|
||||
return d.showSizeOfDeque();
|
||||
}
|
||||
|
||||
bool station::addToIRDequeLeft() {
|
||||
if(current_car == -1 or ir.l.size() > 5) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
bool success = ir.addToIRDequeLeft(current_car);
|
||||
if(success) {
|
||||
current_car = -1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool station::removeFromIRDequeLeft() {
|
||||
if(ir.l.size() == 0 or current_car != -1) {
|
||||
return false;
|
||||
}
|
||||
current_car = ir.removeFromIRDequeRight();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool station::removeFromIRDequeRight() {
|
||||
if(ir.l.size() == 0 or current_car != -1) {
|
||||
return false;
|
||||
}
|
||||
current_car = ir.removeFromIRDequeLeft();
|
||||
return true;
|
||||
}
|
||||
|
||||
int station::showTopOfIRDequeLeft() {
|
||||
if(ir.l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
return ir.showTopOfIRDequeRight();
|
||||
}
|
||||
|
||||
int station::showTopOfIRDequeRight() {
|
||||
if(ir.l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
return ir.showTopOfIRDequeLeft();
|
||||
}
|
||||
|
||||
int station::showSizeOfIRDeque() {
|
||||
return ir.l.size();
|
||||
}
|
||||
|
||||
bool station::addToORDequeLeft() {
|
||||
if(current_car == -1 or ord.l.size() > 5) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
bool success = ord.addToORDequeRight(current_car);
|
||||
if(success) {
|
||||
current_car = -1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool station::addToORDequeRight() {
|
||||
if(current_car == -1 or ord.l.size() > 5) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
bool success = ord.addToORDequeLeft(current_car);
|
||||
if(success) {
|
||||
current_car = -1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool station::removeFromORDequeLeft() {
|
||||
if(ord.l.size() == 0 or current_car != -1) {
|
||||
return false;
|
||||
}
|
||||
current_car = ord.removeFromORDequeLeft();
|
||||
return true;
|
||||
}
|
||||
|
||||
int station::showTopOfORDequeLeft(){
|
||||
if(ord.l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
return ord.showTopOfORDequeLeft();
|
||||
}
|
||||
|
||||
int station::showSizeOfORDeque() {
|
||||
return ord.l.size();
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#ifndef __STATION_H__
|
||||
#define __STATION_H__
|
||||
|
||||
#include "StationInterfaceExtra.h"
|
||||
#include "queue.h"
|
||||
#include "deque.h"
|
||||
#include "stack.h"
|
||||
#include "irdeque.h"
|
||||
#include "ordeque.h"
|
||||
|
||||
class station : public StationInterfaceExtra {
|
||||
public:
|
||||
station(): current_car(-1) {};
|
||||
bool addToStation(int car);
|
||||
int showCurrentCar();
|
||||
bool removeFromStation();
|
||||
bool addToStack();
|
||||
bool removeFromStack();
|
||||
int showTopOfStack();
|
||||
int showSizeOfStack();
|
||||
bool addToQueue();
|
||||
bool removeFromQueue();
|
||||
int showTopOfQueue();
|
||||
int showSizeOfQueue();
|
||||
bool addToDequeLeft();
|
||||
bool addToDequeRight();
|
||||
bool removeFromDequeLeft();
|
||||
bool removeFromDequeRight();
|
||||
int showTopOfDequeLeft();
|
||||
int showTopOfDequeRight();
|
||||
int showSizeOfDeque();
|
||||
bool addToIRDequeLeft();
|
||||
bool removeFromIRDequeLeft();
|
||||
bool removeFromIRDequeRight();
|
||||
int showTopOfIRDequeLeft();
|
||||
int showTopOfIRDequeRight();
|
||||
int showSizeOfIRDeque();
|
||||
bool addToORDequeLeft();
|
||||
bool addToORDequeRight();
|
||||
bool removeFromORDequeLeft();
|
||||
int showTopOfORDequeLeft();
|
||||
int showSizeOfORDeque();
|
||||
private:
|
||||
int current_car;
|
||||
queue q;
|
||||
deque d;
|
||||
stack s;
|
||||
irdeque ir;
|
||||
ordeque ord;
|
||||
};
|
||||
#endif
|
||||
Reference in New Issue
Block a user