c - 链表程序错误

标签 c

用 C 语言为链表编写了这个程序。尝试在链接列表的末尾插入时出现段错误,插入时出现错误。所有其他情况都有效,例如最初插入、插入两个元素之间等。想了很久还是想不通?

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

void insert();
void display();
void search();
void delete();
struct node{
    int val;
    struct node *next;
}*head;
int num,count=0;

void main()
{
    char dec = 'E';
    printf("Welcome to the Linked List C Program!\n");
    head = NULL;

    while(1){
        printf("Make a Choice:\n");
        printf("I.Insert\nD.Delete\nS.Search\nd.Display\nE.Exit\n");
        scanf(" %c",&dec);
        switch(dec){
            case 'I':
                insert();
                break;
            case 'D':
                delete();
                break;
            case 'S':
                search();
                break;
            case 'd':
                display();
                break;
            case 'E':
                exit(0);
                break;
            default:
                printf("Wrong input Try Again!\n");
        }
    }
}

void insert(){
    printf("Enter the number to insert:");
    scanf("%d",&num);
    struct node *temp;
    struct node *newnode;
    struct node *prev;
    int c =0;
    //temp = (struct node *)malloc(sizeof(struct node));
    newnode = (struct node *)malloc(sizeof(struct node));
    //prev = (struct node *)malloc(sizeof(struct node));
    newnode->val = num;
    if(head==NULL)
    {
        head = newnode;
        head->next = NULL;
    }
    else
    {
        temp = head;
        //searching whether linked list is in start or end
        while(temp->val<num && temp !=NULL)
        {
            printf("Index:%d Value:%d Address:%p",c,temp->val,temp);
            prev = temp;
            temp = temp->next;
            c++;
            printf("TEST\n");

        }
        if(c==0)
        {
            head = newnode;
            head->next = temp;

        }
        else
        {
            prev->next=newnode;
            newnode->next=temp;
        }
    }
}

void display()
{
    struct node *temp;
    temp = (struct node *)malloc(sizeof(struct node));
    temp = head;
    while(temp != NULL)
    {
        printf("%d",temp->val);
        temp = temp->next;
    }
    if(temp == NULL)
    {
        printf("Not present!");
    }

}

void search()
{
    printf("Enter the number tosearch for:\n");
    scanf("%d",&num);
    struct node *temp;
    temp=head;
    int c=0;
    while(temp!=NULL)
    {
        if(temp->val==num)
        {
            printf("Present at position %d",c);
            c++;
        }
        else if(temp == NULL)
        {
            printf("Not present!");
        }
        else
            c++;
        temp =temp->next;
    }
}

void delete()
{
    struct node *temp;
    struct node *prev;
    temp = head;
    printf("Enter the value to delete:\n");
    scanf("%d",&num);
    int c=0;
    while(temp!=NULL)
    {
        if(temp->val==num)
        {
            printf("Present at position %d",c);
            printf("Deleting this element");
            prev->next=temp->next;
            free(temp);
            c++;
        }
        else
            c++;
        prev = temp;
        temp =temp->next;

    }
    if(temp == NULL)
    {
        printf("Not present!");
    }

}

最佳答案

在您的 while 循环中,您在检查 temp 是否为 NULL 之前检查了 temp 的成员之一:

while(temp->val<num && temp !=NULL)

相反:

while ((temp != NULL) && (temp->val < num))

关于c - 链表程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18519339/

相关文章:

c - 来源洞察 : Show me Enum Values

C Scanf : What does a return value of -1 mean?

c - 如何修复附件 K 中安全函数发出的运行时错误?

c - 使用文件夹编译共享库的 Makefile

c - 如何使用(Scitools)的perl api在.c文件中查找特定字符串 了解?

c++ - 制作 Makefile

c - 使用 zlib 解压缩 zip 文件

c - execlp() 无法检索正确的输入

c - 将任意/自定义标签添加到 (ex)ctags 文件

c - 动态分配内存大小导致堆栈粉碎错误