zoukankan      html  css  js  c++  java
  • 队列

    队列,与栈相反,它是先进先出的一种线性表。(就像一根管道)

    因为是一种线性表,所以跟线性表类似,队列也有两种存储表示。

    用链表表示的队列简称为 链队列。一个链队列显然需要两个分别指示队头和队尾的指针(分别称为头指针和尾指针)才能唯一确定。

    上代码:

     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 
     4 /****** 宏定义 ******/
     5 #define ERROR 0
     6 #define OVERFLOW -2
     7 #define OK 1
     8 
     9 /****** 取别名 ******/
    10 typedef int QElemType;
    11 typedef int Status;
    12 
    13 //-------- 单链队列 ——队列的链式存储结构
    14 typedef struct QNode{
    15     QElemType data;
    16     struct QNode *next;
    17 }QNode, *QueuePTr;
    18 
    19 typedef struct{
    20     QueuePTr front;    //队头指针
    21     QueuePTr rear;    //队尾5指针
    22 }LinkQueue;
    23 
    24 /********* 构造一个空的队列Q *******/
    25 Status InitQueue(LinkQueue &Q){
    26     Q.front = Q.rear = (QueuePTr)malloc(sizeof(QNode));
    27     if(!Q.front)
    28         exit(OVERFLOW);
    29     Q.front->next =  NULL;
    30     return OK;
    31 }
    32 
    33 /********* 销毁队列Q **********/
    34 Status DestroyQueue(LinkQueue &Q){
    35     while (Q.front){
    36         Q.rear = Q.front->next;
    37         free(Q.front);
    38         Q.front = Q.rear;
    39     }
    40     return OK;
    41 }
    42 
    43 /******** 插入元素e为新的队尾元素 ********/
    44 Status EnQueue(LinkQueue &Q, QElemType e){
    45     QueuePTr p;
    46     p = (QueuePTr)malloc(sizeof(QNode));
    47     if (!p)
    48         exit(OVERFLOW);
    49     p->data = e;
    50     p->next = NULL;
    51     Q.rear->next = p;
    52     Q.rear = p;
    53     return OK;
    54 }
    55 
    56 
    57 /********* 删除对头的元素,并用e返回其值 *******/
    58 Status DeQueue(LinkQueue &Q, QElemType &e){
    59     if (Q.front == Q.rear)
    60         return ERROR;
    61     QueuePTr p;
    62     p = Q.front->next;
    63     e = p->data;
    64     Q.front->next = p->next;
    65     if (Q.rear == p)
    66         Q.rear = Q.front;
    67     free(p);
    68     return OK;
    69 }
    70 
    71 int main(){
    72     //
    73     return 0;
    74 }
  • 相关阅读:
    jquery中简易tab切换
    网页中用哪种空链接
    php中each()与list()函数
    转:const“变量”、define的常量和static 变量
    php错误收集
    php基础小知识
    初学深度学习(TensorFlow框架的心得and经验总结)自用环境的总结
    Python扩展模块——调用WindowsAPI(pywin32的简单使用)
    Python扩展模块——selenium的使用(定位、下载文件等)
    Python扩展模块——自动化(testlinkAPI的使用)
  • 原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/7787633.html
Copyright © 2011-2022 走看看