澳门新葡亰信誉平台游戏顺序循环队列的基本操作

by admin on 2020年1月22日

#include <stdio.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
#define MAXQSIZE 6
typedef struct{
 QElemType *base;
 int front;
 int rear;
}SqQueue;

杨辉三角等腰三角形求解
#include
#include
#define OK 1
#define ERROR -1
#define OVERFLOW -2
#define MAXQSIZE 100

#include

Status InitQueue(SqQueue &Q)
{
 Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
 if(!Q.base)
  return (OVERFLOW);
 Q.front=Q.rear=0;
 return OK;
}

typedef int Status;
typedef struct{
int *base;
int front;
int rear;
}SqQueue;

#include

int QueueLength(SqQueue Q)
{
 return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}

Status InitQueue(SqQueue &Q)
{
Q.base=malloc(MAXQSIZE*sizeof;
if exit;
Q.front=Q.rear=0;
return OK;
}

#define OK 1

Status EnQueue(SqQueue &Q,QElemType e)
{
   if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;
 Q.base[Q.rear]=e;
 Q.rear=(Q.rear+1)%MAXQSIZE;
 return OK;
}
Status DeQueue(SqQueue &Q,QElemType &e){
 if(Q.front==Q.rear) return ERROR;      
 e=Q.base[Q.front];
 Q.front=(Q.front+1)%MAXQSIZE;
 return OK;
}
Status QueueEmpty(SqQueue Q){
 if(Q.front==Q.rear) return true;
 else return false;
}

Status EnQueue(SqQueue &Q,int e)
{
if(%MAXQSIZE==Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear=%MAXQSIZE;
return OK;
}

#define ERROR 0

void main(){
 SqQueue Q;
 QElemType e;
 InitQueue(Q);
 printf(“输入一个入队列的数(以-1表示结束):”);
 scanf(“%d”,&e);
 
     while(e!=-1){
  EnQueue(Q,e);
  printf(“n输入下一个入队列的数(以-1表示结束):”);
  scanf(“%d”,&e);
 }
 if(QueueEmpty(Q)) printf(“队列为空。n”);
 else{
  printf(“n队列不为空,其元素个数为:%dn”, QueueLength(Q));
  printf(“n元素出队列的顺序为:n”);
  while(Q.rear!=Q.front){
   DeQueue(Q,e);
   printf(“%d “,e);
  }
 }
 return;
}

Status DeQueue(SqQueue &Q,int &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return OK;
}

typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等

Status GetHead(SqQueue Q,int &e)
{
e=Q.base[Q.front];
return OK;
}

typedef int QElemType;

void Print
{
SqQueue Q;
int i,s,e;
InitQueue;
EnQueue;
EnQueue;
EnQueue;
for(i=1;i<=n;i++)
{
do
{
DeQueue;
GetHead;
if
printf ;
EnQueue;
}while;
printf;
EnQueue;
}
}

#define MAXQSIZE 100 // 最大队列长度(对于循环队列,最大队列长度要减1)

int main()
{
int n;
scanf(“%d”,&n);
Print;
return 0;
}

 

typedef struct

{

    QElemType *base; // 初始化的动态分配存储空间

    int front; // 头指针,若队列不空,指向队列头元素

    int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置

} SqQueue;

 

//创建队列

Status InitQueue(SqQueue &Q)

{

// 构造一个空队列Q,该队列预定义大小为MAXQSIZE

    Q.base=(QElemType *)malloc(sizeof(SqQueue)*MAXQSIZE);

    if(Q.base==NULL)return ERROR;

    Q.front=Q.rear=0;

    return OK;

}

 

//入队

Status EnQueue(SqQueue &Q,QElemType e)

{

// 插入元素e为Q的新的队尾元素

    if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;    //队满则不插入

    Q.base[Q.rear]=e;

    Q.rear=(Q.rear+1)%MAXQSIZE;

    return OK;

 

}

 

//出队

Status DeQueue(SqQueue &Q, QElemType &e)

{

// 若队列不空, 则删除Q的队头元素, 用e返回其值, 并返回OK; 否则返回ERROR

    if(Q.front==Q.rear)return ERROR;

    e=Q.base[Q.front];

    Q.front=(Q.front+1)%MAXQSIZE;

    return OK;

}

 

//取对头元素

Status GetHead(SqQueue Q, QElemType &e)

{

// 若队列不空,则用e返回队头元素,并返回OK,否则返回ERROR

    if(Q.front==Q.rear)return OK;

    e=Q.base[Q.front];

    return OK;

}

 

//计算队列的元素个数

int QueueLength(SqQueue Q)

{

    return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;

 

}

 

//判断队列是否为空

Status QueueTraverse(SqQueue Q)

{

//
若队列不空,则从队头到队尾依次输出各个队列元素,并返回OK;否则返回ERROR.

    int i;

    i=Q.front;

    if(i==Q.rear)printf(“The Queue is Empty!”);  //请填空

    else

    {

        printf(“The Queue is: “);

        while(i!=Q.rear)     //请填空

        {

            printf(“%d “,Q.base[i]);   //请填空

            i = (i+1)%MAXQSIZE;   //请填空

        }

    }

    printf(“n”);

    return OK;

}

 

//主函数

int main()

{

    int a;

    SqQueue S;

    QElemType x, e;

    if(InitQueue(S))    // 判断顺序表是否创建成功,请填空

    {

        printf(“A Queue Has Created.n”);

    }

    while(1)

    {

        printf(“1:Enter n2:Delete n3:Get the Front n4:Return the
Length of the Queuen5:Load the Queuen0:ExitnPlease choose:n”);

        scanf(“%d”,&a);

        switch(a)

        {

        case 1:

            scanf(“%d”, &x);

            if(!EnQueue(S,x)) printf(“Enter Error!n”); //
判断入队是否合法,请填空

            else printf(“The Element %d is Successfully Entered!n”,
x);

            break;

        case 2:

            if(!DeQueue(S,e)) printf(“Delete Error!n”); //
判断出队是否合法,请填空

            else printf(“The Element %d is Successfully Deleted!n”,
e);

            break;

        case 3:

            if(!GetHead(S,e))printf(“Get Head Error!n”); // 判断Get
Head是否合法,请填空

            else printf(“The Head of the Queue is %d!n”, e);

            break;

        case 4:

            printf(“The Length of the Queue is %d!n”,QueueLength(S));
 //请填空

            break;

        case 5:

            QueueTraverse(S); //请填空

            break;

        case 0:

            return 1;

        }

    }

}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图