stack & queue
DESCRIPTION
stack operationTRANSCRIPT
![Page 1: Stack & queue](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/1.jpg)
Stack & Queue
![Page 2: Stack & queue](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/3.jpg)
Subroutine
![Page 4: Stack & queue](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/5.jpg)
Example
Image processingPattern retrievalParallel processing
![Page 6: Stack & queue](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/9.jpg)
![Page 10: Stack & queue](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/11.jpg)
![Page 12: Stack & queue](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/13.jpg)
Auto increment or decrement
Move NEWITEM, -(SP) -Push operationMove (SP)+, ITEM - POP operation
![Page 14: Stack & queue](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/16.jpg)
Prevent push or pop on empty stack
![Page 17: Stack & queue](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/17.jpg)
![Page 18: Stack & queue](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/18.jpg)
![Page 19: Stack & queue](https://reader036.vdocuments.us/reader036/viewer/2022082702/5561ef30d8b42a9d068b55a7/html5/thumbnails/19.jpg)