Implementation of Stack Using Array

Implementation of Stack Using Array

STACK

The stack is an ordered list in which all insertions and deletions are made at one end. This is called the top.

If the elements are E, D, C, B, A (in order ), then first element to be removed from the stack is A.

Stacks are also called LIFO (last in first out). This means that the element that is inserted at first in the list, is deleted from the list at first.

Examples of the Stack: a stack of books, a deck of cards or a pile of plates, etc.

A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack can either be a fixed size one or it may have a sense of dynamic resizing.

Here, we are going to implement stack using arrays, which makes it a fixed size stack implementation. Also, the simplest way to implement a stack is to use a one dimensional array of n elements.

In stack terminology, insertion operation is called PUSH operation and delete operation is called POP operation.

Stack operations may involve initializing the stack, using it and then de-initializing it.

Apart from these basic operations, a stack is used for the following two primary operations −

  • push() − Pushing (storing) an element on the stack.
  • pop() − Removing (accessing) an element from the stack.

The functions used in the implementation of the stack are:

  1. The Push function checks whether the stack is full or not. If the stack is full, this condition is known as overflow but if not it takes input from the user and pushes it to the top of the stack.
  2. The Pop function checks whether the stack is empty or not. If the stack is empty, this condition is known as underflow else the topmost item is deleted from the stack.

ALGORITHM:

Var stack = array [ 1.n ] of items top : : 0 .. n

Program 1: Push an item onto the stack

Procedure add ( item : items )

{add item to the global stack;

top is the current top of stack

and is its maximum size}

Program 2: Pop an item from the stack

Procedure delete ( var item : items )

{remove top element from the stack and put it in item}

C PROGRAM TO IMPLEMENT STACK USING ARRAY

The source code of C PROGRAM for the implementation of the stack using array is given below:

Similar Problems

Linked List Creation – Algorithm and Code in Java

%d bloggers like this: