c - C 中的链表操作(段错误核心转储!)

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

过去几天我一直在尝试用 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);
            }

    }
}

最佳答案

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

我在这些方面苦苦挣扎,这里有一些值得您花时间的好资源:

斯坦福计算机科学图书馆 - CONCISELY 涵盖指针、链表、二叉树、调试以找出究竟是哪一行代码导致了您的错误; http://cslibrary.stanford.edu/

关于c - C 中的链表操作(段错误核心转储!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58419282/

相关文章:

c - 为什么在 realloc 函数中使用它之前 NULL ing 指针?

c# - ptr[i] 和 *(ptr + i) 有什么区别?

java - 有没有办法将 Knuth shuffle 应用于 Stack 数据结构?

data-structures - 使前n个元素保持排序顺序的最佳数据结构是什么?

C++链表删除所有

c - OpenCV多线程XInitThreads错误

c - 如何将用户监禁在目录中

c - 使用此特定函数将元素附加到循环链表的末尾

c# - Arquivo 制作意大利面的入口文件

c++ - 如何在多维数组上调用new