【问题标题】:Implementation of stack using linked list使用链表实现栈
【发布时间】: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 &lt;stdlib.h&gt; 吗?
  • '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


【解决方案1】:

编译器正在抱怨,因为您在 if 语句之外没有 return 语句。即使你在if 分支中调用exit,从语法上讲这只是另一个函数调用; 在结构上,编译器会看到一条路径,在该路径中,您可以在没有 return 语句的情况下到达函数体的结束 }

您要确保returnif-else 语句的主体之外是可访问的,最好的方法是将else 分支完全从语句中取出:

int pop( void )
{
  int temp;

  if ( !top )
  { 
    fputs( "Stack is empty", stderr );
    exit( 1 );
  }

  temp = top->element;
  top = top->next;
  return temp;
}

【讨论】:

  • 在 int temp = top->element;它显示“此处不允许声明”
  • 在函数开头声明int temp;,在if之后只使用temp = top-&gt;element;
  • @AnshTyagi:没错,您使用的是不支持混合声明和代码的古老实现。您需要在函数开头声明int temp;,并将赋值更改为temp = top-&gt;element;。对此感到抱歉。
  • john bode,请发送带有必要更改的代码,我不知道我应该在哪里声明 int temp;
【解决方案2】:

您可以如下更改您的pop 函数(假设您没有将 -1 作为元素存储在堆栈中)

int pop()
{
    if (top == NULL)
    {
        printf("\n\STACK is Empty.");
        getch();
        return -1;// or other invalid value which indicates stack empty
    }
    else
    {
        int temp = top->element;
        top = top->next;
        return (temp);
    }
}

在你调用的地方修改如下

case 2:
{
    num2 = pop();
    if(num2 != -1) {
        printf("\n\t%d element popped out of the stack\n\t", num2);
        getch();
    }else{
        printf("Stack is Empty\n");
        exit(1);
    }
    break;
}

【讨论】:

  • 我不想过多修改代码,请仅在同一个问题中进行一些修改
  • 这不是太多代码,您应该正确处理错误场景并且您还没有处理代码中的内存泄漏。
猜你喜欢
  • 2011-07-29
  • 1970-01-01
  • 1970-01-01
  • 2012-05-24
  • 2019-03-04
  • 2020-01-16
  • 2021-10-30
相关资源
最近更新 更多