23
DIT 2014 1  Chapter 4 : Stack Shazana Md Zin, FTMM (2010)

Chapter 4- Stack

Embed Size (px)

Citation preview

Page 1: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 1/23

DIT 2014

1

 Chapter 4 : Stack 

Shazana Md Zin, FTMM (2010)

Page 2: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 2/23

What is Stack?2

Page 3: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 3/23

Stack related term

A memory portion, which is used for storing the elements.

Based on principle of Last-In-First-Out (LIFO).

TOP  the pointer points to the top element in the stack.

Stack Underflow  there is no element in the stack.

Stack Overflow the stack contains equal or no more

elements to be added.

PUSH inserting new element to the top of the stack.

POP removing one element from the top of the stack.

3

Page 4: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 4/23

Introduction4

Page 5: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 5/23

Introduction 

Page 6: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 6/23

Introduction6

Page 7: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 7/23

Basic Stack Operations7

There is no need to search or remove an

arbitrary node

The last element added is the first to removed

 push  Adds an item to the top of a stack.

 pop  Removes an item from the top of the stack

and returns it to the user.

stack top   Copies the top item of the stack and

returns it to the user; the item is not removed,

hence the stack is not altered.

Page 8: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 8/23

Last - In- First - Out

8

The last element added is the first to beremoved

Example The stack of rice bowls in a

cupboard, the stack of books.

Page 9: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 9/23

Process9

Example:

Push books

Red Book

Blue Book

Green Book

Yellow Book

Pop books

Yellow Book Green Book

Blue Book

Red Book

Page 10: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 10/23

10

Create Stack size = 4

3

2

1

0

top = -1

Stack Operation

Page 11: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 11/23

11

Make sure the stack is empty@not  before pop

If 0 => cannot pop

3

2

1

0

top =-1

Stack Operation

Page 12: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 12/23

12

Make sure the stack is empty@not  before push

If stack is full => cannot push

3

2

1

0

top = 3

Stack Operation

Page 13: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 13/23

13

Push data into the stack

If stack full => push to add a node

3

2

1

0

-1021top3

top

Stack Operation

Page 14: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 14/23

14

Pop data from the stack

If stack empty => pop to remove a node

3

2

1

0

top

3021top -1

Stack Operation

Page 15: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 15/23

Page 16: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 16/23

top list

-1

3

2

1

0

v oid create(stack *t)

{

t->top = -1;

}

Create Stack

Page 17: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 17/23

int empty(stack *t)

{if(t->top == -1)

return (1);

else

return(0);

}

int full(stack *t)

{

if (t->top ==3)

return (1);else

return (0);

}

Overflow and Underflow Controller

Page 18: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 18/23

v oid push(stack *t)

{

int data;

if (full(t) == 1)

cout<<³Stack is Full´<<endl;

else

{

cout<<³Push Data : ³<<endl;

cin>> data;

t->top++;

t->list[t->top] = data;

}

}

top list

0

3

2

1

0 10

Push Data

Page 19: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 19/23

top list

v oid pop(stack *t){

if(empty(t) == 1)

cout<<³Stack is Empty´<<endl;

else

t->top--;}

-1

3

2

1

0 10

Pop Data

Page 20: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 20/23

Stack Class Implementation

Create the stack

Insert element to the stack PUSH()

Remove element from the stack POP()

Using array and pointer implementation

Page 21: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 21/23

Page 22: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 22/23

Conclusion

Understand the concept of the stack

How to create the stack

Remove your data from the stack POP()

Insert your data to the stack Push()

Implementing ARRAY and LINKED LIST to build

your stack

Page 23: Chapter 4- Stack

8/8/2019 Chapter 4- Stack

http://slidepdf.com/reader/full/chapter-4-stack 23/23

THANK 

YOU