單鏈表隊列

來源:互聯網
上載者:User
#include <stdio.h>#include <malloc.h>#include <stdlib.h>#define STACK_SIZE 100#define STACK_INCREASE 10#define OK -100#define ERROR -1//--------------------------------------------------------------------------typedef int QElemType; //定義取決與對象是什麼typedef int Status;typedef struct QNode{    QElemType data;    struct QNode * next;}QNode, * QueuePtr;typedef struct{    QueuePtr front; //隊頭指標    QueuePtr rear; //隊尾指標}LinkQueue;//--------------------------------------------------------------------------Status InitQueue( LinkQueue &Q );//初始化隊列Status DestroyQueue( LinkQueue &Q );//銷毀隊列Status EnQueue( LinkQueue &Q, QElemType e );//插入元素到隊尾Status ClearQueue( LinkQueue &Q );//清空隊列Status QueueEmpty( LinkQueue Q );//判斷是否為空白int QueueLength( LinkQueue Q );//求隊列元素個數Status GetHead( LinkQueue Q, QElemType &e );//取得對頭元素Status DeQueue( LinkQueue Q, QElemType &e );//刪除對頭元素void status_( int sat );//--------------------------------------------------------------------------int main(){    int choice, status, e;    LinkQueue Q;    puts( "No1 InitQueue" );    puts( "No2 DestroyQueue" );    puts( "No3 En Queue (input)" );    puts( "No4 ClearQueue " );    puts( "No5 is empty?" );    puts( "No6 QueueLength" );    puts( "No7 GetHead" );    puts( "No8 DeQueue" );    puts( "No9 exit" );    printf( "please choose a number:" );    scanf( "%d", &choice);    while( 1 )    {           switch( choice )           {                case 1:        status = InitQueue( Q );        status_( status );        break;                case 2:status = DestroyQueue( Q );status_( status );break;case 3:printf( "input a number:" );scanf( "%d", &e );status = EnQueue( Q, e );status_( status );break;case 4:status = ClearQueue( Q );status_( status );break;case 5:status = QueueEmpty( Q );status_( status );break;case 6:status = QueueLength( Q );status_( status );break;case 7:status = GetHead( Q, e );printf( "%d\n", e);status_( status );break;case 8:status = DeQueue( Q, e );printf( "the del number is %d\n", e);status_( status );break;case 9:exit(-1);break;default:printf("wrong input exit!!\n");exit(-1);break;  }printf( "please choose a number:" );scanf( "%d", &choice );    }    return 0;}void status_( int sat )//status 狀態判斷{if( OK == sat )printf( "OK!\n" );else if( ERROR == sat )printf( "error\n" );elseprintf( "%d\n", sat );}Status InitQueue( LinkQueue & Q)//初始化隊列{Q.front = Q.rear = ( QueuePtr )malloc( sizeof(QNode) );if( !Q.front )return ERROR;elseQ.front->next = NULL;return OK;}Status DestroyQueue( LinkQueue &Q ) //銷毀隊列{while( Q.front ){Q.rear = Q.front->next;free( Q.front );Q.front = Q.rear;}return OK;}Status EnQueue( LinkQueue &Q, QElemType e )//插入元素到隊尾{QueuePtr p;p = ( QueuePtr )malloc( sizeof(QNode) );if( !p )return ERROR;else{p->data = e;p->next = NULL;Q.rear->next = p;Q.rear = p;}return OK;}Status ClearQueue( LinkQueue &Q )//清空隊列{if( Q.front == Q.rear ){puts( "the Queue is empty" );return OK;}else{QueuePtr p;p = Q.front;while( p != NULL ){free( p );p = p->next;}}return OK;}Status QueueEmpty( LinkQueue Q )//判斷是否為空白{if( Q.front == Q.rear )puts( "is empty " );elseputs( "unempty" );return OK;}int QueueLength( LinkQueue Q )//求隊列元素個數{int i = -1;QueuePtr p = Q.front;while( p != NULL ){p = p->next;i++;}return i;}Status GetHead( LinkQueue Q, QElemType &e )//取得對頭元素{if( Q.front == Q.rear ){puts( "empty" );return ERROR;}else{e = Q.front->next->data;}return OK;}Status DeQueue( LinkQueue Q, QElemType &e )//刪除對頭元素{QueuePtr p = Q.front;if( Q.front == Q.rear ){puts( "empty" );return ERROR;}else{p = p->next;e = p->data;Q.front->next = p->next;if( Q.rear == p )Q.rear == Q.front;free( p );}return OK;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.