zoukankan      html  css  js  c++  java
  • 链队列的初始化,建立,插入,查找,删除。

    代码


    ////////////////////////////////////////////
    //链队列的初始化,建立,插入,查找,删除。//
    //Author:Wang Yong //
    //Date: 2010.8.19 //
    ////////////////////////////////////////////



    #include
    <stdio.h>
    #include
    <stdlib.h>

    typedef
    int ElemType;

    //////////////////////////////////////////

    //定义队列结点类型

    typedef
    struct Qnode
    {
    ElemType data;
    struct Qnode *next;
    } Qnode;

    ///定义队列结点的头指针,为指针

    typedef
    struct
    {
    Qnode
    *front;
    Qnode
    *rear;
    }LQueue,
    *LinkQueue;

    //////////////////////////////////////////

    //队列初始化

    LinkQueue LinkQueueInit()
    {
    LinkQueue Q;
    Qnode
    *P;
    Q
    = (LinkQueue)malloc(sizeof(LQueue));//申请头,尾指针结点
    P = (Qnode *)malloc(sizeof(Qnode));//申请头结点
    P->next = NULL;
    Q
    ->front = Q->rear = P;
    return Q;
    }

    /////////////////////////////////////////

    //入队

    void LinkQueueEnter(LinkQueue Q,ElemType x)
    {
    Qnode
    *p;

    p
    = (Qnode *)malloc(sizeof(Qnode));//申请新结点
    p->data = x;
    p
    ->next = NULL;

    Q
    ->rear->next = p;

    Q
    ->rear = p;

    }

    /////////////////////////////////////////

    //出队

    ElemType LinkQueueOut(LinkQueue Q)
    {
    ElemType x;
    Qnode
    *p;
    if(Q->front != Q->rear)
    {
    p
    = Q->front->next;;
    x
    = p->data;
    Q
    ->front->next = p->next;//移动头指针
    free(p);
    if(Q->front->next == NULL)//最后一个元素出队后,队空,修改队尾指针
    Q->rear = Q->front;
    }
    else
    return 0;
    return x;
    }
    int main()
    {
    LinkQueue lqueue;
    lqueue
    = LinkQueueInit();
    ElemType x;
    printf(
    "请输入入队列的元素:");
    while(scanf("%d",&x) != EOF)
    {
    LinkQueueEnter(lqueue,x);
    }
    Qnode
    *p;

    for(p = lqueue->front->next; p != lqueue->rear->next; p = p->next )
    printf(
    "%d ",p->data);
    printf(
    "出队列的结果为:");
    while(lqueue->front!= lqueue->rear)
    {

    printf(
    "%d ",LinkQueueOut(lqueue));
    }

    return 0;
    }

  • 相关阅读:
    delphi TMemoryStream读取html文件中文乱码
    tfrxbarcode2dview 和 tfrxbarcode not found 解决办法
    delphi ForceDirectories 用法
    Java Scanner next() 与 nextLine() 区别
    delphi 用superobject解析json
    delphi 方法可以提前声明
    delphi 判断文件是否被占用
    mysql explain用法和结果的含义
    C# SpinWait
    js异步编程
  • 原文地址:https://www.cnblogs.com/newwy/p/1847463.html
Copyright © 2011-2022 走看看