#include
using namespace std;
typedef struct node
{
int data;
struct node *next;
}Node;
typedef struct queue
{
Node *front;
Node *rear;
}MyQueue;
MyQueue *CreatMyQueue()//creat a empty queue
{
MyQueue *q=new MyQueue;
q->front=NULL;
q->rear=NULL;
return q;
}
MyQueue *PushMyQueue(MyQueue *q,int data)
{
Node *temp=NULL;
temp=new Node;
temp->data=data;
temp->next=NULL;
if(q->rear==NULL)//if is a empty queue
q->front=q->rear=temp;
else
{
q->rear->next=temp;
q->rear=temp;
}
return q;
}
MyQueue *PopMyQueue(MyQueue *q)
{
Node *temp=NULL;
temp=q->front;
if(temp==NULL)
cout<<"empty queue";
else
{
q->front=q->front->next;
if(q->front==NULL)//only one Node
q->rear=NULL;
delete temp;
}
return q;
}
int PrintMyQueue(MyQueue *q)
{
Node *temp=q->front;
if(temp==NULL)
{
cout<<"empty queue"< return 1;
}
cout<<"data are:"<
while(temp!=q->rear)
{
cout<data< temp=temp->next;
}
cout<data< return 0;
}
int main(void)
{
MyQueue *tp=CreatMyQueue();
PushMyQueue(tp,1);
PushMyQueue(tp,2);
PushMyQueue(tp,3);
PrintMyQueue(tp);
PopMyQueue(tp);
PopMyQueue(tp);
PrintMyQueue(tp);
return 0;
}
阅读(847) | 评论(0) | 转发(1) |