正在加载图片...
★输出:成功时返回OK、通过数据元素指针e返回队首元素的值 空队列时返回 ERROR int DeQueue( Linkqueue*Q, ElemType*e)/*出队列*/ QueuePtr p: if( Queue Empty(*Q) return ERROR;/*空队列,返回错误*/ p=Q->front->next /*p指向删除结点*/ /*取数据*/ Q-> front->next=p->next;/*修改队头指针* if(Q->rear==p)Q-rear=Q-> front;/*若删除结点为队尾结点, 设置为空队列*/ return OK maino ElemType elem LinkQueue que /*定义队列对象que*/ InitQueue(&que) /*队列que初始化*/ EnQueue(&que, 1) /*元素1进队列* EnQueue(&que, 2) /*元素2进队列*/ EnQueue(&que, 3) /*元素3进队列*/ DeQueue(&que,&elem); printf("%d",elem);/*1出队列*/ DeQueue(&que,&elem); printf("%d",elem);/*2出队列*/ DeQueue(&que,&elem); printf("%d",elem);/*3出队列*/ DeQueue(&que, &elem): printf("%d", elem): /*队列已空,由于未检查返回结果,取出的还是3,产生错误*/ 顺序队列 # define MAXQSIzE100/*首次分配连续存储单元的大小,可存储 MAXQSIZE个元素*/ typedef int elemType typedef struct sqlist ElemType*base;/*连续存储单元首地址* int front /*队列头指针,若队列不空,指向队头元素*/ /*队列尾指针,若队列不空,指向队尾元素的下一个位置*/ SqQueue /* SqQueue为结构类型,用其说明的变量在程序中作为队列*/** 输出: 成功时返回 OK、通过数据元素指针 e 返回队首元素的值 ** ** 空队列时返回ERROR ** *******************************************************************/ int DeQueue(LinkQueue *Q, ElemType *e) /*出队列*/ { QueuePtr p; if (QueueEmpty(*Q)) return ERROR; /*空队列,返回错误*/ p=Q->front->next; /*p指向删除结点*/ *e=p->data; /*取数据*/ Q->front->next=p->next; /*修改队头指针*/ if (Q->rear==p) Q->rear=Q->front;/*若删除结点为队尾结点, 设置为空队列*/ free(p); return OK; } _ main() { int i; ElemType elem; LinkQueue que; /*定义队列对象que*/ InitQueue(&que); /*队列que初始化*/ EnQueue(&que,1); /*元素1进队列*/ EnQueue(&que,2); /*元素2进队列*/ EnQueue(&que,3); /*元素3进队列*/ DeQueue(&que,&elem); printf("%d",elem); /*1 出队列*/ DeQueue(&que,&elem); printf("%d",elem); /*2 出队列*/ DeQueue(&que,&elem); printf("%d",elem); /*3 出队列*/ DeQueue(&que,&elem); printf("%d",elem); /*队列已空,由于未检查返回结果,取出的还是3,产生错误*/ } 5.顺序队列 #define MAXQSIZE 100 /*首次分配连续存储单元的大小,可存储MAXQSIZE个元素*/ typedef int ElemType; typedef struct SqList { ElemType *base; /*连续存储单元首地址*/ int front; /*队列头指针,若队列不空,指向队头元素*/ int rear; /*队列尾指针,若队列不空,指向队尾元素的下一个位置*/ } SqQueue; /*SqQueue为结构类型,用其说明的变量在程序中作为队列*/
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有