You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于链表实现栈的Turbo C++代码报错求助:第91行函数应返回值

解决Turbo C++中“Function should return a value”错误

我太懂这种被老旧编译器卡脖子的感觉了——Turbo C++对代码的严格性和现代编译器完全不是一个路数,尤其是在返回值的检查上特别死板。咱们直接来搞定这个问题:

错误根源分析

你遇到的第91行错误,其实是两个编译器的“死板规则”导致的:

  1. int pop()函数的路径检查问题:
    虽然你在栈为空的分支里调用了exit(1)直接终止程序,但Turbo C++的静态代码分析根本不管这个——它只会看到if分支里没有写return语句,直接抛出“函数应返回值”的错误。
  2. void main()的兼容性隐患:
    虽然Turbo C++名义上允许void main(),但部分版本的严格模式下,会因为main函数没有返回值触发警告甚至错误。

修复后的完整代码

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>  // 补上malloc、exit需要的头文件

struct stack {
    int element;
    struct stack *next;
} * top;

void push(int);
int pop();
void display();

// 改成标准的int main(),适配Turbo C++的严格检查
int 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(EXIT_SUCCESS);  // 用标准宏代替魔法数字1
                break;
            default: 
                printf("\nInvalid choice !\n");
                getch();  // 加个暂停,让用户看到错误提示
                break;
        }
    }
    // 虽然while是死循环,但加return避免编译器找茬
    return 0;
}

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("\nStack is Empty.");  // 修正原代码里多打了一个反斜杠的问题
        getch();
        exit(EXIT_FAILURE);
        // 给编译器加个“安慰剂”return——永远不会执行,但能骗过静态检查
        return -1;
    } else {
        int temp = top->element;
        top = top->next;
        return temp;
    }
}

void display() {
    struct stack *ptr1 = top;
    printf("\nThe various stack elements are:\n");
    if (ptr1 == NULL) {  // 增加空栈判断,提升用户体验
        printf("\tStack is Empty!\n");
        return;
    }
    while (ptr1 != NULL) {
        printf("%d\t", ptr1->element);
        ptr1 = ptr1->next;
    }
    return;
}

关键修复点说明

  • 给pop()的空栈分支加冗余return:
    这完全是给Turbo C++的死板检查“喂安慰剂”——exit(EXIT_FAILURE)已经会直接终止程序,这个return永远不会被执行,但编译器看不到这一点,加了之后就能消除错误。
  • 把void main()改成int main():
    这是C标准规定的写法,Turbo C++对这个写法的兼容性更好,最后加的return 0;也是为了彻底堵住编译器的嘴。
  • 补上stdlib.h头文件:
    你的代码里用了malloc和exit,这两个函数的声明都在stdlib.h里,虽然Turbo C++可能会隐式允许,但显式包含能避免潜在的奇怪问题。
  • 小细节优化:
    修正了原代码里的拼写错误(多打了一个反斜杠),给display()加了空栈判断,给default分支加了暂停,让程序更友好。

这样修改后,代码在Turbo C++里肯定能顺利编译运行,完美满足你的提交要求!

内容的提问来源于stack exchange,提问作者Ansh Tyagi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:27:46