【问题标题】:Linked List Operations In C (Segmentation Error Core Dumped!)C中的链表操作(分段错误核心转储!)
【发布时间】:2019-10-16 18:07:07
【问题描述】:

过去几天我一直在尝试在 c 中实现链表操作,但我一次又一次地遇到同样的错误,上面写着“Segmentation Error Core Dumped”。我无法弄清楚逻辑的哪一部分出了问题。奇怪的是,在解决了所有警告和错误之后,代码却没有执行。如果有人指出我是否正确使用了链表和指针,那也很棒。我的代码似乎很大,但我无法缩短它。再一次,我将感谢所有可以帮助我并使这段代码更具可读性并减少歧义的人。

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

struct nodes 
{
    int data;
    struct nodes* next; 
};
typedef struct nodes* node;
node head=NULL;
void InsertFront()
{
    node temp=NULL;
    printf("Enter The Value Of Node\n");
    scanf("%d",&temp->data);
    if(head==NULL)
    {
        head->data=temp->data;     
    }
    else
    {
        temp->next=head;
        head=temp;
    }
}

void InsertBack()
{
    node temp,newnode;
    printf("Enter The Value Of Node\n");`enter code here`
        scanf("%d",&newnode->data);
    if(head==NULL)
    {
        head->data=newnode->data;
    }
    else
    {
        temp=head;
        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        newnode=temp->next;
    }    
}

void InsertPosition()
{
    node previousnode,newnode,nextnode,temp;
    int position,count=0;
    printf("Enter the Position At Which New Node Has To Be Inserted");
    scanf("%d",&position);
    printf("Enter The Value Of Node\n");
    scanf("%d",&newnode->data);
    if(head==NULL)
    {
        head->data=newnode->data;
    }
    else
    {
        temp=head;
        while(temp->next!=NULL && count<position);
        {
            previousnode=temp;
            temp=temp->next;
            count=count+1;
            nextnode=temp;
        }
        previousnode->next=newnode;
        newnode->next=nextnode;
    }
}

void DeleteFront()
{
    node temp;
    if(head->next==NULL)
    {
        head==NULL;
    }
    else
    {
        head->next=temp;
        head=NULL;
        head=temp;
    }
}

void DeleteBack()
{
    node temp;
    if(head->next==NULL)
    {
        head==NULL;
    }
    else
    {
        temp=head;
        while(temp->next!=NULL)
        {
            temp=temp->next;
        }
        temp=NULL;
    }
}

void DeletePosition()
{
    node temp,nextnode,deletenode;
    int position,count=0;
    printf("Enter The Position At Which The Node Has To Be Deleted");
    scanf("%d",&position);
    if(head->next==NULL)
    {
        head==NULL;
    }
    else
    {
        temp=head;
        while(temp->next!=NULL && count!=position)
        {
            temp=temp->next;
            count=count+1;
        }
        deletenode=temp->next;
        nextnode=deletenode->next;
        deletenode->next=NULL;
        temp->next=nextnode;
    }
}

void Display()
{
    node temp;
    temp=head;
    if(head==NULL)
    {
        printf("Linked List Seems To Be Empty");
    }
    while(temp->next!=NULL)
    {
        printf("%d -> ",temp->data);
    }

}

int main()
{ 
    int choice;
    printf("\nLINKED LIST OPERATIONS\n\n");
    printf("Select An Option\n1 - Insert From Front\t   2 - Insert From Back\n3 - Insert At 
            A Position   4 - Delete At The Front\n5 - Delete At The Back\t   6 - Delete At A 
            Position\n7 - Display\t           8 - Exit\n\n");
    scanf("%d",&choice);
    switch(choice)
    {
        case(1) :
            {
                InsertFront();
            }
        case(2) :
            {
                InsertBack();
            }
        case(3) :
            {
                InsertPosition();
            }
        case(4) :
            {
                DeleteFront();
            }
        case(5) :
            {
                DeleteBack();
            }
        case(6) :
            {
                DeletePosition();
            }
        case(7) :
            {
                Display();
            }
        case(8) :
            {
                exit(0);
            }
        default :
            {
                printf("Invalid Input\n");
                exit(0);
            }

    }
}

【问题讨论】:

  • 我没有看所有代码,但立即看到了这个:if(head==NULL) { head-&gt;data=newnode-&gt;data; }。您正在取消引用 NULL 指针。你不能那样做。
  • 如何在不引用 NULL 指针的情况下编辑代码
  • 只是建议,尝试调试工具来解决此类错误(例如 Valgrind)。
  • 请先清理您的警告。你有一些像“head==NULL;”这样的行这不是你想要的。这将解决您的一些问题。
  • 你可能想要:if (head == NULL) { head = newnode;}

标签: c pointers data-structures linked-list singly-linked-list


【解决方案1】:

即兴发挥,您的第一个函数可能希望将头指针作为参数传递给函数,或者将返回值设为节点*,这样它就更通用了。 head 和 temp 都没有初始化,这三种方式发生:将现有变量地址分配给指针,进行 malloc 调用以分配存储空间,以及我不记得的第三种方式,请查看下面的资源。他们会帮忙的!您已经通过声明本地和全局节点*分配了一个指针变量,它们是兼容的,但是您没有使用对另一个变量的引用来初始化它们中的任何一个;你只是简单地声明了它们。必须使用来自 malloc、&addressof 等的对另一个地址的引用分配来初始化指针。Null 只是将它们标记为坏指针,而不会在测试时引发段错误。把它想象成一个没有分配任何存储空间的文件名......你不能保存到它,但它就在那里。由于两者都没有初始化,当您尝试分配给它们时,您会收到内存错误,因为尚未预留存储空间。这意味着......段错误!从表面上看,指针包含三个组成部分:[存储/对象的别名/命名区域的指针地址|内存块保留用于保存您声明所需的任何大小,在这种情况下,您将其放在一边:head = malloc( sizeof(struct node *)),这很可能分配了一个 8 字节的地址空间,该地址空间指向一个合适的存储区域->|*存储引用的地址的解引用值] 尝试运行 gdb 并在第一个函数中的两个指针上设置一个中断。我希望这会有所帮助,并希望我没有给你不好的建议......

我在这些方面也遇到了很多困难,这里有一些值得你花时间的好资源:

Stanfor CS 库 - 简明扼要地涵盖了指针、链表、二叉树、调试以准确找出是哪一行代码产生了错误; http://cslibrary.stanford.edu/

【讨论】:

  • 非常感谢 pchelper 抽出宝贵时间帮助我,我一定会实现一切并检查我的代码是否运行顺利。
猜你喜欢
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
  • 2016-11-19
  • 2022-01-14
  • 2017-02-25
  • 2016-07-12
相关资源
最近更新 更多