数据结构——队列

    科技2025-09-08  64

    一.概念

    队列是只允许在一端进行插入操作、另一端进行删除操作的线性表。队列是一种先进先出(First In First Out)的线性表,简称FIFO。允许插入的端称为队尾,允许删除的一端称为队头。

    入栈:(底部)A<-B<-C<-D(顶部) 出栈:(底部)A->B->C->D(顶部)

    出队列(头部)A<-B<-C<-D 入队列(尾部)

    二.方法

    InitQueue(&Q):初始化队列,构造一个空队列Q。 DestroyQueue(&Q):销毁队列。销毁并释放队列Q所占用的内存空间。 EnQueue(&Q,x):入队,若队列Q未满,将x加入,使之成为新的队尾。 DeQueue(&Q,&x):出队,若队列Q非空,删除队头元素,并用x返回。 GetHead(Q,&x):读队头元素,若队列Q非空,则将队头元素赋值给x。 其他常用操作: QueueEmpty(Q):判队列空,若队列Q为空返回true,否则返回false。

    三.队列的顺序实现

    // // Created by 15565 on 2020/10/8. // #define MaxSize 10 //定义数组的大小 typeof struct { ElemType data[MaxSize]; int front,rear; } SqQueue; void testQueue(){ SqQueue Q; } //初始化队列 void InitQueue(SqQueue &Q){ Q.rear=Q.font=0; } //入队 bool EnQueue(SqQueue &Q,ElemType x){ if("队列已满"){ return false; } Q.data[Q.rear]=X; Q.rear=Q.rear+1; return true; }

    四.循环队列

    // // Created by 15565 on 2020/10/8. // #define MaxSize 10 //定义数组的大小 typeof struct { ElemType data[MaxSize]; int front,rear; } SqQueue; void testQueue(){ SqQueue Q; } //初始化队列 void InitQueue(SqQueue &Q){ Q.rear=Q.font=0; } //入队 bool EnQueue(SqQueue &Q,ElemType x){ if((Q.rear+1)%MaxSize==0Q.front) return false; Q.data[Q.rear]=x; Q.rear=(Q.rear+1)%MaxSize; return true; }

    五.链式结构

    // // Created by 15565 on 2020/10/8. // typeof struct LinkNode{ ElemType data; struct LinkNode *next; } LinkNode; typeof struct { LinkNode *front,*rear; }; void InitQueue(LinkNode &Q){ Q.font=Q.rear=(LinkNode*)malloc(sizeof(LinkNode)); Q.font->next=null; }

     

     

    Processed: 0.017, SQL: 8