在C语言中将字符串推入并弹出到堆栈中

在C语言中将字符串推入并弹出到堆栈中,c,string,stack,C,String,Stack,我需要一种在堆栈中使用字符串的方法。我尝试了下面的代码,但似乎无法正常工作。每次我试图显示堆栈时,它都会崩溃。任何形式的帮助都将不胜感激。多谢各位 #include <conio.h> #include <stdio.h> #include <string.h> #include <stdbool.h> #define M 10 typedef struct { char stk[M]; int top; }STAK; voi

我需要一种在堆栈中使用字符串的方法。我尝试了下面的代码,但似乎无法正常工作。每次我试图显示堆栈时,它都会崩溃。任何形式的帮助都将不胜感激。多谢各位

#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define M 10

typedef struct
{
    char stk[M];
    int top;
}STAK;

void createStack(STAK *stak){
    memset(stak->stk,M,'\0'); 
    stak -> top = -1;
}

bool emptyStack(int top){
    bool empty = false;
    if (top == -1)
       empty = true;
    return empty;
}

bool fullStack(int top){
    bool full = false;
      if (top == (M - 1))
        full = true;
 return full;
}

void push(STAK *stak, char par[]){
    stak -> top++;
    stak -> stk[stak -> top] = par;

 return;
}

char pop(STAK *stak){
    char par;
    par = stak -> stk[stak -> top];
    stak -> top--;

 return par;
}

void display(STAK stak){
    int i;
    for (i = stak.top; i >= 0; i--){
        printf ("%s\n", stak.stk[i]);
    }
}

int main(){
    STAK stak;

    bool full, empty;
    createStack(&stak);
    char choice;
    char ln[6];
    do{
        printf("MENU");
        printf("\n\n[A] Park a car");
        printf("\n[B] Pick up a car");
        printf("\n[C] Display all cars");
        printf("\n[D] Exit Program");
        printf("\n\nEnter choice: ");
        choice=tolower(getche());
        system("cls");

        switch (choice){
           case 'a': 
                printf("Input your license number: ");
                gets(ln);
                full = fullStack(stak.top);
                if (full)
                    printf("Garage is full damnit");
                else 
                    push(&stak, ln);

                break;
           case 'b':
                empty = emptyStack(stak.top);
                if (empty)
                    printf("Garage empty.");
                else{
                    //some codes...
                }
                break;
           case 'c':
                empty = emptyStack(stak.top);
                if (empty)
                    printf("Garage   empty.");
                else
                    display(stak);
                break;
           case 'd':
                printf("Program will now end");
                break;
          default: printf("Invalid character. Try again");
                break;
      }

      getch();
      system("cls");

   }while (choice!='d');
}
#包括
#包括
#包括
#包括
#定义M 10
类型定义结构
{
char-stk[M];
int top;
}斯塔克;
void createStack(STAK*STAK){
memset(stak->stk,M,'\0');
stak->top=-1;
}
布尔清空堆栈(内部顶部){
bool empty=false;
如果(顶部==-1)
空=真;
返回空;
}
bool fullStack(int-top){
布尔满=假;
如果(顶部==(M-1))
完整=正确;
全额退还;
}
无效推送(STAK*STAK,char par[]){
stak->top++;
STAK>STK〔STAK->Top〕=PAR;
返回;
}
煤焦汽水(斯塔克*斯塔克){
字符;
PAR=STAK>STK〔STAK->Top〕;
斯塔克->顶--;
返回票面金额;
}
空显示(STAK STAK){
int i;
对于(i=stak.top;i>=0;i--){
printf(“%s\n”,stak.stk[i]);
}
}
int main(){
斯塔克斯塔克;
满的,空的;
createStack(&stak);
字符选择;
char-ln[6];
做{
printf(“菜单”);
printf(“\n\n[A]停车”);
printf(“\n[B]取车”);
printf(“\n[C]显示所有车辆”);
printf(“\n[D]退出程序”);
printf(“\n\n输入选项:”);
choice=tolower(getche());
系统(“cls”);
开关(选择){
案例“a”:
printf(“输入您的许可证号码:”);
获取(ln);
满=满栈(桩顶);
如果(完整)
printf(“车库简直是该死的”);
其他的
推压(和支撑,ln);
打破
案例“b”:
清空=清空堆栈(堆栈顶部);
if(空)
printf(“车库空置”);
否则{
//一些代码。。。
}
打破
案例“c”:
清空=清空堆栈(堆栈顶部);
if(空)
printf(“车库空置”);
其他的
显示器(stak);
打破
案例“d”:
printf(“程序现在将结束”);
打破
默认值:printf(“无效字符,请重试”);
打破
}
getch();
系统(“cls”);
}while(choice!=“d”);
}
很多问题:

检查
createStack
函数中
memset
的语法

top
是结构的一个元素,而不是一个变量

您需要使用strcpy在push和pop函数上复制字符串,不要使用赋值运算符

在pop函数中,若要返回字符,请尝试返回字符串数组的基址

编辑:
您的代码应该至少包含一个指针数组来存储多个字符串

堆栈中的每个元素只能包含一个字符。请进行更正,现在让我了解您的输出。如果您能理解您的问题,请接受我的回答