The readers are requested to check for any errors in the program (as the program is typed and copied from MS Word)
QUEUE IMPLEMENTATION USING ARRAYS
#include<stdio.h>
#include<conio.h>
void create(void);
void display(void);
void queue_in(void);
void queue_out(void);
void front_rear(void);
int front,rear;
int q[25];
void create()
{
int n;
printf("Enter the number of elements in the queue \n");
scanf("%d",&n);
printf("Enter the elements \n");
for(rear=0;rear<n;rear++)
{
scanf("%d",&q[rear]);
}
front=0;
}
void display()
{
if(rear!=0)
{
printf("The elements in queue \n");
for(front=0;front<rear;front++)
{
printf("%d",q[front]);
}
front=0;
}
else if(rear==0)
printf("The queue is empty \n");
else if(rear==25)
printf("The queue is full \n");
}
void front_rear()
{
if(rear!=0)
{
printf("the front element is : %d \n",q[front]);
printf("the rear element is : %d \n",q[rear-1]);
}
else
printf("Queue is empty \n");
}
void queue_in()
{
if(rear<25)
{
printf("Enter the elements \n");
scanf("%d",&q[rear]);
++rear;
}
else
printf("Queue is full \n");
}
void queue_out()
{
if(rear!=0)
{
printf("The deleted element is : %d \n ",q[front]);
for(front=0;front<rear-1;++front)
q[front]=q[front+1];
rear--;
front=0;
}
}
int main()
{
int ch=0;
clrscr();
printf("QUEUE MANIPULATION \n");
printf("creation \n");
create();
display();
getch();
do
{
clrscr();
printf("Queue operations \n");
printf("---------------- \n");
printf("1. Inserting in to the Queue \n");
printf("2. Deletion from queue \n");
printf("3. Front and rear element \n");
printf("4. Displaying the queue \n");
printf("5. Exit \n");
printf("Enter your choice \n");
scanf("%d\n",&ch);
switch(ch)
{
case 1 :queue_in();
display();
break;
case 2 :queue_out();
display();
break;
case 3 :front_rear();
break;
case 4 :display();
break;
case 5 :printf("END \n");
break;
default: printf("Invalid Entry \n");
}
getch();
}
while(ch!=5);
return (0);
}
Sample output:
QUEUE MANIPULATION
Creation
Enter the number of elements in the queue
4
Enter the elements
1
2
3
4
The elements in queue
1234
Queue operations
----------------
1. Inserting in to the Queue
2. Deletion from queue
3. Front and rear element
4. Displaying the queue
5. Exit
Enter your choice
1
5
Enter the elements
The elements in queue
12345
Enter your choice
3
the front element is : 1
the rear element is : 5
Enter your choice
4
The elements in queue
12345
Enter your choice
2
The deleted element is: 1
The elements in queue
2345
Enter your choice
5
END
QUEUE USING LINKED LIST
#include<stdio.h>
typedef struct node
{
int data;
struct node*link;
}queue;
queue*getnode();
void releasenode(queue*p);
int isfull();
int isempty(queue*front);
void enqueue(queue**frontptr,queue**rearptr,int value);
void dequeue(queue**frontptr,queue**rearptr,int*value);
void peek(queue*front,int*value);
void view(queue*front);
int size(queue*front);
void displaymenu(void);
void main()
{
queue*front=NULL,*rear=NULL;
int choice,item;
displaymenu();
while(1)
{
printf("\n?");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(isfull())
printf("\nQueue overflow on ENQUEUE");
else
{
printf("\nEnter the element:");
fflush(stdin);
scanf("%d",&item);
enqueue(&front,&rear,item);
}
break;
case 2:
if(isempty(front))
printf("\n Queue underflow on DEQUEUE");
else
{
dequeue(&front,&rear,&item);
printf("\nThe dequeued value is %d",item);
}
break;
case 3:
if(!isempty(front))
{
peek(front,&item);
printf("\nThe front value is %d",item);
}
else
printf("\nQueue is empty");
break;
case 4:
printf("\nCount of queue elements=%d",size(front));
break;
case 5:
view(front);
break;
default:
printf("\nEnd of run of your program..");
exit(0);
}
}
}
void displaymenu()
{
printf("\nRepresentation of queue using linked list...");
printf("\n\t1.enqueue");
printf("\n\t2.dequeue");
printf("\n\t3.peek");
printf("\n\t4.size");
printf("\n\t5.view");
printf("\n\t6.exit");
}
void releasenode(queue*p)
{
free(p);
}
queue*getnode()
{
int size;
queue*newnode;
size=sizeof(queue);
newnode=(queue*)malloc(size);
return(newnode);
}
int isempty(queue*front)
{
if(front==NULL)
return 1;
else
return 0;
}
int isfull()
{
queue*newnode;
newnode=getnode();
if(newnode==NULL)
return 1;
releasenode(newnode);
return 0;
}
void enqueue(queue**frontptr,queue**rearptr,int value)
{
queue*newnode;
if(isfull())
{
printf("\nMemory not available");
return;
}
newnode=getnode();
newnode->data=value;
newnode->link=NULL;
if(*frontptr==NULL)
*frontptr=newnode;
else
(*rearptr)->link=newnode;
*rearptr=newnode;
}
void dequeue(queue**frontptr,queue**rearptr,int*value)
{
queue*tempnode;
if(isempty(*frontptr))
return;
tempnode=*frontptr;
*frontptr=(*frontptr)->link;
if(*frontptr==NULL)
*rearptr=NULL;
*value=tempnode->data;
releasenode(tempnode);
}
void peek(queue*front,int*value)
{
if(isempty(front))
{
printf("\nThe queue is empty!!!");
return;
}
*value=front->data;
}
void view(queue*front)
{
if(isempty(front))
{
printf("\nThe queue is empty!!!");
return;
}
printf("\nQueue contains...front->");
while(front!=NULL)
{
printf("%d-->",front->data);
front=front->link;
}
printf("rear/n");
}
int size(queue*front)
{
int count=0;
if(front==NULL)
return count;
for(;front!=NULL;)
{
count++;
front=front->link;
}
return count;
}
Sample output:
Representation of queue using linked list..
1. enqueue
2. dequeue
3. peek
4. size
5. view
6. exit
?2
Queue underflow on DEQUEUE
?3
Queue is empty
?4
Count of queue elements=0
?5
The queue is empty!!!
?1
Enter the element:11
?1
Enter the element:22
?1
Enter the element:33
?5
Queue contains...front->11-->22-->33-->rear/n
?4
Count of queue elements=3
?2
The dequeued value is 11
?6
Comments
Post a Comment