C/C++队列操作
1.链队结构
typedef struct queuenode
{
int data;
struct queuenode *next;
}Queue;
typedef struct
{
Queue *fronts,*rear;
}linkqueue;
2.入队操作
//进队函数
void inQueue(linkqueue *q)
{
int x;
Queue *p;
p = new Queue;
printf("\n\t\t请键入一个整数:");
scanf("%d",&x);
p->data = x;
p->next = NULL;
if(q->fronts == NULL)
q->fronts = p;
else
q->rear->next = p;
q->rear = p;
if(p)
printf("\n\t\t%d进队成功!",x);
}
3.出队操作
//出队函数
int outQueue(linkqueue *q,int &x)
{
Queue *p;
if(q->fronts == NULL)
{
printf("\n\t\t队空无法出队!");
return 0;
}
else
{
p = q->fronts;
x = p->data;
q->fronts = p->next;
if(q->fronts == NULL)
q->rear = NULL;
delete p;
printf("\n\t\t%d出队成功!",x);
return 1;
}
}
4.读队首
//读取队列首元素的函数
void read_quequ_top(linkqueue *q)
{
if(q == NULL || q->fronts == NULL)
printf("\n\t\t队列为空!");
else
printf("\n\t\t队首元素为:%4d\n",q->fronts->data);
}
5.显示队列
//显示队列
void show_queue(linkqueue *q)
{
Queue *p;
p = q->fronts;
if(p == NULL)
printf("\n\t\t队列为空!");
else
{
printf("\n\t\t队列中的元素为:");
while(p != NULL)
{
printf("%6d",p->data);
p = p->next;
}
printf("\n");
}
}
6.双向队列
//双队列函数
#define QueueMax 20
int queues[QueueMax];
int fronts = -1;
int rear = -1;
void inQueue(int val) //输入队列数据
{
rear = (rear++)%QueueMax;
rear++;
if(fronts == rear)
printf("\n\t\t队列已满!");
else
queues[rear] = val;
}
int outQueue_rear() //从队尾输出元素
{
int t;
if(fronts == rear)
return -1;
t = queues[rear--];
if(rear < 0 && fronts != -1)
rear = QueueMax-1;
return t;
}
int outQueue_front() //从队首输出元素
{
int t;
if(fronts == rear)
return -1;
t = queues[++fronts];
if(fronts == QueueMax)
fronts = 0;
return t;
}
//双向队列菜单
void DQ()
{
int choice;
int out[5];
int in[5] ={5,4,3,2,1}; //队列预先存入五个数据
int t,pos=0,i;
for(i = 0;i < 5;i++)
{
inQueue(in[i]);
}
printf("\n\t\t初始化数据顺序为:");
for(i = 0;i < 5;i++)
printf("[%d]",in[i]);
printf("\n\t\t 1--------从头出队 2-------从尾出队");
while(fronts != rear)
{
printf("\n\t\t请输入选择:");
scanf("%d",&choice);
switch(choice)
{
case 1:
t = outQueue_front();
out[pos++] = t;
break;
case 2:
t = outQueue_rear();
out[pos++] = t;
break;
}
}
printf("\n\t\t数据输出的顺序为:");
for(i = 0;i < 5;i++)
printf("[%d]",out[i]);
printf("\n");
}
7.圆形队列
//入队时
q.rear = (q.rear+1)%MAXLEN;
q.front = (q.front+1)%MAXLEN;
8.主函数
void queue_main()
{
linkqueue *q;
q = new linkqueue;
int val,i = 1;
char w;
int choice;
q->fronts = q->rear = NULL;
while(1)
{
printf("\n");
printf("\n\t\t***************************************************");
printf("\n\t\t* 队列子系统 *");
printf("\n\t\t***************************************************");
printf("\n\t\t* 1------进 队 *");
printf("\n\t\t* 2------出 队 *");
printf("\n\t\t* 3------读队首元素 *");
printf("\n\t\t* 4------显 示 *");
printf("\n\t\t* 5------双队列 *");
printf("\n\t\t* 0------返 回 *");
printf("\n\t\t***************************************************");
printf("\n\t\t请选择菜单号:");
scanf("%d",&choice);
switch(choice)
{
case 1:
inQueue(q);
break;
case 2:
if(outQueue(q,val) == 0)
printf("\n\t\t队列为空!");
else
printf("\n\t\t出队元素为:%4d",val);
break;
case 3:
read_quequ_top(q);
break;
case 4:
show_queue(q);
break;
case 5:
DQ();
break;
case 0:
i = 0;
break;
default:
printf("\n\t\t输入错误!请重新输入!");
break;
}
}
}