posts - 0,comments - 0,trackbacks - 0
#include<iostream>
#define maxSize 10
using namespace std;
typedef struct QNode
{
    int data;
    struct QNode *next;
}QNode;
typedef struct LiQueue
{
    QNode *front;
    QNode *rear;
}LiQueue;

void InitQueue(LiQueue *&lqu)
{
    lqu=(LiQueue*)malloc(sizeof(LiQueue));
    lqu->front=lqu->rear=NULL;
}
int QueueEmpty(LiQueue *lqu)
{
    if(lqu->rear==NULL||lqu->front==NULL)
        return 1;
    else return 0;
}
void enQueue(LiQueue *&lqu,int x)
{
    QNode *p;
    p=(QNode*)malloc(sizeof(QNode));
    p->data=x;
    p->next=NULL;
    if(lqu->rear==NULL)
        lqu->front=lqu->rear=p;
    else
    {
        lqu->rear->next=p;
        lqu->rear=p;
    }
}
int deQueue(LiQueue *&lqu,int &x)
{
    QNode *p;
    if(lqu->rear==NULL)
        return 0;
    p=lqu->front;
    if(lqu->front==lqu->rear)
        lqu->front=lqu->rear=NULL;
    else
        lqu->front=lqu->front->next;
    x=p->data;
    free(p);
    return 1;
}
void print(LiQueue *lqu)
{
    cout<<"队列的打印:";
    QNode *q=lqu->front;
    while(q!=lqu->rear)
    {
        cout<<q->data<<"  ";
        q=q->next;
    }
    cout<<lqu->rear->data<<endl;
}
int main()
{
    LiQueue *li;
    InitQueue(li);
    enQueue(li,1);
    enQueue(li,2);
    enQueue(li,3);
    print(li);

    int x;
    deQueue(li,x);
    cout<<"队列删除队首元素:"<<x<<endl;
    print(li);

    enQueue(li,4);
    enQueue(li,5);
    print(li);
    return 1;
}
posted on 2012-08-25 08:53 yyj 阅读(162) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理