您的位置:首页 > 理论基础 > 数据结构算法

数据结构_链队列相关操作

2014-07-15 22:34 239 查看
#include<stdio.h>

#include<stdlib.h>

typedef struct Node

{

        int data;

        struct Node *next;

}Node;

typedef struct

{

        Node * front;

        Node * rear;

}Queue;

void InitQueue(Queue &Q)

{

    Node *p=(Node*)malloc(sizeof(Node));

    p->next = NULL;

     Q.front = p;

     Q.rear=p;

}

int QueueEmpty(Queue Q)

{

  return  Q.rear==Q.front ? 1 : 0;

}

void EnQueue(Queue &Q, int e)

{

     Node *p=(Node*)malloc(sizeof(Node));

     p->data = e;

     p->next =NULL;

    

     Q.rear->next = p;

     Q.rear = p;

}

void Visit(Queue Q)

{

     Node *p=Q.front->next;

     while(NULL !=p)

     {

      printf("%d\n",p->data);

      p=p->next;          

     }

}

int DeQueue(Queue &Q, int &e)

{

    Node *p=Q.front->next;

    if(Q.front == Q.rear)

    {

               return 0;

    }

    e = p->data;

   

    Q.front->next = p->next;

    if(Q.rear==p)

    {

      Q.rear = Q.front;

    }

    

    free(p);

    

    return 1;

}

int main(void)

{

    const int N = 10;

    int i;

    int e;

    Queue Q;

    InitQueue(Q);

    

    for(i=0;i<N;++i)

    {

        EnQueue(Q,i);

    }

    

    for(i=0;i<N;++i)

   {

          DeQueue(Q,e); 

          printf("data[%d]=%d\n",i,e);

    }

    

    system("pause");

    return 0;

    

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息