Stack and Queue in an array.
Algorithm_Push():
1.if ((top>=size-1) OR (top>=rear-1)) //check top occupies whole arrray (ie, size-1) or just before the queue (ie, rear-1)
1.print Stack is Full
2.else
1.top=top+1
2.s[top]=item
3.End if
4.Stop
Algorithm_Pop():
1.if (top<=-1) //check top is less than 0, ie no element in stack
1.print Stack is Empty
2.else
1.top=top-1 //decrement value of top so that element with radix top is avoided from the stack
3.End if
4.Stop
Algorithm_Enqueue():
1.if(rear<=top+1) //check rear end of queue is just after the top element of stack (ie, array is full)
2.print Queue is full
2.else
1.if (front==size) //front==size means, no element in the queue
1.front=size-1 //defines front end as last element of queue (ie, size-1)
2.rear=rear-1 //position of the rear end is varied
3.s[rear]=item
3.End if
4.End if
5.Stop
Algorithm_Dequeue():
1.if(front==size) //front end=size, means no element in queue
1.print Queue is Empty
2.else
1.if(front==rear) //if front end=rear end, only one element in queue
1.front=size //define front end and rear end to out of the array
2.rear=size //
2.else
1.front=front-1 //front end is decremented
3.End if
3.End if
4.Stop
Algorithm_Stack_Display():
1.if (top<0)
1.print Stack is Empty
2.else
1.for(i=top;i>=0;i--)
2.print s[i]
3.End if
4.Stop
Algorithm_Queue_Display():
1.if(front>=size)
1.print Queue is Empty
2.else
1.for(i=front;i>=rear;i--)
2.print s[i]
3.End if
4.Stop
Program:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void push(int s[],int *top,int size,int *rear)
{
int item;
if ((*top>=size-1) || (*top>=*rear-1))
{
printf("\nStack Overflow!!");
}
else
{
*top=*top+1;
printf("\nEnter the item to be inserted ");
scanf("%d",&item);
s[*top]=item;
}
}
void pop(int s[],int *top,int size)
{
if (*top<0)
{
printf("\nStack Underflow!!");
}
else
{
*top=*top-1;
}
}
void enqueue(int s[],int *top,int *rear, int *front, int size)
{
int item;
if(*rear<=*top+1)
{
printf("\nQueue Overflow!!");
}
else
{
printf("\nEnter the item to be inserted ");
scanf("%d",&item);
if (*front==size)
{
*front=size-1;
}
*rear=*rear-1;
s[*rear]=item;
}
}
void dequeue(int s[], int *rear,int *front,int size)
{
if(*front==size)
{
printf("\nQueue Underflow!!");
}
else
{
if(*front==*rear)
{
*front=size;
*rear=size;
}
else
{
*front=*front-1;
}
}
}
void displaystack(int s[],int *top)
{
int i;
if (*top<0)
{
printf("\nStack Underflow!!");
}
else
{
printf("\nStack: ");
for(i=*top;i>=0;i--)
printf(" %d",s[i]);
}
}
void displayqueue(int s[],int *rear, int *front, int size)
{
int i;
if(*front>=size)
{
printf("\nQueue Underflow!!");
}
else
{
printf("\nQueue: ");
for(i=*front;i>=*rear;i--)
printf(" %d",s[i]);
}
}
int main()
{
int s[100],size,ch,top=-1,front,rear;
printf("\nEnter size of the stack ");
scanf("%d",&size);
front=size;
rear=size;
do
{
printf("\n..Menu..\n1.Push\n2.Pop\n3.Enqueue\n4.Dequeue\n5.Display Stack\n6.Display Queue\n7.Exit\n\nEnter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: push(s,&top,size,&rear);
break;
case 2: pop(s,&top,size);
break;
case 3: enqueue(s,&top,&rear,&front,size);
break;
case 4: dequeue(s,&rear,&front,size);
break;
case 5: displaystack(s,&top);
break;
case 6: displayqueue(s,&rear,&front,size);
break;
case 7: exit(0);
default: printf("\nWrong Choice!! Try again!!");
}
}while(1);
return 0;
getch();
}
1.if ((top>=size-1) OR (top>=rear-1)) //check top occupies whole arrray (ie, size-1) or just before the queue (ie, rear-1)
1.print Stack is Full
2.else
1.top=top+1
2.s[top]=item
3.End if
4.Stop
Algorithm_Pop():
1.if (top<=-1) //check top is less than 0, ie no element in stack
1.print Stack is Empty
2.else
1.top=top-1 //decrement value of top so that element with radix top is avoided from the stack
3.End if
4.Stop
Algorithm_Enqueue():
1.if(rear<=top+1) //check rear end of queue is just after the top element of stack (ie, array is full)
2.print Queue is full
2.else
1.if (front==size) //front==size means, no element in the queue
1.front=size-1 //defines front end as last element of queue (ie, size-1)
2.rear=rear-1 //position of the rear end is varied
3.s[rear]=item
3.End if
4.End if
5.Stop
Algorithm_Dequeue():
1.if(front==size) //front end=size, means no element in queue
1.print Queue is Empty
2.else
1.if(front==rear) //if front end=rear end, only one element in queue
1.front=size //define front end and rear end to out of the array
2.rear=size //
2.else
1.front=front-1 //front end is decremented
3.End if
3.End if
4.Stop
Algorithm_Stack_Display():
1.if (top<0)
1.print Stack is Empty
2.else
1.for(i=top;i>=0;i--)
2.print s[i]
3.End if
4.Stop
Algorithm_Queue_Display():
1.if(front>=size)
1.print Queue is Empty
2.else
1.for(i=front;i>=rear;i--)
2.print s[i]
3.End if
4.Stop
Program:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void push(int s[],int *top,int size,int *rear)
{
int item;
if ((*top>=size-1) || (*top>=*rear-1))
{
printf("\nStack Overflow!!");
}
else
{
*top=*top+1;
printf("\nEnter the item to be inserted ");
scanf("%d",&item);
s[*top]=item;
}
}
void pop(int s[],int *top,int size)
{
if (*top<0)
{
printf("\nStack Underflow!!");
}
else
{
*top=*top-1;
}
}
void enqueue(int s[],int *top,int *rear, int *front, int size)
{
int item;
if(*rear<=*top+1)
{
printf("\nQueue Overflow!!");
}
else
{
printf("\nEnter the item to be inserted ");
scanf("%d",&item);
if (*front==size)
{
*front=size-1;
}
*rear=*rear-1;
s[*rear]=item;
}
}
void dequeue(int s[], int *rear,int *front,int size)
{
if(*front==size)
{
printf("\nQueue Underflow!!");
}
else
{
if(*front==*rear)
{
*front=size;
*rear=size;
}
else
{
*front=*front-1;
}
}
}
void displaystack(int s[],int *top)
{
int i;
if (*top<0)
{
printf("\nStack Underflow!!");
}
else
{
printf("\nStack: ");
for(i=*top;i>=0;i--)
printf(" %d",s[i]);
}
}
void displayqueue(int s[],int *rear, int *front, int size)
{
int i;
if(*front>=size)
{
printf("\nQueue Underflow!!");
}
else
{
printf("\nQueue: ");
for(i=*front;i>=*rear;i--)
printf(" %d",s[i]);
}
}
int main()
{
int s[100],size,ch,top=-1,front,rear;
printf("\nEnter size of the stack ");
scanf("%d",&size);
front=size;
rear=size;
do
{
printf("\n..Menu..\n1.Push\n2.Pop\n3.Enqueue\n4.Dequeue\n5.Display Stack\n6.Display Queue\n7.Exit\n\nEnter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: push(s,&top,size,&rear);
break;
case 2: pop(s,&top,size);
break;
case 3: enqueue(s,&top,&rear,&front,size);
break;
case 4: dequeue(s,&rear,&front,size);
break;
case 5: displaystack(s,&top);
break;
case 6: displayqueue(s,&rear,&front,size);
break;
case 7: exit(0);
default: printf("\nWrong Choice!! Try again!!");
}
}while(1);
return 0;
getch();
}
No comments