Stack
Lecture : 3rd
What is a Stack?
 Stack is linear data structure. By linear means that
elements are stored in continuous memory location in
computer.
 It is an ordered group of homogeneous items
of elements.
 A stack is a list of elements in which an element may
be inserted or deleted only at the one end , which is
called top stack in which they are inserted into stack.
 It means that element of stack can be removed
reverse order, in which they are inserted into stack.
Stack…
 Insertion of data or element into the stack top is
called PUSH. And deletion of data or element from
stack top is called POP.
 The items in the stack are stored and retrieved in
LIFO and FILO manner.
 Another name that is used for stack is Push Down
list.
3
Stack…
4
Stack…
5
Features of stacks
 Dynamic data structures
 Do not have a fixed size
 Do not consume a fixed amount of memory
 Size of stack changes with
each push() and pop() operation.
 Each push() and pop() operation increases and
decreases the size of the stack by 1, respectively.
6
Operations on stack
 The following operation can be performed on the stack
 Create Stack- This operation creates an empty stack.
 push – When insert an item at the top of the stack is called PUSH.
 pop – When remove item from the top of the stack is called POP.
 Empty – return true if stack “S” contains no element, other wise
return false
 Full- The stack is full
Overflow and Underflow
Sometimes data are inserted into a
stack data structure but there is no
available space. – This situation is
called overflow
Sometime delete data or item from
stack data structure that is empty. This
situation is called underflow
8
Push Operation:
 It is a term used to insert element into a
stack .
 The steps are as follows:
Access the top or stack pointer.
If stack is full (overflow), refuse entry.
Increment the top or stack pointer.
Store the data at location specified by the
top or stack pointer.
9
1) Algorithm for PUSH operation
 ALGORITHM: PUSH (S,TOP,ITEM,N)
 The algorithm is used to insert an element “ITEM” in stack
“S”.TOP is the top pointer location in Stack. “N” is the number
of memory locations.
Step #01: [Check for overflow]
If(TOP>= N) then
Write(“Overflow or Stack is full”)
Return null or exit;
 Step # 02: [Incremented TOP]
TOP= TOP+1
 Step # 03: [Insert Value]
S[TOP]=New ITEM
 Step # 04: [Finish]
Exit
10
Pop Operation:
It is a term used to delete or remove an
element from a stack .The steps are as
follows:
Check that stack is empty(underflow),
refuse pop.
Remove the top element from stack
Decrement the top or stack pointer.
11
2) Algorithm for POP operation
 ALGORITHM: POP (S,TOP)
 The algorithm is used to delete an element from stack “S”.TOP is the top
pointer location in Stack. “N” is the number of memory locations. TEMP
is temporary variable where we save the deleted element.
Step #01: [Check for underflow]
If(TOP< 1) then
Write(“Underflow or Stack is empty”)
Return null or exit;
 Step # 02: [Delete item]
TEMP= S[TOP]
 Step # 03: [Decrement top pointer]
TOP = TOP -1
 Step # 04: [Finish]
Exit
12
Assignment
 C++ program for Push and PoP operation of stack
13

Data structures & Algorithmsssss -03.ppt

  • 1.
  • 2.
    What is aStack?  Stack is linear data structure. By linear means that elements are stored in continuous memory location in computer.  It is an ordered group of homogeneous items of elements.  A stack is a list of elements in which an element may be inserted or deleted only at the one end , which is called top stack in which they are inserted into stack.  It means that element of stack can be removed reverse order, in which they are inserted into stack.
  • 3.
    Stack…  Insertion ofdata or element into the stack top is called PUSH. And deletion of data or element from stack top is called POP.  The items in the stack are stored and retrieved in LIFO and FILO manner.  Another name that is used for stack is Push Down list. 3
  • 4.
  • 5.
  • 6.
    Features of stacks Dynamic data structures  Do not have a fixed size  Do not consume a fixed amount of memory  Size of stack changes with each push() and pop() operation.  Each push() and pop() operation increases and decreases the size of the stack by 1, respectively. 6
  • 7.
    Operations on stack The following operation can be performed on the stack  Create Stack- This operation creates an empty stack.  push – When insert an item at the top of the stack is called PUSH.  pop – When remove item from the top of the stack is called POP.  Empty – return true if stack “S” contains no element, other wise return false  Full- The stack is full
  • 8.
    Overflow and Underflow Sometimesdata are inserted into a stack data structure but there is no available space. – This situation is called overflow Sometime delete data or item from stack data structure that is empty. This situation is called underflow 8
  • 9.
    Push Operation:  Itis a term used to insert element into a stack .  The steps are as follows: Access the top or stack pointer. If stack is full (overflow), refuse entry. Increment the top or stack pointer. Store the data at location specified by the top or stack pointer. 9
  • 10.
    1) Algorithm forPUSH operation  ALGORITHM: PUSH (S,TOP,ITEM,N)  The algorithm is used to insert an element “ITEM” in stack “S”.TOP is the top pointer location in Stack. “N” is the number of memory locations. Step #01: [Check for overflow] If(TOP>= N) then Write(“Overflow or Stack is full”) Return null or exit;  Step # 02: [Incremented TOP] TOP= TOP+1  Step # 03: [Insert Value] S[TOP]=New ITEM  Step # 04: [Finish] Exit 10
  • 11.
    Pop Operation: It isa term used to delete or remove an element from a stack .The steps are as follows: Check that stack is empty(underflow), refuse pop. Remove the top element from stack Decrement the top or stack pointer. 11
  • 12.
    2) Algorithm forPOP operation  ALGORITHM: POP (S,TOP)  The algorithm is used to delete an element from stack “S”.TOP is the top pointer location in Stack. “N” is the number of memory locations. TEMP is temporary variable where we save the deleted element. Step #01: [Check for underflow] If(TOP< 1) then Write(“Underflow or Stack is empty”) Return null or exit;  Step # 02: [Delete item] TEMP= S[TOP]  Step # 03: [Decrement top pointer] TOP = TOP -1  Step # 04: [Finish] Exit 12
  • 13.
    Assignment  C++ programfor Push and PoP operation of stack 13