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;
    }

    --

  • 相关阅读:
    web服务之NginX介绍
    LVS介绍以及工作模式案例
    sersync 实现实时数据同步
    Java高并发20-并发包中锁原理解析(二)
    Java高并发19-并发包中锁原理解析(一)
    从零开始学VUE之VueRouter(导航守卫)
    从零开始学VUE之VueRouter(传递参数)
    从零开始学VUE之VueRouter(嵌套路由)
    从零开始学VUE之VueRouter(路由懒加载)
    从零开始学VUE之VueRouter(动态路由)
  • 原文地址:https://www.cnblogs.com/Ph-one/p/6889841.html
Copyright © 2011-2022 走看看