隊列ADT

/***************利用數(shù)組模擬循環(huán)隊列***************/

#include "stdio.h"

#define MAXSIZE 10

typedef struct

{

? ? int data[MAXSIZE];//數(shù)據(jù)的存儲區(qū)

? ? int front,rear;//隊頭對尾指針

}c_SeQueue;//循環(huán)隊

void Init_SeQueue(c_SeQueue *q)//初始化隊列

{

? ? q->front=q->rear=0;

}

int In_SeQueue(c_SeQueue *q,int x)//入隊列

{

? ? if((q->rear+1)%MAXSIZE==q->front)

? ? {

? ? ? ? printf("隊滿");

? ? ? ? return 0;

? ? }

? ? else

? ? {

? ? ? ? q->rear=(q->rear+1)%MAXSIZE;

? ? ? ? q->data[q->rear]=x;

? ? ? ? return 1;//入隊完成

? ? }

}

int Out_SeQueue(c_SeQueue *q,int *x)//出隊列

{

? ? if(q->rear==q->front)

? ? {

? ? ? ? printf("隊空");

? ? ? ? return 0;//隊空不能出隊

? ? }

? ? else

? ? {

? ? ? ? q->front=(q->front+1)%MAXSIZE;

? ? ? ? *x=q->data[q->front];//讀出隊頭元素

? ? ? ? return 1;//出隊完成

? ? }

}

int Empty_SeQueue(c_SeQueue *q)//判隊空

{

? ? if(q->rear==q->front)

? ? ? ? return 1;

? ? else

? ? ? ? return 0;

}

int Full_SeQueue(c_SeQueue *q)//判隊滿

{

? ? if((q->rear+1)%MAXSIZE==q->front)

? ? ? ? return 1;

? ? else

? ? ? ? return 0;

}

int main()

{

? ? c_SeQueue q,* cq=&q;

? ? int select,i;

? ? int y,z;

? ? Init_SeQueue(cq);

? ? printf("\n(1)Input data");

? ? printf("\n(2)Output data");

? ? printf("\n(3)Exit");

? ? printf("\nPlease select ont=>");

? ? scanf("%d",&select);

? ? do

? ? {

? ? ? ? switch(select)

? ? ? ? {

? ? ? ? ? ? case 1:printf("\nPlease input the data=>");

? ? ? ? ? ? ? ? scanf("%d",&y);

? ? ? ? ? ? ? ? In_SeQueue(cq,y);

? ? ? ? ? ? ? ? printf("\nthe elements are:\n");

? ? ? ? ? ? ? ? for(i=(cq->front+1)%MAXSIZE;i!=(cq->rear+1)%MAXSIZE;i=(i+1)%MAXSIZE)

? ? ? ? ? ? ? ? ? ? printf("%d ",cq->data[i]);//輸出隊列元素

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case 2:Out_SeQueue(cq,&z);

? ? ? ? ? ? ? ? printf("\nthe elments are:\n");

? ? ? ? ? ? ? ? for(i=(cq->front+1)%MAXSIZE;i!=(cq->rear+1)%MAXSIZE;i=(i+1)%MAXSIZE)

? ? ? ? ? ? ? ? ? ? printf("%d ",cq->data[i]);//輸出隊列元素

? ? ? ? ? ? ? ? break;

? ? ? ? }

? ? ? ? printf("\n(1)Input data");

? ? ? ? printf("\n(2)Output data");

? ? ? ? printf("\n(3)Exit");

? ? ? ? printf("\nPlease select ont=>");

? ? ? ? scanf("%d",&select);

? ? ? ? printf("\n");

? ? }

? ? while(select!=3);

}




/***************利用鏈表模擬鏈隊列***************/

#include "stdio.h"

#include<stdlib.h>

#define MAXSIZE 10

typedef struct node

{

? ? int data;

? ? struct node *next;

}QNode;//鏈隊結點的類型

typedef struct

{

? ? QNode *front,*rear;

}LQueue;//將頭尾指針封裝在一起的鏈隊

void Init_LQueue(LQueue *q)

{

? ? q->front=(QNode *)malloc(sizeof(QNode));//申請鏈隊頭結點

? ? q->front->next=NULL;

? ? q->rear=q->front;

}

void In_LQueue(LQueue *q,int x)

{

? ? QNode *p;

? ? p=(QNode *)malloc(sizeof(QNode));//申請新結點

? ? p->data=x;

? ? p->next=NULL;

? ? q->rear->next=p;

? ? q->rear=p;

}

int Empty_LQueue(LQueue *q)

{

? ? if(q->front==q->rear)

? ? ? ? return 1;

? ? else

? ? ? ? return 0;

}

int Out_LQueue(LQueue *q,int *x)

{

? ? QNode *p;

? ? if(Empty_LQueue(q))

? ? {

? ? ? ? printf("隊空");

? ? ? ? return 0;

? ? }//隊空,出隊失敗

? ? else

? ? {

? ? ? ? p=q->front->next;

? ? ? ? q->front->next=p->next;

? ? ? ? *x=p->data;//隊頭元素放x中

? ? ? ? free(p);

? ? ? ? if(q->front->next==NULL)

? ? ? ? ? ? q->rear=q->front;//只有一個元素時,出隊后隊空,此時要修改隊尾指針

? ? ? ? return 1;

? ? }

}

int main()

{

? ? LQueue q,*lq=&q;

? ? QNode *p;

? ? int select;

? ? int y,z;

? ? Init_LQueue(lq);

? ? printf("\n(1)Input data");

? ? printf("\n(2)Output data");

? ? printf("\n(3)Exit");

? ? printf("\nPlease select one=>");

? ? scanf("%d",&select);

? ? do

? ? {

? ? ? ? switch(select)

? ? ? ? {

? ? ? ? ? ? case 1:printf("\nPlease input the data=>");

? ? ? ? ? ? ? ? scanf("%d",&y);

? ? ? ? ? ? ? ? In_LQueue(lq, y);

? ? ? ? ? ? ? ? printf("\nthe elements are:\n");

? ? ? ? ? ? ? ? p=lq->front->next;

? ? ? ? ? ? ? ? while(p!=NULL)

? ? ? ? ? ? ? ? {

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

? ? ? ? ? ? ? ? ? ? p=p->next;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? printf("\n");

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? case 2:Out_LQueue(lq,&z);

? ? ? ? ? ? ? ? printf("\nthe elements are:\n");

? ? ? ? ? ? ? ? p=lq->front->next;

? ? ? ? ? ? ? ? while(p!=NULL)

? ? ? ? ? ? ? ? {

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

? ? ? ? ? ? ? ? ? ? p=p->next;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? printf("\n");

? ? ? ? ? ? ? ? break;

? ? ? ? }

? ? ? ? printf("\n(1)Input data");

? ? ? ? printf("\n(2)Output data");

? ? ? ? printf("\n(3)Exit");

? ? ? ? printf("\nPlease select one=>");

? ? ? ? scanf("%d",&select);

? ? ? ? printf("\n");

? ? }

? ? while (select!=3);

}

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 題目類型 a.C++與C差異(1-18) 1.C和C++中struct有什么區(qū)別? C沒有Protection行為...
    阿面a閱讀 7,891評論 0 10
  • #include #include<malloc.h>#include<windows.h> #define Ma...
    TDKDPIKA閱讀 267評論 0 0
  • 程序 #include #include<malloc.h>#include<windows.h> #define...
    Doloresxxxx閱讀 279評論 0 0
  • 部分公司編碼參考 Google style guide, 訪問地址在 README 中點擊相關語言規(guī)范的鏈接即可,...
    odirus閱讀 278評論 0 1
  • 昨天上午忙工作的時候,跟小同事爭執(zhí)了幾句。不是爭吵,是很生氣的提高音量說話那種。后來我一直在想這件事,為什么自己現(xiàn)...
    簡約Dr閱讀 307評論 0 0

友情鏈接更多精彩內容