Stack Data Structure: Implementations and Operations, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/19/2009

koofers-user-9d3
koofers-user-9d3 🇺🇸

5

(1)

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CHAPTER 6
STACKS
Defn: A stack is a data structure in which the item last in is the
item first out (LIFO).
Examples:
dishes on a shelf
newspapers on a rack
storing context switching information in memory
We want to design an ADT for Stacks.
The data structure must provide the following attributes:
length
top
contents
1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Stack Data Structure: Implementations and Operations and more Study notes Computer Science in PDF only on Docsity!

CHAPTER 6

STACKS

Defn: A stack is a data structure in which the item last in is the

item first out (LIFO).

Examples:

 dishes on a shelf

 newspapers on a rack

 storing context switching information in memory

We want to design an ADT for Stacks.

The data structure must provide the following attributes:

 length

 top

 contents

The following operations must be provided in the ADT:

 Push : add to the top of the stack

 Pop : remove from the top of the stack

 Top : get the information stored in the top item of the

stack

 Empty : check to see if the stack is empty

 Initialize : create an empty stack

We will look at two possible implementations of Stack ADTs:

 Simple array

 Simple linked list

 accessing an empty stack

C++ Code:

 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; } }

Specification for the Linked Stack

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 };

C++ Code:

 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; } }