队列是一种与栈相反的数据结构,它是先进先出(first in, first out),同样它也是一种运算受限的结构。
队列的抽象数据类型:
ADT QUEUE =
{
InitQueue(&Q)
//create a queue
DestroyQueue(&q)
//destroy a queue
clearQueue(&q)
//clear the queue
............
}
队列的c语言实现
1:
顺序队列:
/* ******************************************
* FileName:squeue.c
* Author:hufeng Date:2007/11/21
* Version:v1.0
* Description: The program creat a queue and lots
* of operating
* ******************************************/
/* header file */
#include <stdio.h>
#define MAX 20
/* creat a struct */
typedef struct squeue
{
int date[MAX]; /*declartion a array */
int front,rear; /* declarion a front and a rear pointer */
}sq;
/* assign queue each elem */
void Assign_squeue(sq *p, int count)
{
int i; /* declaration a cycle variable */
if (p->rear > MAX - 1)
{
printf("Overflow!/n");
return;
}
printf("Please enter each elem:/n");
for (i = 0; i < count; i++)
{
scanf("%d", &p->date[++p->rear]);
}
}
/* insert a new elem in the queue */
void Insert_squeue(sq *p, int num)
{
if (p->rear > MAX - 1)
{
printf("Over flow!/n");
return;
}
else
{
p->date[++p->rear] = num;
}
}
/* Delete a elem */
void Delete_squeue(sq *p)
{
int x;
if (p->front == p->rear)
{
printf("Empty queue!/n");
return;
}
else
{
x = p->date[++p->front];
printf("P->front = %d/n",p->front);
printf("The deleted number is:%d/n",x);
}
}
/* main program */
int main()
{
sq *p = NULL; /* declaration a pointer */
int num; /* decalration a number of queue */
p->front = -1;
p->rear = p->front;
printf("Please enter the length of queue:/n");
scanf("%d", &num);
Assign_squeue(p, num); /* assign array date */
/* insert a number in the queue */
printf("Please enter which number you want to insert:/n");
scanf("%d", &num);
Insert_squeue(p, num);
/* delete a number */
Delete_squeue(p);
getch();
return 0;
}
2:
循环队列:
/* ******************************************
* FileName:cycle_queue.c
* Author:hufeng Date:2007/11/21
* Version:v1.0
* Description:The program is a cycle queue
*********************************************/
/* header file */
#include <stdio.h>
#define MAX 20
/* creat a struct */
typedef struct cycle_queue
{
int date[MAX];
int front,rear;
}cq;
/* assign queue */
void Assign_CycleQueue(cq *p, int n)
{
int i;
int x;
if (n < 1 || n > MAX - 1)
{
printf("Overflow!/n");
return;
}
printf("Please enter each elem:/n");
for (i = 0; i < n; i++)
{
scanf("%d", &x);
p->date[p->rear++] = x;
}
}
/* insert a number */
void Insert_CycleQueue(cq *p, int num)
{
if ((p->rear + 1) % MAX == p->front)
{
printf("full queue!/n");
return;
}
else
{
p->date[++p->rear] = num;
}
}
/* delete number */
void Delete_CycleQueue(cq *p)
{
int x;
if ((p->front + 1) % MAX == p->rear)
{
printf("empty queue!/n");
return;
}
x = p->date[p->front];
printf("p->front = %d/n", p->front);
printf("The deleted number is:%d/n",x);
}
/* main program */
int main()
{
int num;
cq *p = NULL;
p->front = 0;
p->rear = p->front;
printf("Please enter the length of queue:/n");
scanf("%d", &num);
Assign_CycleQueue(p,num);
printf("Please enter the inserting number:/n");
scanf("%d",&num);
Insert_CycleQueue(p, num);
Delete_CycleQueue(p);
getch();
return 0;
}
3.
链队:
/* ******************************************
* FileName:cycle_queue.c
* Author:hufeng Date:2007/11/21
* Version:v1.0
* Description:The program is a cycle queue
*********************************************/
/* header file */
#include <stdio.h>
#define MAX 20
/* creat a struct */
typedef struct cycle_queue
{
int date[MAX];
int front,rear;
}cq;
/* assign queue */
void Assign_CycleQueue(cq *p, int n)
{
int i;
int x;
if (n < 1 || n > MAX - 1)
{
printf("Overflow!/n");
return;
}
printf("Please enter each elem:/n");
for (i = 0; i < n; i++)
{
scanf("%d", &x);
p->date[p->rear++] = x;
}
}
/* insert a number */
void Insert_CycleQueue(cq *p, int num)
{
if ((p->rear + 1) % MAX == p->front)
{
printf("full queue!/n");
return;
}
else
{
p->date[++p->rear] = num;
}
}
/* delete number */
void Delete_CycleQueue(cq *p)
{
int x;
if ((p->front + 1) % MAX == p->rear)
{
printf("empty queue!/n");
return;
}
x = p->date[p->front];
printf("p->front = %d/n", p->front);
printf("The deleted number is:%d/n",x);
}
/* main program */
int main()
{
int num;
cq *p = NULL;
p->front = 0;
p->rear = p->front;
printf("Please enter the length of queue:/n");
scanf("%d", &num);
Assign_CycleQueue(p,num);
printf("Please enter the inserting number:/n");
scanf("%d",&num);
Insert_CycleQueue(p, num);
Delete_CycleQueue(p);
getch();
return 0;
}