Stack & queue

19
Stack & Queue

description

stack operation

Transcript of Stack & queue

Page 1: Stack & queue

Stack & Queue

Page 2: Stack & queue

A computer often need to perform a particular subtask using the familiar subroutine structure.

In order to maintain information and linkage between main program and subroutine, a data structure called stack is used.

Page 3: Stack & queue

Subroutine

Page 4: Stack & queue

STACK

A stack is a list of data elements, usually words or bytes, with accessing restriction that elements can be added (or) removed at one end of the list is called top of the stack.

Other end is called bottom of the stack.The structure is sometimes referred to as a pushdown stack.

Page 5: Stack & queue

Example

Image processingPattern retrievalParallel processing

Page 6: Stack & queue

Another descriptive

LIFO-the last data placed on the stack is the first one removed when retrieval begins.

Push & Pop- used to placing a new item on the stack and removing the top item from the stack.

Page 7: Stack & queue

Assume data stored in stack

First element is placed in location BOTTOM, when new elements are pushed onto the stack, they are store successive lower address locations.

We use a stack that grows in the direction of decreasing memory addresses.

Page 8: Stack & queue

example

Stack of word items in the memory of a computer.

It contain numerical values, with 43 at the bottom and -28 at the top.

SP- A processor register point the top of the stack.

32 bit word length and byte addressable.

Page 9: Stack & queue
Page 10: Stack & queue

Push operation

Subtract #4, SPMove NEWITEM, (SP)

The subtract instruction subtracts the source operand 4 from the destination operand contained in SP and place the result in SP.

These two instructions move the word from location NEWITEM onto the top of the stack, by decrementing the stack pointer by 4 before the move.

Page 11: Stack & queue
Page 12: Stack & queue

POP operation

Move (SP), ITEM Add #4,SP

These two instruction move the top value from stack into location ITEM and then increment the stack pointer by 4, now SP point to the new top element.

Page 13: Stack & queue

Auto increment or decrement

Move NEWITEM, -(SP) -Push operationMove (SP)+, ITEM - POP operation

Page 14: Stack & queue

concept

When stack is used in a program, it is usually allocated a fixed amount of space in the memory.

We must avoid push an item onto the stack when the stack has reached its max size.

We also avoid pop an item when the stack off an empty stack.

Which could result from a programming error.

Page 15: Stack & queue

example

Stack runs from location – 2000(Bottom)And no further than location 1500Recall that SP is decremented by 4 before

new data are stored on the stack.Hence, an initial value of 2004 means that

the first item pushed onto the stack will be at location 2000.

Page 16: Stack & queue

Prevent push or pop on empty stack

Page 17: Stack & queue
Page 18: Stack & queue
Page 19: Stack & queue