/***************利用數(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);
}