Stacks - cs 6cs6.yolasite.com/resources/STACKS.pdf · Data Structures Using C++ 3 Stacks...

Post on 30-Sep-2020

2 views 0 download

Transcript of Stacks - cs 6cs6.yolasite.com/resources/STACKS.pdf · Data Structures Using C++ 3 Stacks...

Data Structures Using C++ 1

A stack is a last in, first out (LIFO) abstract data type and data

structure. A stack can have any abstract data type as an element,

but is characterized by only two fundamental operations: push

and pop.

• The push operation adds an item to the top of the stack, hiding

any items already on the stack, or initializing the stack if it is

empty.

• The pop operation removes an item from the top of the stack,

and returns this value to the caller. A pop either reveals

previously concealed items, or results in an empty stack.

Stacks

Data Structures Using C++ 2

A stack is a restricted data structure, because only a small number

of operations are performed on it. The nature of the pop and push

operations also means that stack elements have a natural order.

Elements are removed from the stack in the reverse order to the

order of their addition: therefore, the lower elements are those that

have been on the stack the longest.

Data Structures Using C++ 3

Stacks

• Definition: list of homogeneous elements, wherein the addition and deletion of elements occur only at one end, called the top of the stack.

• Last In First Out (LIFO) data structure

• Used to implement function calls.

• Used to convert recursive algorithms into nonrecursive algorithms.

Data Structures Using C++ 4

Various Types of Stacks

Data Structures Using C++ 5

LIFO

• Last In First Out (LIFO) data structure

– Top element of stack is last element to be added

to stack.

– Elements added and removed from one end

(top).

– Item added last are removed first.

Data Structures Using C++ 6

Empty Stack Stack Operations

Data Structures Using C++ 7

Basic Operations on a Stack

• InitializeStack: Initializes the stack to an

empty state.

• DestroyStack: Removes all the elements

from the stack, leaving the stack empty.

• IsEmptyStack: Checks whether the stack is

empty. If empty, it returns true; otherwise, it

returns false.

Data Structures Using C++ 8

Continue…

• IsFullStack: Checks whether the stack is full. If

full, it returns true; otherwise, it returns false

• Push:

– Add new element to the top of the stack

– The input consists of the stack and the new element.

– Prior to this operation, the stack must exist and must

not be full

Data Structures Using C++ 9

• Top: Returns the top element of the stack.

Prior to this operation, the stack must exist

and must not be empty.

• Pop: Removes the top element of the stack.

Prior to this operation, the stack must exist

and must not be empty.

Continue…

Data Structures Using C++ 10

Empty Stack

Data Structures Using C++ 11

Example of a Stack

Data Structures Using C++ 12

InitializeStack and DestroyStack

template<class Type>

void stackType<Type>::initializeStack()

{

stackTop = 0;

}//end initializeStack

template<class Type>

void stackType<Type>::destroyStack()

{

stackTop = 0;

}//end destroyStack

Data Structures Using C++ 13

EmptyStack and FullStack

template<class Type>

bool stackType<Type>::isEmptyStack()

{

return(stackTop == 0);

}//end isEmptyStack

template<class Type>

bool stackType<Type>::isFullStack()

{

return(stackTop == maxStackSize);

}//end isFullStack

Data Structures Using C++ 14

Push

Data Structures Using C++ 15

Push

template<class Type>

void stackType<Type>::push(const Type& newItem)

{

if(!isFullStack())

{

list[stackTop] = newItem; //add newItem at the top

//of the stack

stackTop++; //increment stackTop

}

else

cerr<<"Cannot add to a full stack."<<endl;

}//end push

Data Structures Using C++ 16

Return Top Element

template<class Type>

Type stackType<Type>::top()

{

assert(stackTop != 0); //if the stack is empty,

//terminate the program

return list[stackTop - 1]; //return the element of the

//stack indicated by

//stackTop - 1

}//end top

Data Structures Using C++ 17

Pop

Data Structures Using C++ 18

Pop

template<class Type>

void stackType<Type>::pop()

{

if(!isEmptyStack())

stackTop-; //decrement stackTop

else

cerr<<"Cannot remove from an empty stack."<<endl;

}//end pop

Data Structures Using C++ 19

copyStack

template<class Type>

void stackType<Type>::copyStack(const stackType<Type>& otherStack)

{

delete [] list;

maxStackSize = otherStack.maxStackSize;

stackTop = otherStack.stackTop;

list = new Type[maxStackSize];

assert(list != NULL);

//copy otherStack into this stack

for(int j = 0; j < stackTop; j++)

list[j] = otherStack.list[j];

}//end copyStack

Data Structures Using C++ 20

Copy Constructor

template<class Type>

stackType<Type>::stackType(const stackType<Type>& otherStack)

{

list = NULL;

copyStack(otherStack);

}//end copy constructor

Data Structures Using C++ 21

Overloading the Assignment

Operator (=)

template<class Type>

const stackType<Type>& stackType<Type>::operator=

(const stackType<Type>& otherStack)

{

if(this != &otherStack) //avoid self-copy

copyStack(otherStack);

return *this;

}//end operator=

Data Structures Using C++ 22

Stack Header File//Header file: myStack.h

#ifndef H_StackType

#define H_StackType

#include <iostream>

#include <cassert>

using namespace std;

//Place the definition of the class template stackType, as given

//previously in this chapter, here.

//Place the definitions of the member functions, as discussed in

//this chapter, here.

#endif

Data Structures Using C++ 23

Empty and Nonempty

Linked Stack

Empty linked stack Nonempty linked stack

Data Structures Using C++ 24

Default Constructor

template<class Type> //default constructor

linkedStackType<Type>::linkedStackType()

{

stackTop = NULL;

}

Data Structures Using C++ 25

Destroy Stacktemplate<class Type>

void linkedStackType<Type>::destroyStack()

{

nodeType<Type> *temp; //pointer to delete the node

while(stackTop != NULL) //while there are elements

//in the stack

{

temp = stackTop; //set temp to point to

//the current node

stackTop = stackTop->link; //advance stackTop

//to the next node

delete temp; //deallocate the memory

//occupied by temp

}

}//end destroyStack

Data Structures Using C++ 26

initializeStack and isStackEmpty

template<class Type>

void linkedStackType<Type>:: initializeStack()

{

destroyStack();

}

template<class Type>

bool linkedStackType<Type>::isEmptyStack()

{

return(stackTop == NULL);

}

template<class Type>

bool linkedStackType<Type>::isFullStack()

{

return false;

Data Structures Using C++ 27

Push

Stack before the push

operation

Stack and newNode

Data Structures Using C++ 28

Return Top Element

template<class Type>

Type linkedStackType<Type>::top()

{

assert(stackTop != NULL); //if the stack is empty,

//terminate the program

return stackTop->info; //return the top element

}//end top