Stack as an Abstract Data Type, Representation of Stacks through Arrays



Stack ADT

View of stack

  • In Stack ADT Implementation instead of data being stored in each node, the pointer to data is stored.
  • The program allocates memory for the data and address is passed to the stack ADT.
  • The head node and the data nodes are encapsulated in the ADT. The calling function can only see the pointer to the stack.
  • The stack head structure also contains a pointer to top and count of number of entries currently in stack.
  • push() – Insert an element at one end of the stack called top.
  • pop() – Remove and return the element at the top of the stack, if it is not empty.
  • peek() – Return the element at the top of the stack without removing it, if the stack is not empty.
  • size() – Return the number of elements in the stack.
  • isEmpty() – Return true if the stack is empty, otherwise return false.
  • isFull() – Return true if the stack is full, otherwise return false.

Stack is a linear data structure that executes all operations according to the LIFO (Last In First Out) principle. Only one end of the stack, starting from the top, is used for insertion and deletion operations. Push operations and pop operations both involve removing data elements from the top of the stack. Push operations include adding a new element to the top of the stack. Stack implementation using array and stack implementation using linked-list are two data structures you may use to implement the stack in memory.

Implementation of Stack using Array

In doing a representation of a stack using an array, the arrays are used to create the stack. Arrays are used for all activities related to the stack implementation.

Many operations are performed while implementing stack using the array.

  1. Push operation
  2. Pop operation
  3. Peek operation

Push Operation

A push operation occurs when you add an element to the top of the stack. The next two steps are involved in the push operation:

  1. The top variable on the stack should be increased so it may refer to the next memory address.
  2. At the top of the increment, add a data element.

When you attempt to put an element into the stack after it is complete, the stack data structure declares an overflow situation.

Algorithm for push operation
Algorithm for push operation while doing representation of stack using array:

  • Begin
  • If top == n
  • The stack is completely filled
  • top = top + 1
  • Stack (top) = item
  • end

Pop Operation

The term "pop operation" refers to the removal of a data element from the stack data structure. The pop operation has the four phases listed below:

  • Every time you remove an item from the stack, the top variable’s value is increased by one.
  • The variable at the top of the stack is put in a different variable, and its value is then reduced by one.
  • The deleted element that was saved in another variable as a result is returned by the pop operation.
  • When a data element is attempted to be deleted when the stack is already empty, the stack data structure reports an underflow problem.

Algorithm for pop operation
Algorithm for pop operation while doing representation of stack using array:

  • Begin
  • if top = 0
  • stack is completely empty
  • item = stack(top)
  • top = top – 1
  • end

Peek Operation

In peek operations, the topmost data element of the stack is returned without being removed from the stack.
If you attempt to return the top-most element while the stack is empty, underflow circumstances could happen.

Algorithm for peek operation
Algorithm for pop operation while doing representation of stack using array:

  • Begin
  • if top = -1
  • stack is empty
  • item = stack[top]
  • return data
  • end

No comments:

Post a Comment