关于有关栈的问题,但是可能不是栈本身有问题

#请问为什么功能1初始化之后就不显示我的菜单了捏,不继续了呢

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define Stack_Size  100
#define FALSE   0
#define TRUE    1
 
typedef int StackElementType; //数据元素类型定义

typedef struct{  //顺序栈的定义
    StackElementType  elem[Stack_Size];
    int top;
}SeqStack;

void  InitStack(SeqStack *S);//初始化栈 
int IsEmpty(SeqStack *S);//判空 
int IsFull(SeqStack *S);//判满 
int Push(SeqStack *S, StackElementType a); //进栈 
int Pop(SeqStack *S, StackElementType *x); //出栈 
int GetTop(SeqStack *S, StackElementType *x); //去栈顶元素 
void PrintStack(SeqStack*S); //打印栈元素(从栈顶到栈底顺序) 


int main()
{
    SeqStack *S;  //创建一个线性表 
    int a;
    int *x; 
    //生成菜单 
    char sel=' ';
    while(sel!='0')
    {
        
        printf("------栈(顺序存储结构)演示系统-------\n");
        printf("   版本:1.0   作者:xinxinping 日期:yyyy-mm-dd\n"); 
        printf("------------------------------------------\n");
        printf("       1.初始化栈\n");
        printf("       2.进栈操作\n");
        printf("       3.出栈操作\n");
        printf("       4.打印栈顶元素\n");
        printf("       5.打印栈\n");
        printf("       6.清空屏幕\n");
        printf("       0.退出系统\n");
        printf("请输入选项[0-7]:"); 
        sel=getch();
        switch(sel)
        {
            case '1':
                printf("初始化栈.\n");
                InitStack(S);
                PrintStack(S);
                system("pause"); //按任意键继续 
                break;
            case '2':
                int b,i;
                printf("进栈操作.\n");
                printf("请输入进栈个数"); 
                scanf("%d",&b);  
                printf("请输入进栈数据"); 
                for(i=0;i<b;i++)
                {
                    scanf("%d",&a);
                }
            
                Push(S, a); 
                system("pause"); //按任意键继续 
                break;
            case '3':
                printf("出栈操作.\n");
                Pop(S, x); 
                system("pause"); //按任意键继续 
                break;
            case '4':
                printf("打印栈顶元素操作.\n");
                GetTop(S, x);
                system("pause"); //按任意键继续 
                break;
            case '5':
                printf("打印栈操作.\n");
                PrintStack(S);
                system("pause"); //按任意键继续 
                break;
            case '6':
                system("cls");
                break;
            case '0':
                printf("\n谢谢使用,再见!\n");
                break;
            default:
                printf("您输入的选项不合法,请重新选择!\n");
        }
    }

    return 0;
}
void  InitStack(SeqStack *S)
{


       S->top = -1;
}


/*if(S->top == -1)
    {
        printf("此栈为空!"); 
        return (FALSE); 
    }


    if(S->top ==Stack_Size-1)
    {
        printf("此栈已满!"); 
        return (TRUE);
    }*/

int Push(SeqStack *S, StackElementType a)
{
    if(S->top == Stack_Size-1)
    return (FALSE);
    S->top++;
    S->elem[S->top] = a;
    return (TRUE);
}

int Pop(SeqStack *S,StackElementType *x)
{ 
    if(S->top == -1)
    return (FALSE);
    else
    {
        *x = S->elem[S->top];
        S->top--;
        return(TRUE);
    }
}

int GetTop(SeqStack *S, StackElementType *x)
{
    if(S->top == -1)
    return (FALSE);
    else
    {
        *x = S->elem[S->top];
        return *x;
    }
}

void PrintStack(SeqStack*S)
{
    if(S->top != -1)
    {
        while(S->top>=0)
        printf("%d",S->elem[S->top]) ;
        S->top--;
        
    }
}

img

整体修改如下,改动处见注释,供参考:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define Stack_Size  100
#define FALSE   0
#define TRUE    1

typedef int StackElementType; //数据元素类型定义

typedef struct {  //顺序栈的定义
    StackElementType  elem[Stack_Size];
    int top;
}SeqStack;

void InitStack(SeqStack* S);//初始化栈 
int  IsEmpty(SeqStack* S);//判空 
int  IsFull(SeqStack* S);//判满 
int  Push(SeqStack* S, StackElementType a); //进栈 
int  Pop(SeqStack* S, StackElementType* x); //出栈 
int  GetTop(SeqStack* S, StackElementType* x); //去栈顶元素 
void PrintStack(SeqStack* S); //打印栈元素(从栈顶到栈底顺序) 


int main()
{
    SeqStack* S = (SeqStack*)malloc(sizeof(SeqStack));  // 修改
    int a;
    int x;     //int* x;
    //生成菜单 
    char sel = ' ';
    while (sel != '0')
    {

        printf("------栈(顺序存储结构)演示系统-------\n");
        printf("   版本:1.0   作者:xinxinping 日期:yyyy-mm-dd\n");
        printf("------------------------------------------\n");
        printf("       1.初始化栈\n");
        printf("       2.进栈操作\n");
        printf("       3.出栈操作\n");
        printf("       4.打印栈顶元素\n");
        printf("       5.打印栈\n");
        printf("       6.清空屏幕\n");
        printf("       0.退出系统\n");
        printf("请输入选项[0-7]:");
        sel = getch();
        switch (sel)
        {
        case '1':
            printf("初始化栈.\n");
            InitStack(S);
            PrintStack(S);
            system("pause"); //按任意键继续 
            break;
        case '2':
            int b, i;
            printf("进栈操作.\n");
            printf("请输入进栈个数");
            scanf("%d", &b);
            printf("请输入进栈数据");
            for (i = 0; i < b; i++)
            {
                scanf("%d", &a);
                Push(S, a); // 修改
            } // 修改
            system("pause"); //按任意键继续 
            break;
        case '3':
            printf("出栈操作.\n");
            if (Pop(S, &x))   // 修改
                printf("Pop: %d\n", x); // 修改
            else
                printf("栈空!\n");     // 修改
            system("pause"); //按任意键继续 
            break;
        case '4':
            printf("打印栈顶元素操作.\n");
            if (GetTop(S, &x)) // 修改
                printf("GetTop:%d", x);// 修改
            else
                printf("栈空!\n");// 修改
            system("pause"); //按任意键继续 
            break;
        case '5':
            printf("打印栈操作.\n");
            PrintStack(S);
            system("pause"); //按任意键继续 
            break;
        case '6':
            system("cls");
            break;
        case '0':
            printf("\n谢谢使用,再见!\n");
            break;
        default:
            printf("您输入的选项不合法,请重新选择!\n");
        }
    }

    return 0;
}
void  InitStack(SeqStack* S)
{
    S->top = -1;
}


/*if(S->top == -1)
    {
        printf("此栈为空!");
        return (FALSE);
    }
    if(S->top ==Stack_Size-1)
    {
        printf("此栈已满!");
        return (TRUE);
    }*/

int Push(SeqStack* S, StackElementType a)
{
    if (S->top == Stack_Size - 1)
        return (FALSE);
    S->top++;
    S->elem[S->top] = a;
    return (TRUE);
}

int Pop(SeqStack* S, StackElementType* x)
{
    if (S->top == -1)
        return (FALSE);
    else
    {
        *x = S->elem[S->top];
        S->top--;
        return(TRUE);
    }
}

int GetTop(SeqStack* S, StackElementType* x)
{
    if (S->top == -1)
        return (FALSE);
    else
    {
        *x = S->elem[S->top];
        return *x;
    }
}

void PrintStack(SeqStack* S)
{
    int top = S->top;     // 修改
    if (S->top != -1)     // 修改
    {
        while (top >= 0)  // 修改
        {                 // 修改
            printf("%d ", S->elem[top]);// 修改
            top--;        // 修改
        }                 // 修改
    }
}

代码中存在一些问题,包括未分配内存给 SeqStack 结构体指针 S,以及未初始化 x 指针。此外,PrintStack 函数中的 while 循环缺少一个换行符。

我就改了一下这里,其他应该没问题,在VS上可以

SeqStack *S = new SeqStack;  //创建一个线性表 
    int a;
    int *x = new int;

img