c - 创建链表时出现段错误

标签 c linked-list singly-linked-list

我正在编写一个小程序,它将数据和 key 存储在链表结构中,并根据用户的 key 检索数据。该程序还检查它是否是唯一键,如果是,则通过在列表的前面创建一个节点来存储数据。但下面的代码总是抛出段错误。

 #include<stdlib.h>

/* Node having data, unique key, and next */.  
struct node
{
    int data;
    int key;
    struct node *next;
}*list='\0',*p;

/* Create a node at the front */
void storeData(int data_x,int key_x)
{
    int check_key;
    position *nn;  //nn specifies newnode
    nn=(position)malloc(sizeof(struct node));

/* Segmentation Fault occurs here */
    if(list->next==NULL)
    {
        nn->next=list->next;
        nn->data = data_x;
        nn->key = key_x;
        list->next = nn;
    }
    else
    {
      check_key=checkUniqueKey(key_x);
      if(check_key != FALSE)
      {
         printf("The entered key is not unique");
      }
      else
      {
          nn->data = data_x;
          nn->key = key_x;
          nn->next=list->next;
          list->next=nn;
      }

   }
}

/* Retreive data based on a key */

int retreiveData(int key_find)
{
   int ret_data = NULL;
   p=list->next;
   while(p->next != NULL)   
   {
        if(p->key == key_find)
        {
            ret_data = p->data;
            break;
        }
     p=p->next;
   }  
   return(ret_data);
}
/*  Checks whether user key is unique */
int checkUniqueKey(int key_x)
{
    int key_check = FALSE;
    p=list->next;
    while(p->next != NULL)
    {
        if(p->key == key_x)
        {
          key_check = TRUE;
          break;    
        }
      p=p->next;
    }
    return(key_check);
}

动态分配后,storeData 函数中出现段错误。

最佳答案

您的代码存在一些问题:

  • 你的列表处理是有缺陷的:你总是取消引用全局指针list,甚至在创建任何列表项之前也是如此。您应该通过将 listNULL 进行比较来测试列表是否为空。

  • 类型位置未定义。避免将指针隐藏在 typedef 后面,这是造成困惑的一个重要原因,这解释了您对列表指针的错误处理。

  • 避免使用名称 p 定义全局变量,无论如何都不需要它。将 p 定义为使用它的函数中的局部变量。

  • NULL 是空指针,0 是零整数值,\0 是 C 末尾的空字节字符串。所有 3 个值的计算结果均为 0,但并不总是可以互换。 为了获得更好的可移植性和可读性,请针对每种情况使用适当的。

这是一个改进的版本:

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

/* Node having data, unique key, and next */.  
struct node {
    int data;
    int key;
    struct node *next;
} *list;

/* Create a node at the front */
void storeData(int data_x, int key_x) {
    if (checkUniqueKey(key_x)) {
        printf("The entered key is not unique\n");
    } else {
        /* add a new node to the list */
        struct node *nn = malloc(sizeof(struct node));
        if (nn == NULL) {
            printf("Cannot allocate memory for node\n");
            return;
        }
        nn->data = data_x;
        nn->key = key_x;
        nn->next = list;
        list = nn;
    }
}

/* Retrieve data based on a key */
int retrieveData(int key_find) {
    struct node *p;
    int ret_data = 0;

    for (p = list; p != NULL; p = p->next) {
        if (p->key == key_find) {
            ret_data = p->data;
            break;
        }
    }
    return ret_data;
}

/* Checks whether user key is unique */
int checkUniqueKey(int key_x) {
    struct node *p;
    int key_check = FALSE;

    for (p = list; p != NULL; p = p->next) {
        if (p->key == key_x) {
            key_check = TRUE;
            break;  
        }
    }
    return key_check;
}

关于c - 创建链表时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48846122/

相关文章:

c - 如何将 BMP 文件读入 C 中的像素网格?

可以生成一个随机字符串,其中字符的频率是固定的吗?

C++ 将整数节点按升序插入模板化单链表类 - 作业

java - 自定义链表与官方链表

C 移位插入链表

c 将字符串指针分配给其他字符串指针

c - 使用指针函数的程序方差和标准差

java - 如何使用 Java 创建二叉树最大深度中包含的节点的链表

c - 单链表 c - 可以在此函数中未初始化地使用 (-Wall -Werror)

.net - 使用大数组时,LinkedList 内存消耗与 List