【问题标题】:Not able to create stack .Getting segmentation fault.What to do?无法创建堆栈。出现分段错误。怎么办?
【发布时间】:2017-05-23 11:19:16
【问题描述】:

请帮助我不知道错误在哪里。我遇到分段错误。我使用代码块作为 IDE。我是编程新手,这是我第一次尝试创建链表。 我猜我的推送功能有问题,但我找不到。

#include<stdio.h>
#include<stdlib.h>

typedef struct list
{
    int val;
    struct list* next;
} node;

int main()
{
    node* top;
    top = NULL;
    int i;
    int n,m;
    while(1)
    {
        fflush(stdin);
        printf("Please enter i\n");
        scanf("%d", i);
        switch(i)
        {
            case 1:
            {
                printf("\nenter val");
                scanf("%d", &n);
                top=push(n, top);
            }
            case 2:
            {
                m = pop(top);
                printf("Deleted value is %d", m);
            }
            case 3:
            {
                return 0;
            }
        }
    }
}


node* push(int a,node* s)
{
    if(s==NULL)
    {
        s = malloc(sizeof(node));
        s->val = a;
        s->next = NULL;
        return s;
    }
    else
    {
        node* temp;
        temp = malloc(sizeof(node));
        temp->val = a;
        temp->next = s;
        s = temp;
        return s;
    }
}

node* pop(node* s)
{
    int x;
    node* temp;
    x = s->val;
    printf("deleted value is %d", x);
    temp = s->next;
    s->next = NULL;
    free(s);
    s = temp;
    return s;
}

【问题讨论】:

  • scanf("%d",i); --> scanf("%d", &amp;i);
  • @BLUEPIXY 永远不会变老 :)
  • 并添加一些break; 到开关。 (并去掉多余的{}
  • 对不起,如果我的编辑与 case 括号混淆,但由于它们被另一个外部编辑删除,我觉得我应该保留它们在原始代码中的样子。
  • 修复this

标签: c linked-list stack


【解决方案1】:

您在当前代码中调用了 UB 2 次:

  1. fflush(stdin);see here
  2. scanf("%d", i); 你应该用过&amp; --> scanf("%d", &amp;i), see here (还有很多其他的例子……)

这就是你得到段错误的原因。

解决这些问题后,您应该注意,编译带有警告的代码会显示m = pop(top); 行有问题,因为mintpop(top) 返回node*,所以我建议通过调整 pop 函数或 m 数据类型来解决此问题。

【讨论】:

    【解决方案2】:

    需要做一些改动,参考下面的代码并按照cmets进行

    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct list
    {
    int val;
    struct list* next;
    } node;
    
    //Add Prototypes
    node* push(int a,node* s);
    int pop(node** s);
    
    int main()
    {
    node* top;
    top = NULL;
    int i;
    int n,m;
    while(1)
    {
        fflush(stdin);
        printf("Please enter i\n");
    
        // Send the address 
        scanf("%d", &i);
    
        switch(i)
        {
            case 1:
            {
                printf("\nenter val");
                scanf("%d", &n);
                top=push(n, top);
            }
            break;// Put break statements after each case
            case 2:
            {
                m = pop(&top);// send address of top to reflect the changes for struct
                if(m==-1){//to indicate stack empty
                    printf("stack empty");
                    break;
                }
                printf("Deleted value is %d", m);
            }
            break;// Put break statements after each case
            case 3:
            {
                return 0;
            }
        }
    }
    }
    
    
    node* push(int a,node* s)
    {
    if(s==NULL)
    {
        s = malloc(sizeof(node));
        s->val = a;
        s->next = NULL;
        return s;
    }
    else
    {
        node* temp;
        temp = malloc(sizeof(node));
        temp->val = a;
        temp->next = s;
        s = temp;
        return s;
    }
    }
    
    // To effect the changes to the structure use double pointer
    int pop(node** s)
    {
    if(*s == NULL)
    {
        printf("Stack empty");
        return -1;
    }
    int x;
    node* temp;
    x = (*s)->val;
    printf("deleted value is %d", x);
    temp = (*s)->next;
    (*s)->next = NULL;
    free(*s);
    *s = temp;
    return x;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-30
      • 2018-10-10
      • 2021-08-05
      • 2013-04-16
      • 1970-01-01
      • 2011-10-30
      • 2022-01-22
      • 2022-01-13
      相关资源
      最近更新 更多