download struct queue code tricks
#include<conio.h>
#define max 10
struct queue
{
int item[max];
int rear,front;
};
void insert(struct queue *q,int c)
{
if(q->front==-1)
q->front=0;
if(q->rear==max-1)
printf("Given queue is overflow");
else
q->item[++(q->rear)]=c;
}
int del(struct queue *q)
{
if(q->front==-1||q->front>q->rear)
printf("Queue underflow");
else
return(q->item[(q->front)++]);
}
void show(struct queue *q)
{
int i;
for(i=q->front;i<=q->rear;i++)
{
printf(" %d\n",q->item[i]);
}
}
void main()
{
struct queue q;
int i,ch;
int e;
q.front=q.rear=-1;
while(1)
{
clrscr();
printf(" *************************** Option *******************************\n");
printf(" 0:Exit.\n");
printf(" 1:Insert element into queue.\n");
printf(" 2:Delete element from queue.\n");
printf(" 3:Show all element of queue.\n");
printf(" ******************************************************************\n");
printf("Please enter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter element to insert=");
scanf("%d",&e);
insert(&q,e);
break;
case 2:
printf("Deleted element=%d",del(&q));
break;
case 3:
printf("Element of queue\n");
show(&q);
break;
default:
exit(0);
}
printf("\n Press enter to next opration");
getch();
}
}
#include<conio.h>
#define max 10
struct queue
{
int item[max];
int rear,front;
};
void insert(struct queue *q,int c)
{
if(q->front==-1)
q->front=0;
if(q->rear==max-1)
printf("Given queue is overflow");
else
q->item[++(q->rear)]=c;
}
int del(struct queue *q)
{
if(q->front==-1||q->front>q->rear)
printf("Queue underflow");
else
return(q->item[(q->front)++]);
}
void show(struct queue *q)
{
int i;
for(i=q->front;i<=q->rear;i++)
{
printf(" %d\n",q->item[i]);
}
}
void main()
{
struct queue q;
int i,ch;
int e;
q.front=q.rear=-1;
while(1)
{
clrscr();
printf(" *************************** Option *******************************\n");
printf(" 0:Exit.\n");
printf(" 1:Insert element into queue.\n");
printf(" 2:Delete element from queue.\n");
printf(" 3:Show all element of queue.\n");
printf(" ******************************************************************\n");
printf("Please enter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter element to insert=");
scanf("%d",&e);
insert(&q,e);
break;
case 2:
printf("Deleted element=%d",del(&q));
break;
case 3:
printf("Element of queue\n");
show(&q);
break;
default:
exit(0);
}
printf("\n Press enter to next opration");
getch();
}
}
0 comments: