Program To Implement Stack in Array
Program To Implement Stack in Array
scanf("%d",&ch);
switch(ch)
{
case 1:while(cha==1)
{
push();
printf("do you wish to continue?(1/0):");
scanf("%d",&cha);
}
break;
case 2: while(chaa==1)
{
pop();
printf("do you wish to continue?(1/0):");
scanf("%d",&chaa);
}
break;
case 3: printf("\nThe STACK is:\n");
for(int i=top;i>=0;i--)
{
printf("\n%d",stack[i]);
}
break;
case 4: exit(0);
}
getch();
}
}
OUTPUT:
switch ( ch )
{
case 1 :while(ch1==1)
{
enqueue() ;
printf("\nWant to enter more...(1/0)");
scanf("%d",&ch1);
}
break ;
case 2 :while(ch2==1)
{
dequeue() ;
printf("\nWant to enter more...(1/0)");
scanf("%d",&ch2);
}
break ;
case 3 : display() ;
break ;
case 4 : exit(0);
default : printf( "Invalid option\n" ) ;
}
}
while ( ch != 4 ) ;
return 0;
}
void enqueue()
{
if ( rear == SIZE)
{
printf( "Queue is full (overflow)\n" ) ;
return ;
}
rear++ ;
printf( "Enter the element to ENQUEUE : " ) ;
scanf( "%d", &arr[ rear ] ) ;
if ( front == -1 )
front++ ;
}
void dequeue()
{
if ( front == -1 )
{
printf( "Queue is empty (underflow)\n" );
return ;
}
printf( "The DEQUEUE element is : %d\n", arr[ front ] ) ;
if ( front == rear )
front = rear = -1 ;
else
front++ ;
}
void display()
{
if ( front == -1 )
{
printf( "Queue is empty (underflow)\n" ) ;
return ;
}
printf( "The elements in queue are : FRONT -> " ) ;
for ( i = front ; i <= rear ; i++ )
printf( "%d<-", arr[ i ] ) ;
printf( "REAR\n" ) ;
}
OUTPUT: