为什么堆栈函数不接受字符串?
Why stack function not accepting a string of characters?
我正在尝试在堆栈中创建一个 push 函数,它接受一个字符串并将每个字符存储在单独的节点中,但是当我 运行 程序并输入一个字符串时它只存储第一个字母.下面是用于该函数的代码:
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 库函数的头文件
- 定义struct stack
- 变量的声明head
- 函数 display() 的原型,因为它在定义之前被调用
此外,您还有以下代码错误:
malloc of char* 而不是链表中节点的正确大小,即 struct stack
printf格式%c,单个字符,当要打印节点中存储的字符串时,即%s
最后,您使用了以下不良做法:
- gets() 已弃用且危险(编译器甚至会在警告中告诉您);使用 fgets() 代替
- 不检查 scanf() 的 return 值来处理用户输入非数字的情况
- 您使用 malloc() 分配的内存永远不会在任何地方释放
但是,通过填写缺失的部分并更正我提到的错误,以及格式化代码以提高可读性,我能够 运行 它并验证 push() 函数确实设法正确地插入到链表的头部。
#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");
}
}
我正在尝试在堆栈中创建一个 push 函数,它接受一个字符串并将每个字符存储在单独的节点中,但是当我 运行 程序并输入一个字符串时它只存储第一个字母.下面是用于该函数的代码:
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 库函数的头文件
- 定义struct stack
- 变量的声明head
- 函数 display() 的原型,因为它在定义之前被调用
此外,您还有以下代码错误:
malloc of char* 而不是链表中节点的正确大小,即 struct stack
printf格式%c,单个字符,当要打印节点中存储的字符串时,即%s
最后,您使用了以下不良做法:
- gets() 已弃用且危险(编译器甚至会在警告中告诉您);使用 fgets() 代替
- 不检查 scanf() 的 return 值来处理用户输入非数字的情况
- 您使用 malloc() 分配的内存永远不会在任何地方释放
但是,通过填写缺失的部分并更正我提到的错误,以及格式化代码以提高可读性,我能够 运行 它并验证 push() 函数确实设法正确地插入到链表的头部。
#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");
}
}