C 为什么堆栈函数不接受字符串?
我试图在堆栈中创建一个push函数,它接受一个字符串并将每个字符存储在单独的节点中,但是当我运行程序并输入一个字符串时,它只存储第一个字母。以下是用于为该函数指定的代码:C 为什么堆栈函数不接受字符串?,c,string,stack,singly-linked-list,C,String,Stack,Singly Linked List,我试图在堆栈中创建一个push函数,它接受一个字符串并将每个字符存储在单独的节点中,但是当我运行程序并输入一个字符串时,它只存储第一个字母。以下是用于为该函数指定的代码: void push(char *word){ struct stack *newNode = malloc(sizeof(char *)); if(newNode == NULL){ printf("unable to push to stack"); }else{ strcpy(newNo
void push(char *word){
struct stack *newNode = malloc(sizeof(char *));
if(newNode == NULL){
printf("unable to push to stack");
}else{
strcpy(newNode -> word,word);
newNode -> next = head;
head = newNode;
}
printf("Inserted in stack\n");
}
int main(){
int choice;
char str[100];
printf("Enter a string: ");
gets(str);
while(1)
{
printf("******** Menu ********\n");
printf(" 1. Push\n 2. Display\n 3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
push(str);
break;
case 2:
display();
break;
case 3:
printf("\nProgram Exited\n");
exit(0);
break;
default:
printf("Incorrect selection!\n");
}
void display(){
struct stack* newNode;
if(head == NULL){
printf("\nStack is Empty!!!\n");
}else{
newNode = head;
while(newNode != NULL){
printf("%c--->", newNode -> word);
newNode = newNode -> next;
}
}
}
}
这里有很多问题,首先是它不是完整的代码集。您缺少:
- 包含您正在使用的C库函数的头文件
- 结构堆栈的定义
- 变量头的声明
- 函数display()的原型,在定义之前调用它
- malloc的字符*而不是链表中节点的正确大小,即结构堆栈
- 当您要打印存储在节点中的字符串(即%s)时,printf格式为%c,单个字符
- gets()已被弃用且危险(编译器甚至会在其警告中告诉您这一点);改为使用fgets()
- 不检查scanf()的返回值以处理用户输入数字以外的内容的情况
- 使用malloc()分配的内存在任何地方都不会被释放
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING_MAX 100
struct stack
{
char word[STRING_MAX];
struct stack *next;
};
struct stack *head = NULL;
void display();
void push(char *word)
{
struct stack *newNode = malloc(sizeof(struct stack));
if(newNode == NULL)
{
printf("unable to push to stack\n");
}
else
{
strcpy(newNode -> word,word);
newNode -> next = head;
head = newNode;
}
printf("Inserted in stack\n");
}
int main()
{
int choice;
char str[STRING_MAX];
printf("Enter a string: ");
gets(str);
while(1)
{
printf("******** Menu ********\n");
printf(" 1. Push\n 2. Display\n 3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
push(str);
break;
case 2:
display();
break;
case 3:
printf("\nProgram Exited\n");
exit(0);
break;
default:
printf("Incorrect selection!\n");
break;
}
}
display();
return 0;
}
void display()
{
struct stack* newNode;
if(head == NULL)
{
printf("\nStack is Empty!!!\n");
}
else
{
newNode = head;
while(newNode != NULL)
{
printf("%s--->", newNode -> word);
newNode = newNode -> next;
}
printf("\n");
}
}
#包括
#包括
#包括
#定义字符串_MAX 100
结构堆栈
{
字符字[STRING_MAX];
结构堆栈*下一步;
};
结构堆栈*head=NULL;
void display();
无效推送(字符*字)
{
结构堆栈*newNode=malloc(sizeof(结构堆栈));
if(newNode==NULL)
{
printf(“无法推送到堆栈\n”);
}
其他的
{
strcpy(newNode->word,word);
新建节点->下一步=头部;
头=新节点;
}
printf(“插入堆栈\n”);
}
int main()
{
智力选择;
字符str[STRING_MAX];
printf(“输入字符串:”);
获取(str);
而(1)
{
printf(“**********菜单*********\n”);
printf(“1.推送\n 2.显示\n 3.退出\n”);
printf(“输入您的选择:”);
scanf(“%d”,选择(&C);
开关(选择)
{
案例1:
推(str);
打破
案例2:
显示();
打破
案例3:
printf(“\n程序已退出\n”);
出口(0);
打破
违约:
printf(“选择错误!\n”);
打破
}
}
显示();
返回0;
}
无效显示()
{
结构堆栈*newNode;
if(head==NULL)
{
printf(“\n堆栈为空!!!\n”);
}
其他的
{
新节点=头部;
while(newNode!=NULL)
{
printf(“%s-->”,newNode->word);
newNode=newNode->next;
}
printf(“\n”);
}
}
为什么只为
sizeof(char*)
分配足够的空间?它应该是malloc(sizeof(struct stack))
strcpy
要求分配目标缓冲区并使其足够大以适合字符串<代码>新节点->word甚至尚未分配。它只是打印一个字符,因为printf中的“%c”意味着打印一个甚至不应该打印正确字符的字符。抱歉,这需要太多的更改。谢谢,伙计,函数现在可以正常工作了。感谢您的回应和帮助!我想将pop函数添加到这段代码中,以生成一个完整的堆栈程序。我已经实现了这个函数,但是它一次弹出整个字符串,而不是一个字符。根据上面的代码对pop函数有什么建议吗?对于典型的堆栈实现,pop的结果正是push插入的结果。因为push函数添加了一个包含字符串的节点,所以pop将返回相同的字符串,而不是一次返回一个字符,这是非常合理的。换句话说,你不会一个字符一个字符地推送,那么你为什么希望一个字符一个字符地推送呢?这是一个非常奇怪的堆栈实现。