【无标题】

8583 顺序栈的基本操作

#include<malloc.h>
#include<stdio.h>
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100 // 存储空间初始分配量
#define STACKINCREMENT 10 // 存储空间分配增量

typedef int SElemType; // 定义栈元素类型
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等

struct SqStack
{
     SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
     SElemType *top; // 栈顶指针
     int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈

Status InitStack(SqStack &S)
{
// 构造一个空栈S,该栈预定义大小为STACK_INIT_SIZE
// 请补全代码
    S.base = (SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!S.base) return 0;
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return 1;
}

Status Push(SqStack &S,SElemType e)
{
// 在栈S中插入元素e为新的栈顶元素
// 请补全代码
    if(S.top-S.base==S.stacksize) return 0;
    *S.top++=e;
    return 1;
}

Status Pop(SqStack &S,SElemType &e)
{
// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
// 请补全代码
    if(S.top==S.base) return 0;
    e = *--S.top;
    return 1;
}

Status GetTop(SqStack S,SElemType &e)
{
// 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
// 请补全代码
    if(S.top==S.base) return 0;
    e = *(S.top-1);//易错
    return 1;
}

int StackLength(SqStack S)
{
// 返回栈S的元素个数
// 请补全代码
    return S.top-S.base;
}

Status StackTraverse(SqStack S)
{
// 从栈顶到栈底依次输出栈中的每个元素
    SElemType *p  =  S.top;        //请填空
    if(S.top==S.base)printf("The Stack is Empty!"); //请填空
    else
    {
        printf("The Stack is: ");
        while(S.top!=S.base)            //请填空
        {
            p = --S.top;                        //请填空
            printf("%d ", *p);

        }
    }
    printf("\n");
    return OK;
}

int main()
{
     int a;
     SqStack S;
SElemType x, e;
     if(InitStack(S))    // 判断顺序表是否创建成功,请填空
{
    printf("A Stack Has Created.\n");
}
while(1)
    {
    printf("1:Push \n2:Pop \n3:Get the Top \n4:Return the Length of the Stack\n5:Load the Stack\n0:Exit\nPlease choose:\n");
    scanf("%d",&a);
        switch(a)
        {
            case 1: scanf("%d", &x);
              if(!Push(S,x)) printf("Push Error!\n"); // 判断Push是否合法,请填空
              else printf("The Element %d is Successfully Pushed!\n", x);
              break;
        case 2: if(!Pop(S,e)) printf("Pop Error!\n"); // 判断Pop是否合法,请填空
              else printf("The Element %d is Successfully Poped!\n", e);
                break;
        case 3: if(!GetTop(S,e))printf("Get Top Error!\n"); // 判断Get Top是否合法,请填空
              else printf("The Top Element is %d!\n", e);
                 break;
            case 4: printf("The Length of the Stack is %d!\n",StackLength(S)); //请填空
                  break;
            case 5: StackTraverse(S);  //请填空
                  break;
            case 0: return 1;
        }
    }
}
 

8584 循环队列的基本操作

#include<malloc.h>
#include<stdio.h>
#define OK 1
#define ERROR 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
typedef int QElemType;
#define MAXQSIZE 100 // 最大队列长度(对于循环队列,最大队列长度要减1)

typedef struct
{
   QElemType *base; // 初始化的动态分配存储空间
   int front; // 头指针,若队列不空,指向队列头元素
   int rear; // 尾指针,若队列不空,指向队列尾元素的下一个位置
 }SqQueue;

Status InitQueue(SqQueue &Q)
{
// 构造一个空队列Q,该队列预定义大小为MAXQSIZE
// 请补全代码
    Q.base = (QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
    if(!Q.base) return 0;
    Q.front=Q.rear=0;
    return 1;
}

Status EnQueue(SqQueue &Q,QElemType e)
{
// 插入元素e为Q的新的队尾元素
// 请补全代码
    if((Q.rear+1)%MAXQSIZE==Q.front) return 0;
    Q.base[Q.rear] = e;
    Q.rear = (Q.rear+1)%MAXQSIZE;
    return 1;
}

Status DeQueue(SqQueue &Q, QElemType &e)
{
// 若队列不空, 则删除Q的队头元素, 用e返回其值, 并返回OK; 否则返回ERROR
// 请补全代码
    if(Q.front==Q.rear) return 0;
    e = Q.base[Q.front];
    Q.front = (Q.front+1)%MAXQSIZE;
    return 1;
}

Status GetHead(SqQueue Q, QElemType &e)
{
// 若队列不空,则用e返回队头元素,并返回OK,否则返回ERROR
// 请补全代码
    if(Q.front==Q.rear) return 0;
    e = Q.base[Q.front];
    return 1;
}

int QueueLength(SqQueue Q)
{
// 返回Q的元素个数
// 请补全代码
    return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}

Status QueueTraverse(SqQueue Q)
{
// 若队列不空,则从队头到队尾依次输出各个队列元素,并返回OK;否则返回ERROR.
    int i;
    i=Q.front;
    if(Q.front==Q.rear)printf("The Queue is Empty!");  //请填空
    else{
        printf("The Queue is: ");
        while(i%MAXQSIZE!=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 Queue\n5:Load the Queue\n0:Exit\nPlease 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;
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值