Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的链表操作(分段错误核心转储!)_C_Pointers_Data Structures_Linked List_Singly Linked List - Fatal编程技术网

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

C中的链表操作(分段错误核心转储!),c,pointers,data-structures,linked-list,singly-linked-list,C,Pointers,Data Structures,Linked List,Singly Linked List,在过去的几天里,我一直试图在c语言中实现链表操作,但我一次又一次地遇到同样的错误,即“分段错误”。我不知道逻辑的哪一部分出了问题。奇怪的是,在解决了所有警告和错误之后,代码就不会执行了。如果有人向我指出我是否正确使用了链表和指针,那也太好了。我的代码似乎很难发布,但我就是不能缩短它。我再次感谢所有能够帮助我的人,让这段代码更具可读性,减少歧义 #include<stdio.h> #include<stdlib.h> struct nodes { int dat

在过去的几天里,我一直试图在c语言中实现链表操作,但我一次又一次地遇到同样的错误,即“分段错误”。我不知道逻辑的哪一部分出了问题。奇怪的是,在解决了所有警告和错误之后,代码就不会执行了。如果有人向我指出我是否正确使用了链表和指针,那也太好了。我的代码似乎很难发布,但我就是不能缩短它。我再次感谢所有能够帮助我的人,让这段代码更具可读性,减少歧义

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

    }
}
#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
};
typedef结构节点*节点;
节点头=空;
void InsertFront()
{
节点温度=NULL;
printf(“输入节点的值\n”);
scanf(“%d”,&temp->data);
if(head==NULL)
{
头部->数据=温度->数据;
}
其他的
{
温度->下一步=头部;
压头=温度;
}
}
void InsertBack()
{
节点温度,新节点;
printf(“输入节点的值”);`在此处输入代码`
scanf(“%d”,&newnode->data);
if(head==NULL)
{
head->data=newnode->data;
}
其他的
{
温度=水头;
while(临时->下一步!=NULL)
{
温度=温度->下一步;
}
newnode=temp->next;
}    
}
void InsertPosition()
{
节点previousnode、newnode、nextnode、temp;
int位置,计数=0;
printf(“输入必须插入新节点的位置”);
扫描频率(“%d”,位置(&P);
printf(“输入节点的值\n”);
scanf(“%d”,&newnode->data);
if(head==NULL)
{
head->data=newnode->data;
}
其他的
{
温度=水头;
while(temp->next!=NULL&&countnext;
计数=计数+1;
下一个节点=温度;
}
上一个节点->下一个=新节点;
newnode->next=nextnode;
}
}
void DeleteFront()
{
节点温度;
if(head->next==NULL)
{
head==NULL;
}
其他的
{
头部->下一步=温度;
head=NULL;
压头=温度;
}
}
void DeleteBack()
{
节点温度;
if(head->next==NULL)
{
head==NULL;
}
其他的
{
温度=水头;
while(临时->下一步!=NULL)
{
温度=温度->下一步;
}
温度=零;
}
}
void DeletePosition()
{
节点临时、下一个节点、删除节点;
int位置,计数=0;
printf(“输入必须删除节点的位置”);
扫描频率(“%d”,位置(&P);
if(head->next==NULL)
{
head==NULL;
}
其他的
{
温度=水头;
while(临时->下一步!=空值和计数!=位置)
{
温度=温度->下一步;
计数=计数+1;
}
deletenode=temp->next;
nextnode=deletenode->next;
deletenode->next=NULL;
temp->next=nextnode;
}
}
无效显示()
{
节点温度;
温度=水头;
if(head==NULL)
{
printf(“链接列表似乎为空”);
}
while(临时->下一步!=NULL)
{
printf(“%d->”,临时->数据);
}
}
int main()
{ 
智力选择;
printf(“\n链接列表操作\n\n”);
printf(“选择一个选项\n1-从前面插入\t2-从后面插入\n3-在
A位置4-前面删除\n5-后面删除\t 6-后面删除
位置\n7-显示\t 8-退出\n\n”);
scanf(“%d”,选择(&C);
开关(选择)
{
案例(1):
{
InsertFront();
}
案例(2):
{
InsertBack();
}
案例(3):
{
插入位置();
}
案例(4):
{
DeleteFront();
}
案例(5):
{
DeleteBack();
}
案例(6):
{
DeletePosition();
}
案例(7):
{
显示();
}
案例(8):
{
出口(0);
}
违约:
{
printf(“无效输入\n”);
出口(0);
}
}
}

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

我也曾为这些问题苦苦挣扎过,这里有一些很好的资源值得一试