队列,是一种运算受限制的线性表,只允许在一端进行插入(队尾),另一端(队头)进行删除
在队列的顺序存储结构中,设头指针为front,队尾指针rear,队的容量(存储空间的大小)为MaxSize。当有元素加入到队列时, 若rear=MaxSize(初始时rear=0)则发生队列的上溢现象,该元素不能加入队列。特别要注意的是队列的假溢出现象:队列中还有剩余空间但元素却不能进入队列,造成这种现象的原因是由于队列的操作方法所致。
//2011-02-06 UNIX环境 队列的出队,入队
#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct student { int num; struct student *next; }node; typedef struct linkqueue { node *front;//队首指针 node *rear;//队尾指针 }queue; queue *insert(queue *HQ)//入队 { int x; printf("please input the number:"); scanf("%d",&x); node *s; s = (node *)malloc(sizeof(node)); s->num = x; s->next = NULL; if(HQ->rear == NULL) { HQ->front = s; HQ->rear = s; } else { HQ->rear->next = s; HQ->rear = s; } return (HQ); } queue *delete(queue *HQ)//出队 { node *p; int x; if(HQ->front == NULL) { printf("yi chu"); } else { p = HQ->front; x = HQ->front->num; if(HQ->front == HQ->rear) { HQ->front = NULL; HQ->rear = NULL; } else { HQ->front = HQ->front->next; free(p); } return (HQ); } } void print(queue *HQ)//打印队列 { node *p; p = HQ->front; while(p != NULL) { printf("number:%d/n",p->num); p = p->next; } } int main(int argc, char *argv[]) { queue *hq; hq = (queue *)malloc(sizeof(queue)); hq->rear = hq->front = NULL; hq = insert(hq); hq = insert(hq); print(hq); hq = delete(hq); print(hq); return 0; }