【发布时间】:2023-03-23 08:54:01
【问题描述】:
我在 Turbo C++ 代码的第 91 行收到“函数应该返回一个值”错误,请帮助我,因为我必须提交我的项目,我知道 Turbo C++ 是一个非常古老的编译器,但就是这样我们的大学老师建议,所以我无能为力
'''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''' ''''''''
#include <stdio.h>
#include <conio.h>
struct stack
{
int element;
struct stack *next;
} * top;
void push(int);
int pop();
void display();
void main()
{
int num1, num2, choice;
while (1)
{
clrscr();
printf("Select a choice from the following:");
printf("\n[1] Push an element into the stack");
printf("\n[2] Pop out an element from the stack");
printf("\n[3] Display the stack elements");
printf("\n[4] Exit\n");
printf("\n\tYour choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
printf("\n\tEnter the element to be pushed into the stack: ");
scanf("%d", &num1);
push(num1);
break;
}
case 2:
{
num2 = pop();
printf("\n\t%d element popped out of the stack\n\t", num2);
getch();
break;
}
case 3:
{
display();
getch();
break;
}
case 4:
exit(1);
break;
default:
printf("\nInvalid choice !\n");
break;
}
}
}
void push(int value)
{
struct stack *ptr;
ptr = (struct stack *)malloc(sizeof(struct stack));
ptr->element = value;
ptr->next = top;
top = ptr;
return;
}
int pop()
{
if (top == NULL)
{
printf("\n\STACK is Empty.");
getch();
exit(1);
}
else
{
int temp = top->element;
top = top->next;
return (temp);
}
}
void display()
{
struct stack *ptr1 = NULL;
ptr1 = top;
printf("\nThe various stack elements are:\n");
while (ptr1 != NULL)
{
printf("%d\t", ptr1->element);
ptr1 = ptr1->next;
}
}
'''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''' ''''''''
【问题讨论】:
-
第 91 行,我看到
printf("\n\STACK is Empty.");如果是这种情况,请在\n之后删除 \,它应该是printf("\n STACK is Empty.");,你还包括#include <stdlib.h>吗? -
'void push(int value) { struct stack *ptr; ptr = (struct stack *)malloc(sizeof(struct stack)); ptr->元素=值; ptr->下一个=顶部;顶部 = ptr;返回; }'程序在返回后的括号中显示错误,不,我没有包含'#include
' -
第 91 行是函数
pop()的结尾,它确实在所有情况下都会返回一个值,因此这是一个错误警告。尝试删除else,因为在之前的exit(1);之后没有else替代项。第一条注释指的是第81 行的代码。 -
与错误无关,但在
push函数中不需要使用return; -
感谢天气风向标它工作了,但我想知道删除“else”后括号中的代码{ int temp = top->element;顶部=顶部->下一个;返回(温度);将工作与否?还是没用所以我应该删除它
标签: c linked-list implementation