0% found this document useful (0 votes)
7 views2 pages

C Queue Implementation in C Language

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

C Queue Implementation in C Language

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include<stdio.

h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}node;
typedef struct{
node *rear,*front;
} queue;
queue *createqueue(){
queue q=(queue)malloc(sizeof(queue));
q->front=q->rear=NULL;
return q;
}
void enqueue(queue *q,int element){
node* new_node=(node*)malloc(sizeof(node));
if(new_node==NULL){
printf("memory is not allocated");
}
else{
new_node->data=element;
new_node->next=NULL;
if(q->rear==NULL){
q->front=q->rear=new_node;
return;
}
q->rear->next=new_node;
q->rear=new_node;
printf("the element inserted is %d\n",element);
}
}
void dequeue(queue *q){
if(q->front==NULL){
printf("queue is empty\n");
return;
}
node *temp;
temp=q->front;
q->front=q->front->next;
printf("%d is deleted\n",temp->data);
free(temp);
if(q->front==NULL)
{
q->rear=NULL;
}
}
void display(queue *q){
if(q->front==NULL){
printf("queue is empty\n");
return;
}
node *temp=q->front;
while(temp!=NULL){
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
int main(){
queue *q=createqueue();
int element,ch;
while(1){
printf("enter 1 to enqueue\nenter 2 to dequeue\nenter 3 to display\
nenter 4 to exit\n");
scanf("%d",&ch);
switch(ch){
case 1:printf("enter the element to be inserted ");
scanf("%d",&element);
enqueue(q,element);
break;
case 2:dequeue(q);
break;
case 3:display(q);
break;
case 4:exit(0);break;
}
}
return 0;
}

You might also like