Data Structures and Algorithms - Old Questions

1. Define stack as ADT. Describe its primitive operations on Array implementation and linked list implementation.

10 marks | Asked in 2067

stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end, called the top of the stack. The deletion and insertion in a stack is done from top of the stack.

 It is a linear data structure which store data temporarily to perform PUSH and POP operation in LIFO (Last-In-First-Out) sequence.


C Stack Using Array

Fig::A stack containing items or elements

Stack as ADT

stack of elements of type is a finite sequence of elements of together with the operations

  • CreateEmptyStack(S): Create or make stack be an empty stack
  • Push(S, x): Insert at one end of the stack, called its top
  • Top(S): If stack is not empty; then retrieve the element at its top
  • Pop(S): If stack is not empty; then delete the element at its top
  •  IsFull(S): Determine if is full or not. Return true if is full stack; return false otherwise
  • IsEmpty(S): Determine if is empty or not. Return true if is an empty stack; return false otherwise.

Thus by using a stack we can perform above operations thus a stack acts as an ADT.


Primitive operations of stack

The following operations can be performed on a stack:

1. PUSH operation: The push operation is used to add (or push or insert) elements in a Stack.

ü  When we add an item to a stack, we say that we push it onto the stack

ü  The last item put into the stack is at the top


2. POP operation: The pop operation is used to remove or delete the top element from the stack.

  üwe remove an item, we say that we pop item from the stack.

 üWhen an item popped, it is always the top item which is removed.


Assembly language | the stack

 

The PUSH and the POP operations are the basic or primitive operations on a stack. Some others operations are:

  • CreateEmptyStack operation: This operation is used to create an empty stack.
  • IsFull operation: The isfull operation is used to check whether the stack is full or not ( i.e. stack overflow)
  •  IsEmpty operation: The isempty operation is used to check whether the stack is empty or not. (i. e. stack underflow)
  • Top operations: This operation returns the current item at the top of the stack, it doesn’t remove it