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.
Subroutine
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.
Example 
Image processing 
Pattern retrieval 
Parallel processing
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.
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.
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.
Push operation 
Subtract #4, SP 
Move 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.
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.
Auto increment or decrement 
Move NEWITEM, -(SP) -Push operation 
Move (SP)+, ITEM - POP operation
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.
example 
Stack runs from location – 2000(Bottom) 
And no further than location 1500 
Recall 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.
Prevent push or pop on empty stack
Stack & queue
Stack & queue
Stack & queue

Stack & queue

  • 1.
  • 2.
    A computer oftenneed 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.
  • 3.
  • 4.
    STACK  Astack 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.
  • 5.
    Example Image processing Pattern retrieval Parallel processing
  • 6.
    Another descriptive LIFO-thelast 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.
  • 7.
    Assume data storedin 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.
  • 8.
    example Stack ofword 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.
  • 10.
    Push operation Subtract#4, SP Move 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.
  • 12.
    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.
  • 13.
    Auto increment ordecrement Move NEWITEM, -(SP) -Push operation Move (SP)+, ITEM - POP operation
  • 14.
    concept When stackis 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.
  • 15.
    example Stack runsfrom location – 2000(Bottom) And no further than location 1500 Recall 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.
  • 16.
    Prevent push orpop on empty stack