Project4 - Thomas Nanke CIS 200-001 CIS 200L-001...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Thomas Nanke CIS 200-001 CIS 200L-001 Programming Project #4 Stack and Queue 4/14/08
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The Machine type that I used for this program is the PC with Microsoft Windows XP Professional as the operating system. The compiler type that I used is the Microsoft Visual 6.0 Compiler. The test machine was an HP (not sure if that knowledge is needed). The goal of this project was to design and implement a stack and a queue that could perform all of the basis functions that these data structures normally perform, with the addition of a length() and print() function. The length function was to return the number of elements in both, and the print function was to print out these elements. After implementing all of the different functions, we were given main() routines to test the various functions. Question 1: Here is my source code: #include<iostream> using namespace std; template<class ItemType> struct NodeType { ItemType info; NodeType* next; }; template<class ItemType> class Stack { private: NodeType<ItemType>* topPtr; // It points to a singly-linked list public: Stack( ); //Default constructor //Post: Stack is created and empty. Stack(const Stack<ItemType> &anotherItem); //Copy constructor //Implicitly called for pass by value. void operator= (Stack<ItemType>); void MakeEmpty(); //Post: Stack is empty bool IsEmpty( ); //Pre: Stack has been initialized //Post: True is returned if there are no elements in the stack. // False is returned otherwise.
Background image of page 2
bool IsFull( ); //Pre: Stack has been initialized //Post: True is returned if there is no room for another ItemType // on the free store. False is returned otherwise. int length( ); //Post: The number of elements in the stack is returned. void Print( ); //Post: The value of all elements in the stack in the sequence // from the top to bottom has been printed. void Push(ItemType newItem); //Pre: Stack has been initialized and is not full //Post: If stack is full, an exception is thrown. // Otherwise, newItem is at the top of the stack. void Pop(ItemType &item); //Pre: Stack has been initialized and is not empty //Post: If stack is empty, an exception is thrown. // Otherwise, top element has been removed from the stack. // item is a copy of removed element. ~Stack(); //Destructor //Post: The Stack is empty; memory for nodes has been deallocated. }; template<class ItemType> Stack<ItemType>::Stack() { topPtr = NULL; } template<class ItemType> { NodeType<ItemType>* ptr1; NodeType<ItemType>* ptr2; if(anotherStack.topPtr == NULL) topPtr = NULL; else // allocate memory for first node { topPtr = new NodeType<ItemType>; topPtr->info = anotherStack.topPtr->info; ptr1 = anotherStack.topPtr->next; ptr2 = topPtr; while(ptr1 != NULL) // deep copy other nodes { ptr2->next = new NodeType<ItemType>; ptr2 = ptr2->next;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ptr2->info = ptr1->info; ptr1 = ptr1->next; } ptr2->next = NULL; } } template<class ItemType> void Stack<ItemType>::MakeEmpty() { topPtr = NULL; //sets topPtr equal to NULL
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

Project4 - Thomas Nanke CIS 200-001 CIS 200L-001...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online