CODING:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
struct queue
{
struct node *front;
struct node *rear;
};
struct queue *q;
void create_queue(struct queue *);
struct queue *insert(struct queue *);
struct queue *delete_element(struct queue *);
struct queue *display(struct queue *);
int peek(struct queue *);
void main()
{
int ch,num;
clrscr();
create_queue(q);
do
{
printf("\nS E L E C T C H O I C E");
printf("\n1.INSERT");
printf("\n2.DELETE");
printf("\n3.PEEK");
printf("\n4.DISPLAY");
printf("\n5.EXIT");
printf("\nEnter the Number:");
scanf("%d",&ch);
switch(ch)
{
case 1:
q=insert(q);
break;
case 2:
q=delete_element(q);
break;
case 3:
num=peek(q);
printf("%d is stored at the top of Queue.",num);
break;
case 4:
q=display(q);
break;
case 5:
exit(0);
break;
default:
printf("Invalid Enter the number,TRY AGAIN.");
break;
}
}while(ch!=5);
getch();
}
void create_queue(struct queue *q)
{
q->front=NULL;
q->rear=NULL;
}
struct queue *insert(struct queue *q)
{
int num;
struct node *ptr;
printf("Enter the number to be inserted in the Queue:");
scanf("%d",&num);
ptr=(struct node *)malloc(sizeof(struct node *));
ptr->data=num;
if(q->front==NULL)
{
q->front=ptr;
q->rear=ptr;
q->front->next=q->rear->next=NULL;
}
else
{
q->rear->next=ptr;
q->rear=ptr;
q->rear->next=NULL;
}
return q;
}
struct queue *delete_element(struct queue *q)
{
struct node *ptr;
ptr=q->front;
if(q->front==NULL)
printf("Queue is underflow.");
else
{
q->front=q->front->next;
printf("%d is deleted in Queue.",ptr->data);
free(ptr);
}
return q;
}
struct queue *display(struct queue *q)
{
struct node *ptr;
ptr=q->front;
if(ptr==NULL)
printf("Queue is Empty.");
else
{
printf("QUEUE:\n");
while(ptr!=q->rear)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
printf("%d",ptr->data);
}
return q;
}
int peek(struct queue *q)
{
return q->front->data;
}
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
struct queue
{
struct node *front;
struct node *rear;
};
struct queue *q;
void create_queue(struct queue *);
struct queue *insert(struct queue *);
struct queue *delete_element(struct queue *);
struct queue *display(struct queue *);
int peek(struct queue *);
void main()
{
int ch,num;
clrscr();
create_queue(q);
do
{
printf("\nS E L E C T C H O I C E");
printf("\n1.INSERT");
printf("\n2.DELETE");
printf("\n3.PEEK");
printf("\n4.DISPLAY");
printf("\n5.EXIT");
printf("\nEnter the Number:");
scanf("%d",&ch);
switch(ch)
{
case 1:
q=insert(q);
break;
case 2:
q=delete_element(q);
break;
case 3:
num=peek(q);
printf("%d is stored at the top of Queue.",num);
break;
case 4:
q=display(q);
break;
case 5:
exit(0);
break;
default:
printf("Invalid Enter the number,TRY AGAIN.");
break;
}
}while(ch!=5);
getch();
}
void create_queue(struct queue *q)
{
q->front=NULL;
q->rear=NULL;
}
struct queue *insert(struct queue *q)
{
int num;
struct node *ptr;
printf("Enter the number to be inserted in the Queue:");
scanf("%d",&num);
ptr=(struct node *)malloc(sizeof(struct node *));
ptr->data=num;
if(q->front==NULL)
{
q->front=ptr;
q->rear=ptr;
q->front->next=q->rear->next=NULL;
}
else
{
q->rear->next=ptr;
q->rear=ptr;
q->rear->next=NULL;
}
return q;
}
struct queue *delete_element(struct queue *q)
{
struct node *ptr;
ptr=q->front;
if(q->front==NULL)
printf("Queue is underflow.");
else
{
q->front=q->front->next;
printf("%d is deleted in Queue.",ptr->data);
free(ptr);
}
return q;
}
struct queue *display(struct queue *q)
{
struct node *ptr;
ptr=q->front;
if(ptr==NULL)
printf("Queue is Empty.");
else
{
printf("QUEUE:\n");
while(ptr!=q->rear)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
printf("%d",ptr->data);
}
return q;
}
int peek(struct queue *q)
{
return q->front->data;
}
Comments
Post a Comment