数据结构之链队列

#include <iostream>


using namespace std;


struct LinkNode
{
int data;
LinkNode *next;
};


struct LinkQueue
{
LinkNode *front;
LinkNode *rear;
};


void init(LinkQueue *&Q)
{
Q=new LinkQueue;
Q->front=new LinkNode;


Q->front->next=NULL;
Q->rear=Q->front;
}


bool isEmpty(LinkQueue *Q)
{
return Q->front==Q->rear;
}


void push(LinkQueue *&Q,int data)
{
LinkNode *p=new LinkNode;
p->data=data;
p->next=NULL;


Q->rear->next=p;
Q->rear=p;
}


void pop(LinkQueue *&Q)
{
LinkNode *p=Q->front->next;
Q->front->next=p->next;
if(Q->rear==p)
Q->rear=Q->front;
delete p;
}


int getdata(const LinkQueue *Q)
{
return Q->front->next->data;
}


void test(LinkQueue *&Q)
{
int i=1;
while(i)
{
cin>>i;
if(i>0)
push(Q,i);
if(i<0)
{
cout<<"getdata="<<getdata(Q)<<endl;
pop(Q);
}
if(i==0)
break;
}
}


void show(const LinkQueue *Q)
{
cout<<endl;
while(Q->front->next)
{
cout<<Q->front->next->data<<" ";
Q->front->next=Q->front->next->next;
}

cout<<endl;
}


int main()
{
LinkQueue *Q;
init(Q);
test(Q);
show(Q);


return 0;
}

猜你喜欢

转载自blog.csdn.net/wrc_nb/article/details/80098372
今日推荐