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

Linked List Operations In C (Segmentation Error Core Dumped!)

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

#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);
            }

    }
}

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

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

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