// 队列.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "malloc.h"
#define maxSize 10
//循环队列
typedef struct
{
int data[maxSize];
int front;
int rear;
}SqQueue;
void initQueue(SqQueue &qu)//初始队
{
qu.front=qu.rear=0;
}
int isEmpty(SqQueue qu)//判断队是否为空
{
if(qu.front==qu.rear)
return 1;
else
return 0;
}
void enQueue(SqQueue &qu,int x)//进队
{
if((qu.rear+1)%maxSize==qu.front)
return;
else
qu.rear=(qu.rear+1)%maxSize;
qu.data[qu.rear]=x;
}
void delQueue(SqQueue &qu,int &x)//出队
{
if(qu.front==qu.rear)
return;
qu.front=(qu.front+1)%maxSize;
x=qu.data[qu.front];
}
void show(SqQueue &qu)
{
int x;
while(qu.front!=qu.rear)
{
delQueue(qu,x);
printf("%d",x);
}
}
//链队
typedef struct QNode
{
int data;
struct QNode *next;
}QNode;
typedef struct
{
QNode *front;
QNode *rear;
}LiQueue;
void Li_initQueue(LiQueue *&L)
{
L=(LiQueue*)malloc(sizeof(LiQueue));
L->front=L->rear=NULL;
}
void Li_enQueue(LiQueue *&L,int x)
{
QNode *p;
p=(QNode*)malloc(sizeof(QNode));
p->data=x;
p->next=NULL;
if(L->rear==NULL)
L->front=L->rear=p;
else
{
L->rear->next=p;
L->rear=p;
}
}
void Li_delQueue(LiQueue *&L,int &x)
{
QNode *p;
if(L->front==NULL)
return;
else
{
p=L->front;
if(L->front==L->rear)
{
L->front=L->rear=NULL;
}
else
{
L->front=L->front->next;
}
x=p->data;
free(p);
}
}
void Li_show(LiQueue *&L)
{
int x;
while(L->front!=NULL)
{
Li_delQueue(L,x);
printf("%d",x);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
/*
//顺序队
SqQueue qu;
initQueue(qu);
for(int i=1;i<10;i++)
{
enQueue(qu,i);
}
show(qu);
*/
//链队
LiQueue *Li;
Li_initQueue(Li);
for(int j=1;j<5;j++)
{
Li_enQueue(Li,j);
}
Li_show(Li);
}
数据结构之 队列的操作与实现
最新推荐文章于 2023-11-18 00:03:31 发布