





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of a stack data structure, its definition, examples, and the attributes and operations provided in its abstract data type (adt). Two possible implementations of stack adts are explored: simple array and simple linked list. Code snippets and potential implementation problems for each method.
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






The top of the stack will move. Top is initially -1. void stackClass::Push(stackItemType NewItem, bool& Success) { Success = bool(Top < MAX_STACK - 1); if (Success) { ++Top; Items[Top] = NewItem; } } void stackClass::GetStackTop(stackItemType& StackTop, bool& Success) const { Success = bool(!StackIsEmpty()); if (Success) StackTop = Items[Top]; } void stackClass::Pop(bool& Success) { Success = bool(!StackIsEmpty()); if (Success) --Top; } void stackClass::Pop(stackItemType& StackTop, bool& Success) { Success = bool(!StackIsEmpty()); if (Success) { StackTop = Items[Top]; --Top; } }
typedef desired-type-of-stack-item stackItemType; struct stackNode; // forward reference typedef stackNode* ptrType; // pointer to node struct stackNode { stackItemType Item; ptrType Next; }; class stackClass { public: stackClass(); stackClass(const stackClass& S); ~stackClass(); bool StackIsEmpty() const; void Push(stackItemType NewItem, bool& Success); void Pop(bool& Success); void Pop(stackItemType& StackTop, bool& Success); void GetStackTop(stackItemType& StackTop, bool& Success) const; private: ptrType TopPtr; // points to top of stack };
A stack initializes to null void stackClass::Push(stackItemType NewItem, bool& Success) { ptrType NewPtr = new stackNode; Success = bool(NewPtr != NULL); if (Success) { NewPtr->Item = NewItem; NewPtr->Next = TopPtr; TopPtr = NewPtr; } } void stackClass::GetStackTop(stackItemType& StackTop, bool& Success) const { Success = bool(!StackIsEmpty()); if (Success) StackTop = TopPtr->Item; } void stackClass::Pop(bool& Success) { Success = bool(!StackIsEmpty()); if (Success) { ptrType Temp = TopPtr; TopPtr = TopPtr->Next; Temp->Next = NULL: // safeguard delete Temp; } } void stackClass::Pop(stackItemType& StackTop, bool& Success) { Success = bool(!StackIsEmpty()); if (Success) { StackTop = TopPtr->Item; ptrType Temp = TopPtr; TopPtr = TopPtr->Next; Temp->Next = NULL: // safeguard delete Temp; } }