Stack Using Array | Operation on Stack | Stack Program in C

In this article stack using array we give the information about implementation of stack using array, operation on stack, definition of stack in c, introduction to stack and stack program in c.

Stack using array:

Stack is a non-primitive linear data structure. A stack can be using either statically or dynamically. Array is used to implement stack statically and linked list is used to implement stack dynamically.

Introduction to stack:

The important features of stack in data structure for representing things that need to maintain in a particular order. For example, when a function calls another function, which in turn calls a third function, it’s important that the third function return back to the second function rather than the first.

Another example of stack is putting note books one above the other.
Consider this example. There is a pile of 7 note books on a table. You want to add one note book to the pile. What do you do? You simply add the note book on the TOP of the pile. What, if you want the fourth note book from the new 8 note book pile? You then lift each note book one by one from the TOP until the 4th note book reaches the TOP. Then you take the 4th note book and replace all the others back into the pile by adding them from the TOP.

Definition of stack in c:

Stack is a non-primitive linear data structure. In Stack all insertions and deletion are made at one end, called the TOP because Stack is an ordered list. Every time an element is added, it goes on the top of the stack; the only element that can be removed is the element that was at the top of the stack. Thus, the basic working principle of stack is LAST IN FIRST OUT (LIFO).

Operation on Stack:

There are two operations that can be performed on stack; first operation is to add an element in stack and second operation is to remove an element from the stack. This operation named as,
1. PUSH(): – This function is used to add an element into the stack.
2. POP(): – This function is used to remove an element from the stack.
Other than this, in stack we use a pointer called TOP that gets incremented, when we add an element in the stack and decremented when we remove an element from the stack.

Additional operations can be defined:
IsEmpty(): – Reports whether the stack is empty or not.
IsFull():- Reports whether the stack is full or not.

Example:
The following diagrams explain the different operations on stack.

Declaration of stack:

A stack can be implemented either statically or dynamically. Array is used to implement stack statically and linked list is used to implement stack dynamically.
Stack Statically:
A stack is an ordered collection of data, so to implement stack array is required. A stack to be implemented using array requires.
1. A fixed size array to hold elements of stack.
2. An integer that indicate the position of top.

Syntax:
#define MaxSize 100
struct stack
{
int data[MaxSize];
int top;
};
This represents only template of stack. Actually stack is declared as
struct stack s;
when stack variable is declared, the integer top has to be initialized to indicate that stack is empty. Hence, to indicate an empty stack, top has to be initialized to -1.

 PUSH and POP Functions:

PUSH( ) function is used to add an element into the stack before inserting an element we must check whether stack is full or not. This is done using IsFull function. If the total elements in the stack are equal to the MaxSize of an array, then IsFull function returns TRUE otherwise it returns FALSE.

int IsFull( )

{

            if ( s.top = = MaxSize – 1)

                        return 1;

            else

                        return 0;

}

Implementation of PUSH function is as follows:

void PUSH ( )

{

            if ( IsFull() )

            {

                        printf(“\n Stack Overflow… ”);

            }

            else

            {

                        printf(“\n Enter element :”);

                        scanf(“%d”,&x);

                        s.top = s.top + 1;

                        s.data[s.top] = x;

            }

}

POP ( ) Function:

It is used to remove an element from the stack. Before removing an element we must check; whether stack is empty or not. This is done using IsEmpty function. If the elements in the stack are equal to zero, then IsEmpty function returns TRUE otherwise it returns FALSE.

int IsEmpty( )

{

int ( s.top = = -1)

                        return 1;

else

                        return 0;

}

Implementation of POP function is as follows:

void POP( )

{

            if ( IsEmpty() )

            {

                        printf( “ \n Stack Underflow…” );

            }

            else

            {

                        x=s.data[s.top];

                        printf(“ \n Removed element is %d: ”, x);

                        s.top = s.top-1;

            }

}

Stack program in c:

// This program implements the PUSH and POP operations on stack

#include<stdio.h>

#include<conio.h>

#define MAX 6

struct stack

{

            int data[MAX];

            int top;

}p;

void main()

{

int ch, y, i;

void PUSH(int y);

int POP();

clrscr();

p.top= -1;

while(1)

{

            printf(“\n 1: PUSH \n 2: POP \n 3: Display \n 4: Exit”);

            printf(“\n Enter your choice :”);

            scanf(“%d”,&ch);

switch(ch)

{

            case 1:

                        if(IsFull())

                        {

                                    printf(“\n Stack Overflow….”);

                        }

                        else

                        {

                                    printf(“\n Enter data to PUSH: ”);

                                    scanf(“%d”,&y);

                                    PUSH(y);

                        }

                        break;

            case 2:

                        if(IsEmpty())

                        {

                                    printf(“\n Stack Underflow…”);

                        }

                        else

                        {

                                    y=POP();

                                    printf(“\n Poped element : %d”,y);

                        }

                        break;

case 3:

            if(!IsEmpty())

            {

                        printf(“\n Elements in stack are: ”);

                        for(i=p.top;i>=0;i–)

                                    printf(“\n %d”,p.data[i]);

            }

            else

            printf(“\n Stack underflow….”);

            break;

case 4:

            exit(0);

default:

            printf(“\n Please Enter correct Choice…”);

}

}

}

int IsFull()

{

            if(p.top==MAX-1)

                        return(1);

            else

                        return(0);

}

int IsEmpty()

{

            if(p.top==-1)

                        return (1);

            else

                        return (0);

}

void PUSH(int y)

{

            p.top++;

            p.data[p.top]=y;

}

int POP()

{

            p.top–;

return(p.data[p.top+1]);

}

Related Link:

Some More: DBMS/ WT/ DMDW

Santosh Nalawade

Work as Assistant Professor and Web Developer.

Leave a Reply

Your email address will not be published. Required fields are marked *