This is the C++ code I had finished for the End of Course Experience C++ Re-implementation:
(The code provided by Matt is also included.)
Files are located in src/data/EoCE/ll_oop_reimp
Makefile:
SHELL = /bin/bash -e INC = -I inc/ CXXFLAGS = -Wall OPTS = CXX = g++ $(CXXFLAGS) $(OPTS) $(INC) OBJ = AR = ar DEBUG = LIBS = src UNIT = testing ALL = $(LIBS) testing BIN = default: libs testing debug: libs-debug testing-debug libs-debug: CXX += -DDEBUG -g libs-debug: DEBUG = debug libs-debug: libs libs: @for i in $(LIBS); do make -C $$i $(DEBUG); echo; done @echo testing-debug: CXX += -DDEBUG -g testing-debug: DEBUG = debug testing-debug: testing testing: @for i in $(UNIT); do make -C $$i $(DEBUG); echo; done @echo clean: @for i in $(ALL); do make -C $$i clean; done @rm -f lib/* copy: mkdir -p ~/src/data/EoCE/ll_oop_reimp cp -av /var/public/data/fall2013/EoCE/ll_oop_reimp/* ~/src/data/EoCE/ll_oop_reimp/ help: @echo @echo "*************[ Data Structures OOP List Re-Implementation ]*************" @echo "** make - build everything **" @echo "** make debug - build everything with debug symbols **" @echo "** **" @echo "** make testing - build unit tests **" @echo "** make testing-debug - build unit tests with debugging symbols **" @echo "** make libs - build all supporting libraries **" @echo "** make libs-debug - build all libraries with debug symbols **" @echo "** **" @echo "** make clean - clean; remove all objects/compiled code **" @echo "** make help - this information **" @echo "************************************************************************"
/inc:
#ifndef _LIST_H #define _LIST_H #include "node.h" class List { public: List(); void insert(Node *, Node *); void append(Node *, Node *); Node *getNode(Node *); void displayf(); void displayb(); void sort(); int getQuantity(); Node * getStart(); Node * getEnd(); ~List(); private: Node *start; Node *end; int qty; }; #endif
#ifndef _NODE_H #define _NODE_H #include <cstdlib> class Node { public: Node(); Node(int); Node *copy(); int getValue(); Node *getNext(); Node *getPrev(); Node *getData(); void setNext(Node *); void setPrev(Node *); void setData(Node *); void setValue(int); ~Node(); private: Node *next; Node *prev; Node *data; int value; }; #endif
#ifndef _QUEUE_H #define _QUEUE_H #include "list.h" class Queue : protected List { public: Queue(); Queue(int); void enqueue(Node *); Node *dequeue(); int getBufferSize(); void setBufferSize(int); ~Queue(); private: List *data; Node *front; Node *back; int bufsiz; }; #endif
#ifndef _STACK_H #define _STACK_H #include "list.h" class Stack : protected List { public: Stack(); Stack(int); void push(Node *); Node *pop (); Node *peek(); int getSize(); int getListSize(); void setSize(int); ~Stack(); private: List *data; Node *top; int size; }; #endif
/lib: Files in this directory are made using the make file:
lab46:~/src/data/EoCE/ll_oop_reimp$ make
/src:
SHELL = /bin/bash -e INC = -I ../inc/ CFLAGS = -Wall OPTS = CXX = g++ $(CFLAGS) $(OPTS) $(INC) PARTS = $(shell /bin/ls -1Ad * | grep -v 'Makefile') DEBUG = default: libs debug: CXX += -DDEBUG -g debug: DEBUG = debug debug: libs libs: @for i in $(PARTS); do make -C $$i $(DEBUG); echo; done @echo clean: @for i in $(PARTS); do make -C $$i clean; done @rm -f ${LIB}
CFLAGS = -Wall INC = -I ../../inc AR = ar CXX = g++ $(CFLAGS) $(INC) SRC = $(shell /bin/ls -1 *.cc 2>/dev/null) OBJ = $(SRC:.cc=.o) LIB = $(shell /bin/pwd | /bin/sed 's/^.*\///') all: $(SRC) $(OBJ) lib debug: CXX += -DDEBUG -g debug: DEBUG = debug debug: $(SRC) $(OBJ) lib .cc.o: ifneq ($(MAKECMDGOALS),debug) @printf "[B] %-20s ... " "$<" @$(CXX) -c $< && echo "OK" || echo "FAIL" else $(CXX) -c $< endif lib: ifneq ($(MAKECMDGOALS),debug) @printf "[L] %-20s ... " "$<" @$(AR) rcs ../../lib/lib$(LIB).a $(OBJ) && echo "SUCCESS" || echo "FAIL" else $(AR) rcs ../../lib/lib$(LIB).a $(OBJ) endif clean: rm -f *.o $(OBJ) core
CFLAGS = -Wall INC = -I ../../inc AR = ar CXX = g++ $(CFLAGS) $(INC) SRC = $(shell /bin/ls -1 *.cc 2>/dev/null) OBJ = $(SRC:.cc=.o) LIB = $(shell /bin/pwd | /bin/sed 's/^.*\///') all: $(SRC) $(OBJ) lib debug: CXX += -DDEBUG -g debug: DEBUG = debug debug: $(SRC) $(OBJ) lib .cc.o: ifneq ($(MAKECMDGOALS),debug) @printf "[B] %-20s ... " "$<" @$(CXX) -c $< && echo "OK" || echo "FAIL" else $(CXX) -c $< endif lib: ifneq ($(MAKECMDGOALS),debug) @printf "[L] %-20s ... " "$<" @$(AR) rcs ../../lib/lib$(LIB).a $(OBJ) && echo "SUCCESS" || echo "FAIL" else $(AR) rcs ../../lib/lib$(LIB).a $(OBJ) endif clean: rm -f *.o $(OBJ) core
#include "list.h" void List :: append(Node *place, Node *newNode) { // append() - insert newNode after place. void List :: append(Node *place, Node *newNode) { Node *tmp; if ((this != NULL) && (newNode != NULL)) { if (this -> end == NULL) { this -> end = this -> end = newNode; newNode -> setPrev(NULL); newNode -> setNext(NULL); } else if (place == this -> end) { this -> end -> setPrev(newNode); newNode -> setNext(this -> end); this -> end = newNode; } else { newNode -> setNext(place); if (place != NULL) { newNode -> setPrev(place -> getPrev()); place -> setPrev(newNode); tmp = newNode -> getPrev(); tmp -> setNext(newNode); } } // Update quantity variable. this -> qty = this -> qty + 1; } }
#include "list.h" List :: List() { this -> start = NULL; this -> end = NULL; this -> qty = 0; }
#include "list.h" List :: ~List() { Node *node, *nextNode; node = this -> start; while (node != NULL) { // Set the next node pointer before node is removed. nextNode = node -> next; // Delete the Node. delete node; // Iterate to the next node. node = nextNode; } }
#include "list.h" #include <stdio.h> void List :: displayf() { Node *tmp; int i = 0; tmp = this -> start; while (tmp != NULL) { fprintf(stdout, "[%u] %u -> ", i, tmp -> getValue()); i = i + 1; tmp = tmp -> getNext(); } fprintf(stdout, "NULL\n"); } void List :: displayb() { Node *tmp; int i = getQuantity(); tmp = this -> end; while (tmp != i) { fprintf(stdout, "[%u] %u -> ", i, tmp -> getValue()); i = i - 1; tmp = tmp -> getPrev(); } fprintf(stdout, "NULL\n"); }
#include "list.h" Node * List :: getEnd() { // Return the end of the list. return (this -> end;) }
#include "list.h" Node * List :: getNode (Node *place) { // to be implemented // get input. // iterate through list to input + 1 (Because 0 is counted). // return input + 1. }
#include "list.h" // insert() - insert newNode before place in this void List :: insert(Node *place, Node *newNode) { Node *tmp; if ((this != NULL) && (newNode != NULL)) { if (this -> start == NULL) { this -> start = this -> end = newNode; newNode -> setPrev(NULL); newNode -> setNext(NULL); } else if (place == this -> start) { this -> start -> setPrev(newNode); newNode -> setNext(this -> start); this -> start = newNode; } else { newNode -> setNext(place); if (place != NULL) { newNode -> setPrev(place -> getPrev()); place -> setPrev(newNode); tmp = newNode -> getPrev(); tmp -> setNext(newNode); } } // Update quantity variable. this -> qty = this -> qty + 1; } }
#include "list.h" int List :: getQuantity() { return(qty); }
#include "list.h" void List :: sort() { list = this; list :: iterator = i; list.sort(); std::cout << "Sorted List: "; // Iterate through the list for (i = this.begin(); i != this.end(); ++i) // Compare the values // If this->node->value > this->node->next->value // remove this->node; append after this->node->next // Print sorted values. std::cout << ' ' << *i; return (0); }
#include "list.h" Node * List :: getStart() { // Return the start of the list. return (this -> start;) }
CFLAGS = -Wall INC = -I ../../inc AR = ar CXX = g++ $(CFLAGS) $(INC) SRC = $(shell /bin/ls -1 *.cc 2>/dev/null) OBJ = $(SRC:.cc=.o) LIB = $(shell /bin/pwd | /bin/sed 's/^.*\///') all: $(SRC) $(OBJ) lib debug: CXX += -DDEBUG -g debug: DEBUG = debug debug: $(SRC) $(OBJ) lib .cc.o: ifneq ($(MAKECMDGOALS),debug) @printf "[B] %-20s ... " "$<" @$(CXX) -c $< && echo "OK" || echo "FAIL" else $(CXX) -c $< endif lib: ifneq ($(MAKECMDGOALS),debug) @printf "[L] %-20s ... " "$<" @$(AR) rcs ../../lib/lib$(LIB).a $(OBJ) && echo "SUCCESS" || echo "FAIL" else $(AR) rcs ../../lib/lib$(LIB).a $(OBJ) endif clean: rm -f *.o $(OBJ) core
#include "node.h" Node * Node :: copy() { // First create an empthy node. Node *tmp; Node *tmp2 = new Node; // "Get" then "Set" the next pointer for tmp2. tmp = this -> getNext(); tmp2 -> setNext(tmp) // "Get" then "Set" the prev pointer for tmp2. tmp = this -> getPrev(); tmp2 -> setPrev(tmp); // "Get" then "Set" the value pointer for tmp2. tmp = this -> getValue(); tmp2 -> setValue(tmp); // "Get" then "Set" the data pointer for tmp2. tmp = this -> getData(); tmp2 -> setData(tmp); // Return copied node. return(tmp2); }
#include "node.h" Node :: Node() { this -> next = NULL; this -> prev = NULL; this -> data = NULL; this -> value = 0; } Node :: Node(int value) { this -> next = NULL; this -> prev = NULL; this -> data = NULL; this -> value = value; }
#include "node.h" Node * Node :: getData() { return (this -> data); } void Node :: setData(Node *data) { this -> data = data; }
#include "node.h" Node :: ~Node() { this -> next = NULL; this -> prev = NULL; if (this -> data != NULL) delete this -> data; this -> data = NULL; this -> value = 0; }
#include "node.h" Node * Node :: getNext() { return (this -> next); } void Node :: setNext(Node *next) { this -> next = next; }
#include "node.h" Node * Node :: getPrev() { return (this -> prev); } void Node :: setPrev(Node *prev) { this -> prev = prev; }
#include "node.h" int Node :: getValue() { return (this -> value); } void Node :: setValue(int value) { this -> value = value; }
CFLAGS = -Wall INC = -I ../../inc AR = ar CXX = g++ $(CFLAGS) $(INC) SRC = $(shell /bin/ls -1 *.cc 2>/dev/null) OBJ = $(SRC:.cc=.o) LIB = $(shell /bin/pwd | /bin/sed 's/^.*\///') all: $(SRC) $(OBJ) lib debug: CXX += -DDEBUG -g debug: DEBUG = debug debug: $(SRC) $(OBJ) lib .cc.o: ifneq ($(MAKECMDGOALS),debug) @printf "[B] %-20s ... " "$<" @$(CXX) -c $< && echo "OK" || echo "FAIL" else $(CXX) -c $< endif lib: ifneq ($(MAKECMDGOALS),debug) @printf "[L] %-20s ... " "$<" @$(AR) rcs ../../lib/lib$(LIB).a $(OBJ) && echo "SUCCESS" || echo "FAIL" else $(AR) rcs ../../lib/lib$(LIB).a $(OBJ) endif clean: rm -f *.o $(OBJ) core
#include "queue.h" Queue :: Queue() { // to be implemented } Queue :: Queue(int size) { // to be implemented }
#include "queue.h" Node * Queue :: dequeue() { // to be implemented }
#include "queue.h" Queue :: ~Queue() { delete this -> data; this -> data = NULL; this -> bufsiz = 0; this -> front = NULL; this -> back = NULL; }
#include "queue.h" void Queue :: enqueue(Node *newNode) { // to be implemented }
#include "queue.h" int Queue :: getBufferSize() { // to be implemented } void Queue :: setBufferSize(int size) { // to be implemented }
CFLAGS = -Wall INC = -I ../../inc AR = ar CXX = g++ $(CFLAGS) $(INC) SRC = $(shell /bin/ls -1 *.cc 2>/dev/null) OBJ = $(SRC:.cc=.o) LIB = $(shell /bin/pwd | /bin/sed 's/^.*\///') all: $(SRC) $(OBJ) lib debug: CXX += -DDEBUG -g debug: DEBUG = debug debug: $(SRC) $(OBJ) lib .cc.o: ifneq ($(MAKECMDGOALS),debug) @printf "[B] %-20s ... " "$<" @$(CXX) -c $< && echo "OK" || echo "FAIL" else $(CXX) -c $< endif lib: ifneq ($(MAKECMDGOALS),debug) @printf "[L] %-20s ... " "$<" @$(AR) rcs ../../lib/lib$(LIB).a $(OBJ) && echo "SUCCESS" || echo "FAIL" else $(AR) rcs ../../lib/lib$(LIB).a $(OBJ) endif clean: rm -f *.o $(OBJ) core
#include "stack.h" Stack :: Stack() { // to be implemented // Call create(); from /src/list // Set *top pointer. /* Node *top; this -> start = top; this -> qty = 0; */ } Stack :: Stack(int size) { return(getSize); }
#include "stack.h" Stack :: ~Stack() { delete this -> data; // here I subconsciously did the "right thing"- set data to NULL immediately after calling delete on it this -> data = NULL; this -> size = 0; this -> top = NULL; // a possible means of establishing sane behavior. This needs testing; it may not work here in the destructor. this = NULL; } // NOTE: If the explicit "this = NULL;" line doesn't work, one will need to set the deleted pointer to NULL as an immediate next action.
#include "stack.h" Node * Stack :: peek() { return(stack.top) }
#include "stack.h" Node * Stack :: pop() { // to be implemented }
#include "stack.h" void Stack :: push(Node *newNode) { // to be implemented }
#include "stack.h" int Stack :: getSize() { // to be implemented // 1. Set *size to list. // 2. Iterate through list while incrementing *size // 3. return (size); // tmp variable for List. List *tmp; // i is an iterator. int i = 0; // Set tmp to the start of this. tmp = this -> start; // Iterate through this while incrementing the node count i. while (tmp != NULL) { // Count the node FIRST. i = i + 1; // Iterate tmp to the next node. tmp = tmp -> getNext(); } return(tmp) } void Stack :: setSize(int size) { this -> size = size; } int Stack :: getListSize() { return (this -> getQuantity()); }
/testing:
CFLAGS = -Wall -L../lib -lqueue -lstack -llist -lnode INC = -I ../inc/ CXX = g++ $(INC) SRC = $(shell /bin/ls -1 *.cc 2>/dev/null) BIN = $(SRC:.cc=) all: $(SRC) $(BIN) debug: CXX += -DDEBUG -g debug: DEBUG = debug debug: $(SRC) $(BIN) .cc: ifneq ($(MAKECMDGOALS),debug) @printf "[B] %-20s ... " "$<" @$(CXX) -o $@ $< $(CFLAGS) && echo "OK" || echo "FAIL" else $(CXX) -o $@ $< $(CFLAGS) endif clean: rm -f *.o $(BIN) core
#include <stdio.h> #include "stack.h" int main() { Node *tmp = new Node; Stack *myStack = new Stack(0); tmp -> setValue(fgetc(stdin)); fgetc(stdin); while(tmp->getValue() != '\n') { myStack -> push(tmp); tmp = new Node(fgetc(stdin)); fgetc(stdin); } fprintf(stdout, "linked list has %d nodes\n", myStack -> getListSize()); fprintf(stdout, "String is: "); tmp = myStack -> pop(); while (tmp != NULL) { fprintf(stdout, "%c", tmp -> getValue()); delete tmp; tmp = myStack -> pop(); } while(tmp != NULL); fprintf(stdout, "\n"); return(0); }