• 入队/出队


    入队/出队操作

    #include<iostream>
    using namespace std;
    struct node
    {
        int data;
        node *next;
    };
    struct queue
    {
        node *head;
        node *rear;
    };
    //入队
    queue *push_queue(queue *team,int x)
    {
        node *s=new node;
        s->data=x;
        s->next=NULL;
        if(team->rear==NULL)
        {
            team->head=s;
            team->rear=s;
        }
        else
        {
            team->rear->next=s;
            team->rear=s;
        }
        return team;
    }
    //出队
    queue *pop_queue(queue *team)
    {
        int x;
        node *s=new node;
        if(team->head==NULL)
        {
            cout<<"empty"<<endl;
        }
        else
        {
            x=team->head->data;
            s=team->head;
            if(team->head==team->rear)
            {
                team->head=NULL;
                team->rear=NULL;
            }
            else
            {
                team->head=team->head->next;
                delete s;
            }
            cout<<x<<endl;
            return  team;
        }
    }
    int main()
    {
    
        queue *H=new queue;
        H->rear=NULL;
        H->head=NULL;//建一个空队
        cout<<"Input the data pushed to queue"<<endl;
        int x;char c;
        queue *p;
        while(cin>>x)
        {
            p=push_queue(H,x);
            cin.get(c);
            if(c=='
    ')break;
        }
        while(p->rear!=NULL)
        {
            p=pop_queue(p);
        }
        return 0;
    }
  • 相关阅读:
    AJAX学习笔记
    JQuery 学习笔记-2017.05.22
    十二.GUI
    十一.文件
    十.模块和库
    九.类的进化(魔法方法、特性和迭代器)
    八.异常
    七.类的继承
    六.函数和类
    五.条件、循环和其他语句
  • 原文地址:https://www.cnblogs.com/riden/p/4564446.html
Copyright © 2020-2023  润新知