鏈隊列的實現

來源:互聯網
上載者:User

標籤:鏈隊列   資料結構   

         //------------------------------隊列----------------------------------------//
//隊列與棧相反,是一種先進先出(FIFO)的線性表。它只允許在表的一端進行插入,而在另一端刪除元素
//允許插入的一端叫做隊尾(rear),允許刪除的一端叫做隊頭(front)
//給鏈隊列增加一個頭結點,並令頭指標指向頭結點。空的鏈隊列的判決條件:頭指標和尾指標均指向頭結點
//鏈隊列的操作即為單鏈表的插入和刪除操作的特殊情況

//-----------------------------------------------------------------------------//

function.h

#define Status int#define OVERFLOW -2#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0typedef int QElemType;typedef struct QNode{        QElemType data;        struct QNode *next;}QNode, *QueuePtr;typedef struct{       QueuePtr front;       QueuePtr rear;}LinkQueue;void visit_print(QueuePtr p);Status InitQueue(LinkQueue *Q);   //構造Status DestroyQueue(LinkQueue *Q);   //銷毀Status ClearQueue(LinkQueue *Q);     //清空Status QueueEmpty(LinkQueue Q); // 為空白返回TRUE,否則返回FALSEStatus QueueLength(LinkQueue Q);//返回隊列長度Status GetHead(LinkQueue Q, QElemType *e);   // 返回隊列的頭元素Status EnQueue(LinkQueue *Q, QElemType e);   //插入元素e為新的隊尾元素Status DeQueue(LinkQueue *Q, QElemType *e);//若隊列不為空白返回隊頭元素,否則返回ERRORStatus QueueTraverse(LinkQueue Q,  void (*visit)(QueuePtr) );//從隊頭到隊尾的隊列中每個元素調用函數visit(),一旦失敗則操作失敗

function.c

#include "function.h"#include <stdio.h>#include <stdlib.h>void visit_print(QueuePtr p){        printf("%d   ", p->data);}Status QueueEmpty(LinkQueue Q){        if( Q.front == Q.rear)        {                printf("the queue is empty!\n");                return OK;        }        else                 return ERROR;}Status InitQueue(LinkQueue *Q){        Q->front = Q->rear = (QueuePtr ) malloc ( sizeof ( QNode ) );//產生一個頭結點,資料域為空白        if( !Q->front )          exit(OVERFLOW);  //分配失敗        Q->front->next=NULL;        return OK;}Status EnQueue(LinkQueue *Q, QElemType e){        QueuePtr p;        p=(QueuePtr ) malloc (sizeof (QNode ));        p->data = e;        p->next = NULL;        Q->rear->next = p;        Q->rear = p;//尾指標後移        return OK;}Status DeQueue(LinkQueue *Q, QElemType *e){        if( Q->front == Q->rear)          return ERROR;        QueuePtr p= (QueuePtr ) malloc (sizeof (QNode ));        p = Q->front->next;        *e = p->data;        Q->front->next = p->next;//頭指標後移        if( Q->rear == p)        Q->rear = Q->front;        free(p);        return OK;}Status DestroyQueue(LinkQueue *Q){       QueuePtr p= Q->front = Q->rear;       free(p);       return OK;}Status ClearQueue(LinkQueue *Q){        QElemType *e=(QElemType *)malloc(sizeof(QElemType));      while( Q->front != Q->rear)      {              DeQueue( Q,  e);      }      return OK;}int  QueueLength(LinkQueue Q){        int length=0;        while(Q.front != Q.rear)        {              Q.front = Q.front->next;              length++;        }        return length;}Status GetHead(LinkQueue Q, QElemType *e){        if( Q.front == Q.rear)          return ERROR;        *e = Q.front->next->data;        return OK;}Status QueueTraverse(LinkQueue Q, void (*visit)(QueuePtr)){        if( Q.front == Q.rear)          return ERROR;        QNode *p;        for( p= Q.front->next;p; p= p->next)        {                visit(p);        }        printf("\n");        return OK;}

main.c

//------------------------------隊列----------------------------------------////隊列與棧相反,是一種先進先出(FIFO)的線性表。它只允許在表的一端進行插入,而在另一端刪除元素//允許插入的一端叫做隊尾(rear),允許刪除的一端叫做隊頭(front)//給鏈隊列增加一個頭結點,並令頭指標指向頭結點。空的鏈隊列的判決條件:頭指標和尾指標均指向頭結點//鏈隊列的操作即為單鏈表的插入和刪除操作的特殊情況//-----------------------------------------------------------------------------//#include <stdio.h>#include <stdlib.h>#include "function.h"int main(void){        LinkQueue Q;        QElemType e;        int i;        InitQueue(&Q);        QueueEmpty(Q);       for(i=0;i<5;i++)        {                scanf("%d ", &e);                EnQueue(&Q, e);        }        printf("the queue are:");        QueueTraverse(Q, visit_print);        printf("the queue's length is %d \n", QueueLength(Q));        GetHead(Q ,&e);        printf("the head member of queue is %d\n ", e);        DeQueue(&Q, &e);        printf("delete the head member in queue is %d\n", e);        printf("after delete the head node ,queue are:");        QueueTraverse(Q, visit_print);        printf("the queue length is %d\n", QueueLength(Q));        ClearQueue(&Q);         QueueEmpty(Q);        return 0;}

運行結果:


鏈隊列的實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.