IMPLEMENTATION OF STACK ADT USING ARRAY


SOURCE CODE:

“filo.h” File:

# include <stdio.h>
# include <conio.h>
struct stack
{
   int capacity,size,top;
   int*array;
};
typedef struct stack *STACK;

STACK create(int max)
{
   STACK S;
   S=(struct stack*) malloc(sizeof (struct stack));
   if(S==NULL)
      printf("\nFatal Error");
   else
   {
      S->capacity=max;
      S->size=0;
      S->top=-1;
      S->array=(int*)malloc(sizeof(int)*max);
      if(S->array==NULL)
     printf("\nFatal Error");
   }
   return S;
}
void Push(int X,STACK S)
{
   if(Isfull(S))
      printf("\nstack is full");
   else
   {
      S->top++;
      S->array[S->top]=X;
      S->size++;
   }
}
void Pop(STACK S)
{
   if(Isempty(S))
      printf("\nSTACK is empty");
   else
   {
      S->top--;
      S->size--; 
   }
}

void display(STACK S)
{
   int I;
   for(I=0;I<=S->top;I++)
      printf("\n%d",S->array[I]);
}
int Isempty(STACK S)
{
   return S->top==-1;
}
void makeempty(STACK S)
{
   S->top=-1;
}
int Isfull(STACK S)
{
   return S->size==S->capacity;
}
int Top(STACK S)
{
   return S->array[S->top];
}
STACK disposestack(STACK S)
{
   makeempty(S);
   free(S->array);
   free(S);
   S=NULL;
   return S;
}

“filo.c” File:

#include<stdio.h>
#include<conio.h>
#include "filo.h"
void main()
{
STACK S=NULL;
int n,max,I,s,t;
clrscr();
printf("\n\n1.Create a stack \n2.insert \n3.delete \n4.display \n5.Isempty \n6.makeempty     \n7.Isfull \n8.Top\n 9.disposestack\n 10.exit\n");
x:
printf("\nenter your choice:\t");
scanf("%d",&n);
switch(n)
{
case 1:
    if(S==NULL)
    {
               printf("\nenter size of stack");

       scanf("%d",&max);
       S=create(max);
    }
    else
       printf("\nstack is already created");
    break;
case 2:
    if(S==NULL)
       printf("n\stack is not created");
    else
    {
       printf("\nEnter the element");
       scanf("%d",&I);
       Push(I,S);
    }
    break;
case 3:
    if (S==NULL)
       printf ("\nstack is not created");
    else
      Pop(S);
    break;
case 4:
    if (S==NULL)
      printf("n\stack not yet created");
    else
      display(S);
    break;
case 5:
    if (S==NULL)
      printf ("\nstack is not yet created");
    else
    {
      if(Isempty(S))
         printf ("\nstack is empty");
      else
         printf("\nstack is not empty");
    }
    break;
case 6:
    if (S==NULL)
       printf ("\nStack is not created");
    else
       makeempty(S);
    break;
case 7:
    if (S==NULL)
       printf ("\nStack is not created");
    else
    {
    if(Isfull(S))
       printf ("\nstack is full");

    else
       printf ("\nstack is not full");
    }
    break;
case 8:
    if (S==NULL)
       printf ("\nStack is not created");
    else
    {
    t=Top(S);
    if(t==-1)
       printf ("\nstack is empty");
    else
       printf("\nThe top is%d",t);
    }
    break;
case 9:
    if (S==NULL)
       printf ("\nstack is empty");
    else
    {
       S=disposestack(S);
       printf ("\nstack is dispose");
    }
    break;
case 10:
    exit(0);
    break;
default:
    printf("\n******Wrong Entry******");
}
goto x;
}

OUTPUT:

1.Create a stack
2.insert
3.delete
4.display
5.Isempty
6.makeempty
7.Isfull
8.Top
9.disposestack
10.exit
Enter your choice:    1
Enter the size of Stack:    5

Enter your choice:    2
Enter the element    100

Enter your choice:    2
Enter the element    200

Enter your choice:    2
Enter the element    300

Enter your choice:    2
Enter the element    400

Enter your choice:    2
Enter the element    500

Enter your choice:    2
Enter the element    600
Stack is full

Enter your choice:    4
100
200
300
400
500

Enter your choice:    5
Stack is not empty

Enter your choice:    8
The Top is 500

Enter your choice:    3

Enter your choice:    4
100
200


300
400

Enter your choice:    3

Enter your choice:    4
100
200
300

Enter your choice:    8
The Top is 300

Enter your choice:    7
Stack is not full

Enter your choice:    6
Now stack is empty

Enter your choice:    9
Stack is disposed

Enter your choice:    4
Stack is not yet created

Enter your choice:    12
            **********WRONG ENTRY**********

Enter your choice:    10
Previous
Next Post »

Still not found what you are looking for? Try again here.