zoukankan      html  css  js  c++  java
  • 链式队列定义、插入、删除

    #include <stdio.h>
    #include <stdlib.h>
    
    #define ElemType int
    typedef struct QNode
    {
        ElemType data;//定义队列中的元素
        struct QNode *next;
    }QNode;
    
    typedef struct LinkQueue
    {
        QNode *front;
        QNode *rear;
    }LinkQueue;//定义一个队列,队列只有一个头指针和一个尾指针
    
    
    //队列的初始化
    void InitQueue(LinkQueue *q)
    {
        q->front = (LinkQueue*)malloc(sizeof(QNode));
        if(!q->front)
        {
            exit(0);
        }
        q->rear = q->front;
        q->front->next = NULL;
    }
    
    //队列的插入
    void InsertQueue(LinkQueue *p,ElemType *e)
    //队列为p,要插入的元素为e,队列的rear指针指向队列的最后一个元素。队列的front指针指向头结点,头结点无元素
    {
        QNode *ptr;
        ptr = (LinkQueue*)malloc(sizeof(QNode));
        ptr->data = *e;
        ptr->next = NULL;
        p->rear->next = ptr;
    //    p->rear = p->rear->next;
        p->rear = ptr;
    }
    
    //队列的删除
    void DeleteQueue(LinkQueue *p,ElemType *e)
    {
        QNode *s;
        if(p->front == p->rear)
        {
            return;
        }
        s = p->front->next;
        *e = s->data;
        p->front->next = s->next;
     //   if(s == p->rear)
        if(p->front->next == NULL)//用这条也可以是等价的
        {
            p->front = p->rear;
        }
        free(s);
    }
    
    //销毁一个队列
    void DestoryQueue(LinkQueue *p)
    {
        while(p->front)
        {
            p->rear = p->front->next;//p->front->next == NULL
            free(p->front);//释放p的头结点
            p->front = p->rear;//把p的front和rear都置为NULL
        }
    
    }
    int main()
    {
        int i;
        LinkQueue p;
        ElemType e;
        InitQueue(&p);
        for(i = 1;i <= 10;i++)
        {
            InsertQueue(&p,&i);
        }
    
        for(i = 1;i <= 10;i++)
        {
            DeleteQueue(&p,&e);
            printf("%2d",e);
        }
        return 0;
    }
    

     输入一个字符串以字符'#'结束,在屏幕上打印出来

    #include <stdio.h>
    #include <stdlib.h>
    
    #define M 100
    #define ElemType char
    typedef struct QNode
    {
        ElemType data;//定义队列中的元素
        struct QNode *next;
    }QNode;
    
    typedef struct LinkQueue
    {
        QNode *front;
        QNode *rear;
    }LinkQueue;//定义一个队列,队列只有一个头指针和一个尾指针
    
    
    //队列的初始化
    void InitQueue(LinkQueue *q)
    {
        q->front = (LinkQueue*)malloc(sizeof(QNode));
        if(!q->front)
        {
            exit(0);
        }
        q->rear = q->front;
        q->front->next = NULL;
    }
    
    //队列的插入
    void InsertQueue(LinkQueue *p,ElemType *e)
    //队列为p,要插入的元素为e,队列的rear指针指向队列的最后一个元素。队列的front指针指向头结点,头结点无元素
    {
        QNode *ptr;
        ptr = (LinkQueue*)malloc(sizeof(QNode));
        ptr->data = *e;
        ptr->next = NULL;
        p->rear->next = ptr;
    //    p->rear = p->rear->next;
        p->rear = ptr;
    }
    
    //队列的删除
    void DeleteQueue(LinkQueue *p,ElemType *e)
    {
        QNode *s;
        if(p->front == p->rear)
        {
            return;
        }
        s = p->front->next;
        *e = s->data;
        p->front->next = s->next;
     //   if(s == p->rear)
        if(p->front->next == NULL)//用这条也可以是等价的
        {
            p->front = p->rear;
        }
        free(s);
    }
    
    //销毁一个队列
    void DestoryQueue(LinkQueue *p)
    {
        while(p->front)
        {
            p->rear = p->front->next;//p->front->next == NULL
            free(p->front);//释放p的头结点
            p->front = p->rear;//把p的front和rear都置为NULL
        }
    
    }
    int main()
    {
        int i,len = 0;
        LinkQueue p;
        ElemType e;
        char c;
        InitQueue(&p);
        printf("输入字符串:
    ");
        scanf("%c",&c);
        while(c != '#')
        {
            InsertQueue(&p,&c);
            len++;
            scanf("%c",&c);
        }
        printf("字符串为:
    ");
        for(i = 1;i <= len;i++)
        {
            DeleteQueue(&p,&e);
            printf("%c",e);
        }
        return 0;
    }
    

      

     

  • 相关阅读:
    google 地图 v2
    javascript 跨域名 异常:
    cvs 使用规范
    fastcgi apache fcgi
    玄幻系列
    必须掌握的命令行(转)
    浏览器,RIA,flash,flex,siliverlight,socket
    游戏外挂的一些原理
    shtml我几乎要忘了的东西
    北京下雨了,兴奋中
  • 原文地址:https://www.cnblogs.com/devinblog/p/4165780.html
Copyright © 2011-2022 走看看