天地四象 2022-10-29 23:44 采纳率: 66.7%
浏览 102
已结题

C语言字符串的入栈出栈有top与base

结构体里定义top与base的指针,命令行输入字符串,top指针里内容是字符串,top++是不改变字符串的


typedef struct sta{
    ElemType *top;
    ElemType *bottom;
    int stack_size;
}SqStack;//顺序栈
int InitStack(SqStack *S)
{
    S->bottom = (ElemType*)malloc(STACK_SIZE*sizeof(ElemType));
    if (!S->bottom)
        return ERROR;
    S->top = S->bottom;
    S->stack_size = STACK_SIZE;
    return OK;
}//顺序栈的初始化
void PushSqStack(SqStack *S, ElemType *a)//顺序栈入栈
{
    if ((S->top - S->bottom) >= S->stack_size - 1)//判断栈是否满
    {
        S->bottom = (ElemType*)realloc(S->bottom, sizeof(ElemType));//realloc的用法
        if (!S->bottom)
            return;
        S->top = S->bottom + S->stack_size;
        S->stack_size += STACKINCREMENT;
    }
    S->top= a;
    S->top++;
}
 int popSqStack(SqStack *S,int*e)//顺序栈出栈并返回栈顶值
{
    if (S->top == S->bottom)
        return ERROR;
    else
    {
        --(S->top);
        e = S->top;
    }
    return OK;
}


switch (temp)
        {
        case'1':
            printf("顺序栈创建成功!\n");
            int n, i;
            printf("请输入你想要输入的个数:\n");
            scanf("%d", &n);
            printf("请输入:\n");
            for (i = 0; i < n; i++)
            {
                ElemType *a;
a=(ElemType*)malloc(sizeof(ElemType));
                scanf("%s",a);
                PushSqStack(&S, a);
            }
            printf("入栈成功!\n");
            break;
        case'2':
            printf("出栈依次为:\n");
            while (S.bottom != S.top)
            {
                int *e;
                e = (ElemType*)malloc(sizeof(ElemType));
                popSqStack(&S,e);
                printf("%s\n",e);
            }
            printf("出栈成功!\n");
            break;
// ConsoleApplication12.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <malloc.h>
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#define _CRT_SECURE_NO_WARNINGS1
#define ERROR -1
#define OK 0
#define STACK_SIZE 100
#define STACKINCREMENT 10
#define QUEUE_SIZE 100
#define SIZE 30
using namespace std;
typedef char ElemType;
typedef struct sta{
    ElemType *top;
    ElemType *bottom;
    int stack_size;
}SqStack;//顺序栈
int InitStack(SqStack *S)
{
    S->bottom = (ElemType*)malloc(STACK_SIZE*sizeof(ElemType));
    if (!S->bottom)
        return ERROR;
    S->top = S->bottom;
    S->stack_size = STACK_SIZE;
    return OK;
}//顺序栈的初始化
void PushSqStack(SqStack *S, ElemType *a)//顺序栈入栈
{
    if ((S->top - S->bottom) >= S->stack_size - 1)//判断栈是否满
    {
        S->bottom = (ElemType*)realloc(S->bottom, sizeof(ElemType));//realloc的用法
        if (!S->bottom)
            return;
        S->top = S->bottom + S->stack_size;
        S->stack_size += STACKINCREMENT;
    }
    S->top= a;
    S->top++;
}
 int popSqStack(SqStack *S,int*e)//顺序栈出栈并返回栈顶值
{
    if (S->top == S->bottom)
        return ERROR;
    else
    {
        --(S->top);
        e = S->top;
    }
    return OK;
}
int _tmain(int argc, _TCHAR* argv[])

{
    SqStack S;
    InitStack(&S);
    char temp;
    int i;
    i = 0;
    temp = '0';
    while (1)
    {
        if (temp == '0' || temp == '1' || temp == '2' ){
            printf(" press 1 to 创建一个顺序栈并入栈\n");
            printf(" press 2 to 从顺序栈出栈\n");}
        temp = getchar();

        switch (temp)
        {
            printf("顺序栈创建成功!\n");
            int n, i;
            printf("请输入你想要输入的个数:\n");
            scanf("%d", &n);
            printf("请输入:\n");
            for (i = 0; i < n; i++)
            {
                ElemType *a;
a=(ElemType*)malloc(sizeof(ElemType));
                scanf("%s",a);
                PushSqStack(&S, a);
            }
            printf("入栈成功!\n");
            break;
        case'2':
            printf("出栈依次为:\n");
            while (S.bottom != S.top)
            {
                int *e;
                e = (ElemType*)malloc(sizeof(ElemType));
                popSqStack(&S,e);
                printf("%s\n",e);
            }
            printf("出栈成功!\n");
            break;
}
}
return OK;}

  • 写回答

4条回答 默认 最新

  • 快乐鹦鹉 2022-10-30 08:42
    关注
    获得3.60元问题酬金

    兄弟,你到底要问啥啊???

    评论

报告相同问题?

问题事件

  • 系统已结题 11月7日
  • 修改了问题 10月30日
  • 修改了问题 10月30日
  • 赞助了问题酬金15元 10月30日
  • 展开全部

悬赏问题

  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡