码迷,mamicode.com
首页 > 其他好文 > 详细

入队/出队

时间:2015-06-09 21:29:16      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

入队/出队操作

#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==\n)break;
    }
    while(p->rear!=NULL)
    {
        p=pop_queue(p);
    }
    return 0;
}

 

入队/出队

标签:

原文地址:http://www.cnblogs.com/riden/p/4564446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!