zoukankan      html  css  js  c++  java
  • 链式存储队列

    #include <iostream>
    #include <assert.h>
    using namespace std;
    typedef struct node
    {
        int data;
        struct node *next;
    }NODE;
    typedef struct queue
    {
        NODE* head;
        NODE* tail;
    }QUEUE;
    QUEUE* create_queue()
    {
        QUEUE* queue =  new QUEUE;
        queue->head = NULL;
        queue->tail = NULL;
        return queue;
    }
    NODE* create_node(int data)
    {
        NODE* node = new NODE;
        node->data = data;
        node->next = NULL;
        return node;
    }
    void queue_push(QUEUE* queue,int data)
    {
        NODE* node = create_node(data);
        if(queue->tail == NULL)
        {
            queue->tail = node;
            queue->head = node;
        }
        else
        {
            queue->tail->next = node;
            queue->tail = node;
        }
    }
    NODE* destroy_node(NODE* node)
    {
        NODE* next = node->next;
        delete node;
        return next;
    }
    int queue_pop(QUEUE* queue)
    {
        if(queue->head == NULL)
        {
            assert(false);
        }
        int data = queue->head->data;
        queue->head =destroy_node(queue->head);
        if(queue->head == NULL)
        {
            queue->tail = NULL;
        }
        return data;
    }
    int queue_size (QUEUE* queue)
    {
        int size = 0;
        NODE* node = NULL;
        for(node=queue->head; node;node=node->next)
        {
            size++;
        }
        return size;
    }
    void clear(QUEUE* queue)
    {
        while(queue->head)
        {
            queue->head = destroy_node(queue->head);
        }
        queue->tail = NULL;
    }
    void destroy_queue(QUEUE* queue)
    {
        clear(queue);
        delete queue;
    }
    bool queue_empty(QUEUE* queue)
    {
        if( (queue->head==NULL) && (queue->tail==NULL))
        {
            return true;
        }
        return false;
    }
    
    int main()
    {
        QUEUE* queue = create_queue();
        cout<<"input number:  ";
        for(int i=0;i<10;i++)
        {
            queue_push(queue,i);
            cout<<i<<" ";
        }
        cout<<endl;
        //queue_pop(queue);  //
        cout<<"output number:";
        while(! queue_empty(queue))
        {
            cout<<queue_pop(queue)<<" ";
        }
        cout<<endl;
        destroy_queue(queue);
        return 0;
    }

    关注公众号 海量干货等你
  • 相关阅读:
    排列组合STL实现——pku1731
    迷宫搜索变型——【USACO5.2.1】蜗牛的旅行
    知道地球两点算距离——fzu2016
    最长递增子序列——pku2533
    dfs+dp——[Usaco2008 Mar]Cow Travelling游荡的奶牛
    最长不降子序列变型——[Usaco2008 Feb]Eating Together麻烦的聚餐
    规律题——[Usaco2008 Oct]建造栅栏
    简单dp——[Usaco2008 Mar]River Crossing渡河问题
    poj1317
    poj1068
  • 原文地址:https://www.cnblogs.com/sowhat1412/p/12734510.html
Copyright © 2011-2022 走看看