C语言栈的问题,我main函数是想压入进栈1,2,3,4,5显示出来,可是运行的时候崩溃了,求解
#include<stdio.h>#include<stdlib.h>structnode{intvalue;structnode*next;};structnode*c...
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
struct node *chuangjian(void); //创建一个空栈
void push(struct node *head, int x); //压入栈
void print_stack(struct node *head); //显示栈里的元素
int is_empty(struct node *head); //是否是空栈
int top(struct node *head); //返回栈顶
void pop(struct node *head); //出栈
void make_empty(struct node *head1); //确保栈为空栈
int main(void)
{
struct node *head;
int n, i, x[100];
head = chuangjian();
printf("输入你想压入几个元素入栈: ");
scanf_s("%d", &n);
for (i = 0;i < n;i++) {
printf("你想压入多少进栈: ");
scanf_s("%d", &x[i]);
push(head, x[i]);
}
print_stack(head);
return 0;
}
struct node *chuangjian(void)
{
struct node *head;
head = (struct node *) malloc(sizeof(struct node));
if (head == NULL) {
printf("创建空栈的时候出现错误\n");
exit(EXIT_FAILURE);
}
head->next = NULL;
make_empty(head); //确保此栈为空栈
return head;
}
void make_empty(struct node *head) //确保此栈为空栈
{
if (head == NULL) {
printf("no\n");
exit(EXIT_FAILURE);
}
else {
while (is_empty(head) != 1) {
pop(head);
}
}
}
void push(struct node *head, int x)
{
struct node *t;
t = (struct node *) malloc(sizeof(struct node));
if (t == NULL) {
printf("压栈出错\n");
exit(EXIT_FAILURE);
}
else {
t->value = x;
t->next = head;
head = t;
}
}
int is_empty(struct node *head)
{
return head == NULL; // 1为空栈,0为非空栈
}
int top(struct node *head)
{
if (is_empty(head) != 1) {
return head->value;
}
return 0;
}
void pop(struct node *head)
{
struct node *t = NULL;
if (is_empty(head) == 1) {
printf("栈的元素以弹出完毕\n");
}
else {
head = t;
t = t->next;
free(head);
}
}
void print_stack(struct node *head)
{
printf("栈内元素是: ");
while (head != NULL) {
printf("%d ", head->value);
head = head->next;
}
printf("\n");
} 展开
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
struct node *chuangjian(void); //创建一个空栈
void push(struct node *head, int x); //压入栈
void print_stack(struct node *head); //显示栈里的元素
int is_empty(struct node *head); //是否是空栈
int top(struct node *head); //返回栈顶
void pop(struct node *head); //出栈
void make_empty(struct node *head1); //确保栈为空栈
int main(void)
{
struct node *head;
int n, i, x[100];
head = chuangjian();
printf("输入你想压入几个元素入栈: ");
scanf_s("%d", &n);
for (i = 0;i < n;i++) {
printf("你想压入多少进栈: ");
scanf_s("%d", &x[i]);
push(head, x[i]);
}
print_stack(head);
return 0;
}
struct node *chuangjian(void)
{
struct node *head;
head = (struct node *) malloc(sizeof(struct node));
if (head == NULL) {
printf("创建空栈的时候出现错误\n");
exit(EXIT_FAILURE);
}
head->next = NULL;
make_empty(head); //确保此栈为空栈
return head;
}
void make_empty(struct node *head) //确保此栈为空栈
{
if (head == NULL) {
printf("no\n");
exit(EXIT_FAILURE);
}
else {
while (is_empty(head) != 1) {
pop(head);
}
}
}
void push(struct node *head, int x)
{
struct node *t;
t = (struct node *) malloc(sizeof(struct node));
if (t == NULL) {
printf("压栈出错\n");
exit(EXIT_FAILURE);
}
else {
t->value = x;
t->next = head;
head = t;
}
}
int is_empty(struct node *head)
{
return head == NULL; // 1为空栈,0为非空栈
}
int top(struct node *head)
{
if (is_empty(head) != 1) {
return head->value;
}
return 0;
}
void pop(struct node *head)
{
struct node *t = NULL;
if (is_empty(head) == 1) {
printf("栈的元素以弹出完毕\n");
}
else {
head = t;
t = t->next;
free(head);
}
}
void print_stack(struct node *head)
{
printf("栈内元素是: ");
while (head != NULL) {
printf("%d ", head->value);
head = head->next;
}
printf("\n");
} 展开
1个回答
展开全部
我觉得
is_empty
的判定应该是
head->next == NULL;
而不是head == NULL;
还有就是,push和pop要修改到head本身,所以传参要传入指向head的指针
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
struct node *chuangjian(void); //创建一个空栈
void push(struct node **head, int x); //压入栈
void print_stack(struct node *head); //显示栈里的元素
int is_empty(struct node *head); //是否是空栈
int top(struct node *head); //返回栈顶
void pop(struct node **head); //出栈
void make_empty(struct node **head1); //确保栈为空栈
int main(void)
{
struct node *head;
int n, i, x[100];
head = chuangjian();
printf("输入你想压入几个元素入栈: ");
scanf("%d", &n);
for (i = 0;i < n;i++) {
printf("你想压入多少进栈: ");
scanf("%d", &x[i]);
push(&head, x[i]);
getchar();
}
print_stack(head);
return 0;
}
struct node *chuangjian(void)
{
struct node *head;
head = (struct node *) malloc(sizeof(struct node));
if (head == NULL) {
printf("创建空栈的时候出现错误\n");
exit(EXIT_FAILURE);
}
head->next = NULL;
make_empty(&head); //确保此栈为空栈
return head;
}
void make_empty(struct node **head) //确保此栈为空栈
{
if (head == NULL) {
printf("no\n");
exit(EXIT_FAILURE);
}
else {
while (is_empty(*head) != 1) {
pop(head);
}
}
}
void push(struct node **head, int x)
{
struct node *t;
t = (struct node *) malloc(sizeof(struct node));
if (t == NULL) {
printf("压栈出错\n");
exit(EXIT_FAILURE);
}
else {
t->value = x;
t->next = *head;
*head = t;
}
}
int is_empty(struct node *head)
{
return head->next == NULL; // 1为空栈,0为非空栈
}
int top(struct node *head)
{
if (is_empty(head) != 1) {
return head->value;
}
return 0;
}
void pop(struct node **head)
{
struct node *t = NULL;
if (is_empty(*head) == 1) {
printf("栈的元素以弹出完毕\n");
}
else {
*head = t;
t = t->next;
free(*head);
}
}
void print_stack(struct node *head)
{
printf("栈内元素是: ");
while (head->next != NULL) {
printf("%d ", head->value);
head = head->next;
}
printf("\n");
}
更多追问追答
追问
书上的代码就是这个差不多,我知道怎么改就是理解不了,我不理解为什么不能是head,而必须是head->next
追答
你直接使用head就有问题了,因为你创建栈的时候,就创建了一个元素,而且head指向了这个元素,但是这个元素并没有存值。而之后的push也都各自申请了新的空间。也就是有一个栈底元素没有使用,所以,判定就是判定栈底元素,而不是head为空,如果head为空,就说明这个栈根本不存在
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询