Chapter 7

1 / 50

# Chapter 7 - PowerPoint PPT Presentation

Chapter 7. Stacks. Chapter Objectives. Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a stack as a linked list Discover stack applications Learn to use a stack to remove recursion Become aware of the STL class stack.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Chapter 7' - taliesin

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Chapter 7

Stacks

Data Structures Using C++

Chapter Objectives
• Examine various stack operations
• Learn how to implement a stack as an array
• Learn how to implement a stack as a linked list
• Discover stack applications
• Learn to use a stack to remove recursion
• Become aware of the STL class stack

Data Structures Using C++

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 (especially not tail recursive) into nonrecursive algorithms

Data Structures Using C++

Various Types of Stacks

Data Structures Using C++

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++

Empty Stack

Data Structures Using C++

Stack Operations

Data Structures Using C++

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++

Basic Operations on a Stack
• 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++

Basic Operations on a Stack
• 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.

Data Structures Using C++

Example of a Stack

Data Structures Using C++

Empty Stack

Data Structures Using C++

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++

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++

Push

Data Structures Using C++

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++

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++

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++

Pop

Data Structures Using C++

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++

Copy Constructor

template<class Type>

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

{

list = NULL;

copyStack(otherStack);

}//end copy constructor

Data Structures Using C++

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++

#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++

Programming Example: Highest GPA

Input The program reads an input file consisting of each student’s GPA, followed by the student’s name. Sample data is:

3.8 Lisa

3.6 John

3.9 Susan

3.7 Kathy

3.4 Jason

3.9 David

3.4 Jack

Data Structures Using C++

Programming Example: Highest GPA (Algorithm)
• Declare the variables.
• Open the input file.
• If the input file does not exist, exit the program.
• Set the output of the floating-point numbers to a fixed decimal format with a decimal point and trailing zeroes. Also, set the precision to two decimal places.
• Read the GPA and student name.
• highestGPA = GPA;
• Initialize the stack.

Data Structures Using C++

Programming Example: Highest GPA (Algorithm)
• while (not end of file)

{

8.1 if (GPA > highestGPA)

{

8.1.1 destroyStack(stack);

8.1.2 push(stack, student name);

8.1.3 highestGPA = GPA;

}

8.2 else

if(GPA is equal to highestGPA)

push(stack, student name);

8.3 Read the GPA and student name;

}

Data Structures Using C++

Programming Example: Highest GPA (Algorithm)
• Output the highest GPA.
• Output the names of the students having the highest GPA.

Data Structures Using C++

Programming Example: Highest GPA (Sample Run)

3.4 Holt

3.2 Bolt

2.5 Colt

3.4 Tom

3.8 Ron

3.8 Mickey

3.6 Pluto

3.5 Donald

3.8 Cindy

3.7 Dome

3.9 Andy

3.8 Fox

3.9 Minnie

2.7 Goofy

3.9 Doc

3.4 Danny

Data Structures Using C++

Programming Example: Highest GPA (Sample Run)

Output

Highest GPA = 3.90

The students holding the highest GPA are:

Doc

Minnie

Andy

Data Structures Using C++

Data Structures Using C++

Default Constructor

template<class Type> //default constructor

{

stackTop = NULL;

}

Data Structures Using C++

Destroy Stack

template<class Type>

{

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

//to the next node

delete temp; //deallocate the memory

//occupied by temp

}

}//end destroyStack

Data Structures Using C++

initializeStack and isStackEmpty

template<class Type>

{

destroyStack();

}

template<class Type>

{

return(stackTop == NULL);

}

template<class Type>

{

return false;

Data Structures Using C++

Push

Stack before the push operation

Stack and newNode

Data Structures Using C++

Push

Stack after the statement newNode->link = stackTop; executes

Stack after the statement stackTop = newNode; executes

Data Structures Using C++

template<class Type>

{

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

//terminate the program

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

}//end top

Data Structures Using C++

Pop

Stack before the popoperation

Data Structures Using C++

Pop

Stack after the statements temp = stackTop;

Stack after the statement delete temp; executes

Data Structures Using C++

Application of Stacks:Postfix Expression Calculator
• Prefix/Polish Notation
• Suffix/Postfix/Reverse Polish Notation

Data Structures Using C++

Application of Stacks:Postfix Expression Calculator

Stack after pushing 6

Stack after retrieving the top two elements and popping twice

Stack after pushing 3

Stack after pushing the result of op1 + op2, which is 9

Data Structures Using C++

Application of Stacks:Postfix Expression Calculator

Stack after pushing 2

Stack after pushing the result of op1 * op2, which is 18

Stack after retrieving the top two elements and popping twice

Stack after popping the element

Data Structures Using C++

Nonrecursive Algorithm to Print Linked List

current = first; //Line 1

while(current != NULL) //Line 2

{

stack.push(current); //Line 3

}

Data Structures Using C++

Data Structures Using C++

STL class stack (Stack Container Adapter)
• Standard Template Library (STL) provides a class to implement a stack in a program
• Name of the class defining a stack is stack
• Name of the header file containing the definition of the class stack is stack

Data Structures Using C++

Operations on a stack Object

Data Structures Using C++

Chapter Summary
• Stack Data Structure
• Last In First Out (LIFO)
• Stacks Implemented as Arrays
• Stacks Implemented as Linked Lists
• Postfix Expression Calculator
• Nonrecursive Algorithm to Print Linked List
• STL class stack

Data Structures Using C++