队列

  1 typedef  struct QNode
  2 {
  3         QElemType data;
  4         struct QNode *next;
  5 
  6 }QNode,*QueuePtr;
  7 
  8 
  9 typedef struct
 10 {
 11         QueuePtr front, rear;
 12 }LinkQueue;
 13 
 14 
 15 void
 16 InitQueue(LinkQueue *Q)
 17 {
 18         Q->front = Q->next = malloc(sizeof( struct QNode));
 19         if(! Q->front)
 20                 printf("error");
 21 
 22         Q->front->next = NULL;
 23 }
 24 
 25 void
 26 DestoryQueue(LinkQueue *Q)
 27 {
 28         while( Q->front)
 29         {
 30                 Q->rear = Q->front->next;
 31                 free(Q->front);
 32                 Q->front = Q->rear;
 33         }
 34 }
 35 
 36 
 37 
 38 void
 39 clearQueue(LinkQueue *Q)
 40 {
 41         QueuePtr p,q;
 42         Q->rear = Q->front;
 43         p = Q->front->next;
 44         Q->front->next =NULL;
 45         while( p)
 46         {
 47                 q = p;
 48                 p = p->next;
 49                 free(q);
 50         }
 51 }
 52 
 53 bool
 54 QueueEmpty(LinkQueue Q)
 55 {
 56         if(Q.front->next == NULL)
 57                 return true;
 58         else
 59                 return false;
 60 }
 61 
 62 
 63 int
 64 QueueLength(LinkQueue Q)
 65 {
 66         int i =0;
 67         QueuePtr p;
 68         p = Q.front;
 69         while(Q.rear != p)
 70         {
 71                 i++;
 72                 p= p->next;
 73         }
 74         return i;
 75 }
 76 
 77 
 78 Status
 79 GetHead_Q( LinkQueue Q, QElemType *e)
 80 {
 81     QueuePtr p;
 82     if(Q.front == Q.rear)
 83             return ERROR;
 84     p = Q.front->next;
 85     *e = p->data;
 86     return Ok;
 87 }
 88 
 89 
 90 
 91 void
 92 EnQueue(LinkQueue *Q, QElemType e)
 93 {
 94         QueuePtr p = malloc(sizeof(struct Qnode));
 95         if( !p )
 96                 printf("Error");
 97         p->data = e;
 98         p->next = NULL;
 99         Q->rear->next = p;
100         Q->rear = p;
101 }
102 
103 Status
104 DeQueue(LinkQueue *Q, QElemType *e)
105 {
106         QueuePtr p;
107         if( Q->front == Q->rear)
108                 printf("ERROR");
109         p =Q->front->next;
110         *e =p->data;
111         Q->front->next = p->next;
112         if(Q->rear == p)
113                 Q->rear = Q->front;
114         free(p);
115         return Ok;
116 }
posted @ 2012-09-14 20:04  风去无痕  阅读(129)  评论(0编辑  收藏  举报