Monday, May 24, 2010

Write a C program to implement a stack and a queue in a single array?

/* Program implements array as a stack. */





#include %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;





#define MAX 10





struct stack


{


int arr[MAX] ;


int top ;


} ;





void initstack ( struct stack * ) ;


void push ( struct stack *, int item ) ;


int pop ( struct stack * ) ;





void main( )


{


struct stack s ;


int i ;





clrscr( ) ;





initstack ( %26amp;s ) ;





push ( %26amp;s, 11 ) ;


push ( %26amp;s, 23 ) ;


push ( %26amp;s, -8 ) ;


push ( %26amp;s, 16 ) ;


push ( %26amp;s, 27 ) ;


push ( %26amp;s, 14 ) ;


push ( %26amp;s, 20 ) ;


push ( %26amp;s, 39 ) ;


push ( %26amp;s, 2 ) ;


push ( %26amp;s, 15 ) ;


push ( %26amp;s, 7 ) ;





i = pop ( %26amp;s ) ;


printf ( "\n\nItem popped: %d", i ) ;





i = pop ( %26amp;s ) ;


printf ( "\nItem popped: %d", i ) ;





i = pop ( %26amp;s ) ;


printf ( "\nItem popped: %d", i ) ;





i = pop ( %26amp;s ) ;


printf ( "\nItem popped: %d", i ) ;





i = pop ( %26amp;s ) ;


printf ( "\nItem popped: %d", i ) ;





getch( ) ;


}





/* intializes the stack */


void initstack ( struct stack *s )


{


s -%26gt; top = -1 ;


}





/* adds an element to the stack */


void push ( struct stack *s, int item )


{


if ( s -%26gt; top == MAX - 1 )


{


printf ( "\nStack is full." ) ;


return ;


}


s -%26gt; top++ ;


s -%26gt; arr[s -%26gt;top] = item ;


}





/* removes an element from the stack */


int pop ( struct stack *s )


{


int data ;


if ( s -%26gt; top == -1 )


{


printf ( "\nStack is empty." ) ;


return NULL ;


}


data = s -%26gt; arr[s -%26gt; top] ;


s -%26gt; top-- ;


return data ;


}





______________________________________...





/* Program that implements queue as an array. */





#include %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;





#define MAX 10





void addq ( int *, int, int *, int * ) ;


int delq ( int *, int *, int * ) ;





void main( )


{


int arr[MAX] ;


int front = -1, rear = -1, i ;





clrscr( ) ;





addq ( arr, 23, %26amp;front, %26amp;rear ) ;


addq ( arr, 9, %26amp;front, %26amp;rear ) ;


addq ( arr, 11, %26amp;front, %26amp;rear ) ;


addq ( arr, -10, %26amp;front, %26amp;rear ) ;


addq ( arr, 25, %26amp;front, %26amp;rear ) ;


addq ( arr, 16, %26amp;front, %26amp;rear ) ;


addq ( arr, 17, %26amp;front, %26amp;rear ) ;


addq ( arr, 22, %26amp;front, %26amp;rear ) ;


addq ( arr, 19, %26amp;front, %26amp;rear ) ;


addq ( arr, 30, %26amp;front, %26amp;rear ) ;


addq ( arr, 32, %26amp;front, %26amp;rear ) ;





i = delq ( arr, %26amp;front, %26amp;rear ) ;


printf ( "\nItem deleted: %d", i ) ;





i = delq ( arr, %26amp;front, %26amp;rear ) ;


printf ( "\nItem deleted: %d", i ) ;





i = delq ( arr, %26amp;front, %26amp;rear ) ;


printf ( "\nItem deleted: %d", i ) ;





getch( ) ;


}





/* adds an element to the queue */


void addq ( int *arr, int item, int *pfront, int *prear )


{


if ( *prear == MAX - 1 )


{


printf ( "\nQueue is full." ) ;


return ;


}





( *prear )++ ;


arr[*prear] = item ;





if ( *pfront == -1 )


*pfront = 0 ;


}





/* removes an element from the queue */


int delq ( int *arr, int *pfront, int *prear )


{


int data ;





if ( *pfront == -1 )


{


printf ( "\nQueue is Empty." ) ;


return NULL ;


}





data = arr[*pfront] ;


arr[*pfront] = 0 ;


if ( *pfront == *prear )


*pfront = *prear = -1 ;


else


( *pfront )++ ;





return data ;


}


No comments:

Post a Comment