Asp.net 学习资料

伦惠峰

单链表队列

#include <iostream.h>
#include <stdlib.h>

typedef struct QNode
{
    int data;
    struct QNode *next;
}QNode,*QueuePtr;

typedef struct
{
    QueuePtr front;  //队头指针
    QueuePtr rear;  //队尾指针
}LinkQueue;

LinkQueue Q;

void InitQueue(LinkQueue &Q)  //构造一个空队列Q
{
    Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
    if(!Q.front)    //存储分配失败
      exit(0);
    Q.front->next=NULL;
}

void EnQueue(LinkQueue &Q,int e)  //插入元素e为Q的新的队尾元素
{
    QueuePtr p;
    p=(QueuePtr)malloc(sizeof(QNode));
    if(!p)
      exit(0);
    p->data=e;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
}

void Print()
{
    Q.front=Q.front->next;
    while(Q.front!=Q.rear)
    {
        cout<<Q.front->data<<" ";
        Q.front=Q.front->next;
    }
    cout<<Q.rear->data<<endl;
}

void QueueTraverse()            //遍历队列
{
    Print();
}

void main()
{
    int e;
    InitQueue(Q);
    for(int i=0;i<10;i++)
      EnQueue(Q,i);      //插入队列0~9
    QueueTraverse();
}

posted on 2007-08-13 21:36  伦惠峰  阅读(151)  评论(0)    收藏  举报

导航