#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
struct queue
{
struct node *front;
struct node *rear;
};
void initqueue(struct queue*);
void addqueue(struct queue*,int);
int delqueue(struct queue*);
void deallqueue(struct queue*);
void main()
{
struct queue a;
int i;
clrscr();
initqueue(&a);
addqueue(&a,15);
addqueue(&a,5);
addqueue(&a,16);
addqueue(&a,20);
addqueue(&a,12);
addqueue(&a,18);
addqueue(&a,25);
i=delqueue(&a);
printf("\n Item removed :%d",i);
i=delqueue(&a);
printf("\n Item removed :%d",i);
i=delqueue(&a);
printf("\n Item removed :%d",i);
getch();
}
/*Initializes data member */
void initqueue(struct queue *q)
{
q->front=q->rear=NULL;
}
/* Adds an element to the queue */
void addqueue(struct queue *q,int item)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
printf("\n Queue is NULL ");
temp->data=item;
temp->next=NULL;
if(q->front==NULL)
{
q->rear=q->front=temp;
return;
}
q->rear->next=temp;
q->rear=q->rear->next;
}
/* Removes an element from the queue */
int delqueue(struct queue *q)
{
struct node *temp;
int item;
if(q->front==NULL)
{
printf("\n Queue is empty ");
return NULL;
}
item=q->front->data;
temp=q->front;
q->front=q->front->next;
free(temp);
return item;
}
/* Deallocates memory */
void deallqueue(struct queue *q)
{
struct node *temp;
if(q->front == NULL)
return ;
while(q->front !=NULL)
{
temp=q->front;
q->front=q->front->next;
free(temp);
}
}
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
struct queue
{
struct node *front;
struct node *rear;
};
void initqueue(struct queue*);
void addqueue(struct queue*,int);
int delqueue(struct queue*);
void deallqueue(struct queue*);
void main()
{
struct queue a;
int i;
clrscr();
initqueue(&a);
addqueue(&a,15);
addqueue(&a,5);
addqueue(&a,16);
addqueue(&a,20);
addqueue(&a,12);
addqueue(&a,18);
addqueue(&a,25);
i=delqueue(&a);
printf("\n Item removed :%d",i);
i=delqueue(&a);
printf("\n Item removed :%d",i);
i=delqueue(&a);
printf("\n Item removed :%d",i);
getch();
}
/*Initializes data member */
void initqueue(struct queue *q)
{
q->front=q->rear=NULL;
}
/* Adds an element to the queue */
void addqueue(struct queue *q,int item)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
printf("\n Queue is NULL ");
temp->data=item;
temp->next=NULL;
if(q->front==NULL)
{
q->rear=q->front=temp;
return;
}
q->rear->next=temp;
q->rear=q->rear->next;
}
/* Removes an element from the queue */
int delqueue(struct queue *q)
{
struct node *temp;
int item;
if(q->front==NULL)
{
printf("\n Queue is empty ");
return NULL;
}
item=q->front->data;
temp=q->front;
q->front=q->front->next;
free(temp);
return item;
}
/* Deallocates memory */
void deallqueue(struct queue *q)
{
struct node *temp;
if(q->front == NULL)
return ;
while(q->front !=NULL)
{
temp=q->front;
q->front=q->front->next;
free(temp);
}
}
No comments:
Post a Comment