chapter 7 stacks dr. youssef harrath yharrath@uob.edu.bh

Post on 28-Dec-2015

233 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 7

Stacks

Dr. Youssef Harrath

yharrath@uob.edu.bh

Outline

1. Introduction

2. Implementation of Stacks as Arrays

3. Example: Highest GPA

4. Linked Implementation of Stacks

5. Stack as Derived from the class linkedListType

6. Application of Stacks:

A. Postfix Expression Calculator

B. Print a Linked List Backwards

2Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

1. Introduction

3Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Stack of coins

Stack of books

1. Introduction

4Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

A stack is a list of homogenous elements, wherein the addition

and deletion of elements occurs only at one end (top of the stack).

The bottom element of the stack is the earliest added one.

The top element is the last added item.

The last added item will be the first one to be deleted.

A stack is called a LIFO data structure.

1. Introduction: Stack operations

5Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

A

push box A

AB

push box B

AB

C

push box C

AB

C

Retrieve the top element

AB

pop stack

1. Introduction: Stack operations

6Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

The basic operations on a stack are:

initializeStack: intializes 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.

isFullStack: Checks whether the stack is full.

push: Adds a new element to the top of the stack.

top: returns the top of the stack.

pop: Removes the top of the stack.

2. Implementation of Stacks as Arrays

7Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>class stackType{public:

const stackType<Type>& operator=(const stackType<Type>&);void initializeStack();bool isEmptyStack();bool isFullStack();void destroyStack();void push(const Type& newItem);Type top();void pop();stackType(int stackSize = 100);stackType(const stackType<Type>& otherStack);~stackType();

private:int maxStackSize;int stackTop; // variable to point to the top of the stackType *list;void copyStack(const stackType<Type>& otherStack);

};

[99]

[3]

[2]

[1]

[0]

2. Implementation of Stacks as Arrays

8Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

A

B

C

D

.

.

.

100

4

maxStackSize

stackTop

list

stack

2. Implementation of Stacks as Arrays

9Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void

stackType<Type>::initializeStack()

{

stackTop = 0;

}template<class Type>

void stackType<Type>::destroyStack()

{

stackTop = 0;

}

2. Implementation of Stacks as Arrays

10Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

bool stackType<Type>::isFullStack()

{

return(stackTop == maxStackSize);

}

template<class Type>

bool stackType<Type>::isEmptyStack()

{

return(stackTop == 0);

}

2. Implementation of Stacks as Arrays: push

11Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Store newItem in the array component indicated by stackTop.

Increment stackTop.

S

.

.

.

[99]

[3]

[2]

[1]

[0]

100

4

maxStackSize

stackTop

list

stack

u

n

n

S

.

.

.

[99]

[4]

[3]

[2]

[1]

[0]

100

5

maxStackSize

stackTop

list

stack

u

n

n

y

push(‘y’)

2. Implementation of Stacks as Arrays: push

12Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

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

{

if(!isFullStack())

{

list[stackTop] = newItem;

stackTop++;

}

else

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

}

2. Implementation of Stacks as Arrays: top

13Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

Type stackType<Type>::top()

{

assert(stackTop != 0);

return list[stackTop - 1];

}

2. Implementation of Stacks as Arrays: pop

14Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

B

.

.

.

[99]

[3]

[2]

[1]

[0]

100

4

maxStackSize

stackTop

list

stack

o

l

d

B

.

.

.

[99]

[3]

[2]

[1]

[0]

100

3

maxStackSize

stackTop

list

stack

o

l

d

pop()

2. Implementation of Stacks as Arrays: pop

15Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void stackType<Type>::pop()

{

if(!isEmptyStack())

stackTop--;

else

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

}

2. Implementation of Stacks as Arrays: copyStack

16Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

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

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

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

}

2. Implementation of Stacks as Arrays: constructor

17Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>stackType<Type>::stackType(int stackSize)

{if(stackSize <= 0){

cout<<"The size must be positive!"<<endl;maxStackSize = 100;

}else

maxStackSize = stackSize;stackTop = 0;list = new Type[maxStackSize];assert(list != NULL);

}

2. Implementation of Stacks as Arrays: destructor & copy constructor

18Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

stackType<Type>::~stackType()

{

delete [] list;

}

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

{

list = NULL;

copyStack(otherStack);

}

2. Implementation of Stacks as Arrays: overloading(=)

19Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

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

(const stackType<Type>&

otherStack)

{

if(this != &otherStack)

copyStack(otherStack);

return *this;

}

3. Example: Highest GPA

20Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Write a C++ program that reads a data file consisting of

each student’s GPA followed by the student’s name. The

program then prints the highest GPA and the names of

all the students who received that GPA. The program

scans the input file only once.3.9 Marwa3.1 Ali3.9 Ahmad2.1 Salma3.9 Zahra1.8 Salah

3. Example: Highest GPA: Algorithm

21Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

• Read the 1st GPA, the name of the 1st student, and assign the 1st

GPA to the highestGPA.

• Read the 2nd GPA, the name of the 2nd student. We compare the

second GPA with the highestGPA:

1. The new GPA > highestGPA

a. Update the value of the highestGPA

b. Destroy the stack

c. Insert the name of the new student into the stack.

2. The new GPA = highestGPA: add the name to the stack

3. The new GPA < highestGPA: we do nothing

• Read the next GPA and student name and repeat the steps 1, 2,

and 3 until we rich the end of the file.

3. Example: Highest GPA: Algorithm

22Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

1. Declare the variables

2. Open the input file (treat the exception case)

3. Read the GPA and the student name

4. highestGPA = GPA

5. Initialize the stack

6. while(not end of file)

1. if(GPA > highestGPA)

1. destroyStack(stack)

2. push(stack, student name)

3. highestGPA = GPA

2. else if (GPA = highestGPA)

3. Read the GPA and student name

7. Output the highest GPA

8. Output the names of the students having the highest GPA

3. Example: Highest GPA: Algorithm

23Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Program

4. Linked Implementation of Stacks: PartI

24Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>struct nodeType {Type info;nodeType<Type> link;}template<class Type>class linkedStackType {public:const linkedStackType <Type>& operator=(const linkedStackType <Type>&);void initializeStack();bool isEmptyStack();bool isFullStack();void destroyStack();void push(const Type& newItem);Type top();…

4. Linked Implementation of Stacks: Part II

25Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

void pop();

linkedStackType();

linkedStackType(const linkedStackType <Type>& otherStack);

~ linkedStackType ();

private:

nodeType <Type> *stackTop;

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

};

4. Linked Implementation of Stacks

26Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

stackTop

stack

Empty linked stack

stackTop

stack

Nonempty linked stack

C

B

A

4. Linked Implementation of Stacks: default constructor

27Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

linkedStackType<Type>::linkedStackType()

{

stackTop = NULL;

}

4. Linked Implementation of Stacks: destroyStack

28Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void linkedStackType<Type>::destroyStack()

{

nodeType<Type> *temp;

while(stackTop != NULL)

{

temp = stackTop;

stackTop = stackTop->link;

delete temp;

}

}

4. Linked Implementation of Stacks: initializeStack

29Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void linkedStackType<Type>::initializeStack()

{

destroyStack();

}

4. Linked Implementation of Stacks: isEmptyStack & isFullStack

30Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

bool linkedStackType<Type>::isEmptyStack()

{

return (stackTop == NULL);

}

template<class Type>

bool linkedStackType<Type>::isFullStack()

{

return false;

}

4. Linked Implementation of Stacks: push

31Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

stackTop

stack

C

B

A

push(D)

stackTop

stack

C

B

A

DnewNode

4. Linked Implementation of Stacks: push

32Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void linkedStackType<Type>::push(const Type& newElement)

{

nodeType<Type> *newNode;

newNode = new nodeType<Type>;

assert(newNode != NULL);

newNode->info = newElement;

newNode->link = stackTop;

stackTop = newNode;

}

4. Linked Implementation of Stacks: top

33Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

Type linkedStackType<Type>::top()

{

assert(newNode != NULL);

return stackTop->info;

}

4. Linked Implementation of Stacks: pop

34Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

stackTop

stack

C

B

A

pop()

stackTop

stack C

B

A

temp

4. Linked Implementation of Stacks: pop

35Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

template<class Type>

void linkedStackType<Type>::pop()

{

nodeType<Type> *temp;

if(stackTop != NULL)

{

temp = stackTop;

stackTop = stackTop->link;

delete temp;

}

else

cout<<“cannot delete from an empty stack!”<<endl;

}

5. Stack as Derived from the class linkedListType

36Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

push insertFirst

initializeStack

isEmptyStack

initializeList

isEmptyList

The class linkedStackType can be derived from the class linkedListType

5. Stack as Derived from the class linkedListType

37Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Program

• The operator is after/before the operands

• Left to right evaluation

• No precedence

• No need for ()

6. Application of Stacks: Postfix Expression Calculator

38Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Infix notation

a + b a b + + a b

Postfix notation Prefix notation

• The operator is between

the operands

• Left to right evaluation

• Precedence

• ()

Lukasiewicz (Poland, 1950)

6. Application of Stacks: Postfix Expression Calculator

39Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Infix Expression

a + b

a + b * c

a * b + c

(a + b) * c

(a - b) * (c + d)

(a + b) * (c - d / e) + f

a b – c d + *

Equivalent Postfix Expression

a b +

a b c * +

a b * c +

a b + c *

a b + c d e / - * f +

6. Application of Stacks: Postfix Expression Calculator

40Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Infix

(6 + 3) * 2 =

Postfix

6 3 + 2 * =

6

push(6)

6

pop()

9

push(9)

18

push(18)

3

6

push(3)

9

2

push(2)pop() pop()

9

pop()

6. Application of Stacks: Postfix Expression Calculator

41Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

Program will be given as a lab assignment

6. Application of Stacks: Print a Linked List Backwards

42Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

10 155 20first

stackTopstack

10

15

5

20

6. Application of Stacks: Print a Linked List Backwards

43Dr. Youssef Harrath – University of Bahrain – ITCS 215 – 2010/2011

current = first;

while(current != NULL)

{

stack.push(current);

current = current->link;

}

Step 1: save the addresses of the nodes in a stack.

while(!stack.isEmpty())

{

current = stack.top();

stack.pop();

cout<<current->info<<“ “;

}

Step 2: print the linked list in reverse order.

top related