添加到排序的链表时,如何避免出现段错误?

纳西姆·艾哈迈德|

添加新元素时,我需要创建一个新函数,它将其放置在列表中,以便列表保持排序。我不确定我的实现是否正确,我与小组成员的第一次尝试给出了分段错误。当我尝试自己做时,它什么也没做。任何帮助将不胜感激。这是我的代码:

头文件:

typedef struct s{
        int value;
        struct s *next, *previous;
} node, *node_ptr;

C文件:

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

void
print_list(node_ptr list) {
   // walk the list to print out the contents
   while (list) {
        printf("%d ",list->value);
        list = list->next;
   }
   printf("\n");
}

void delete_list(node_ptr list) {
   // walk the list to delete the elements
   node_ptr t;
   while (list) {
        t = list;
        list = list->next;
        free(t);
   }
}

node_ptr new_node(int value) {
   node_ptr t = (node_ptr)malloc(sizeof(node));
   t->value = value;
   t->next = t->previous = NULL;
   return t;
}

node_ptr add_to_back(node_ptr list, int value) {
   node_ptr t = list;
   node_ptr s = new_node(value);
   // special case: starting with an empty list
   if (t == NULL) return s;
   // at this point we know there is a least one element in
   // the list
   while (t->next != NULL)  // walk the list looking for the last element 
     t = t->next;
   // we are at the end so now we arrange the pointers
   t->next = s;
   s->previous = t;
   return list;
}

// my implementation after some research
node_ptr add_sorted(node_ptr list, int value) {
        node_ptr temp = list;
        node_ptr newNode;

        if(temp == NULL || temp->value < newNode->value)
        {
                newNode->next = temp;
                temp = newNode;
        }
        else
        {
                while(temp != NULL && temp->value < value)
                {
                        temp = temp->next;
                }
                newNode->next = temp->next;
                temp->next = newNode;
        }
        return newNode;
}

// second implementation with team
/*
node_ptr add_sorted2(node_ptr list, int value) {
        // This is the function you need to implement
        // when adding a new element place it in the list so that the list stays in sorted order.
        node_ptr temp = list;
        node_ptr n = new_node(value);

        if(temp == NULL)
        {
                temp->value = value;
                temp->next = NULL;
                return n;
        }
        else if(temp->next != NULL) {   
        while(temp->next != NULL) {

                 if(temp->value <= value) {
                        n->next = temp->next;
                        temp->next = n;
                        return n;               
                }
                else if(temp->value > value) {
                        temp = temp->next;
                }
                else {
                        temp->next = n;
                        return n;
                }
        }
        }
        return n; 
}
*/

int
main() {
   int in_val;
   node_ptr my_list = NULL;
   node_ptr sorted_list = NULL;
   scanf("%d",&in_val);
   while (in_val > 0) {  // going to read input until see 0 or negative
           my_list = add_to_back(my_list,in_val);
           sorted_list = add_sorted(sorted_list,in_val);
           scanf("%d",&in_val);
   }
   printf("List:\n");
   print_list(my_list);
   printf("Sorted List:\n");
   print_list(sorted_list);
   delete_list(my_list);
   delete_list(sorted_list);
}
伊哈罗布·阿西米(Iharob Al Asimi)

分段错误对我很清楚,您在这里使用了未初始化的指针

if(temp == NULL || temp->value < newNode->value)
//                                  ^

或在任何newNode地方进行ANY OTHER的取消引用,因为newNode它从未在您的代码中初始化。

如果是temp == NULL,并且您尚未初始化newNode,则未定义行为

在保留顺序的同时将节点添加到列表很容易,

  1. 创建新节点
  2. 如果创建成功,则遍历列表,直到下一个节点大于新节点为止(取决于所需的顺序)。
  3. 找到它后,将当前节点链接next到新节点,并且该next节点应该是新节点的next

仅此而已。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将节点添加到链表时出现分段错误

将项目添加到链表段错误的末尾

将元素添加到已排序的链表C ++中时出现的问题

搜索链表时出现段错误

将Firebase添加到Flutter时出现GradGrad错误

将AFNetworking添加到新项目时出现错误

在实现链表时可变借入错误,其中在Rust中将新元素添加到末尾

重新排列双向链表时偶尔出现段错误

堆栈清除后打印链表时出现段错误

malloc():添加到链表时损坏的顶部大小

将项目添加到“使用...结束时”时出现Excel错误91

我在 django 中注册时尝试将用户添加到定义的组时出现错误

如何在链表中执行添加到特定号码?

将数据添加到Firestore时如何避免不一致?

添加到ArrayList时如何避免“ lambda表达式中的参数类型不兼容”?

将列添加到Pandas数据框时如何避免列和DatetimeIndex之间的混淆

将新节点添加到群集时,如何避免查询花费很长时间

将站点添加到Varnish时如何避免重复sed命令?

将CER转换为PFX时出现强制错误(用于添加到Azure门户)

将累加器逻辑添加到Web服务请求时出现XSLT错误

尝试通过状态将类添加到列表项元素时出现解析错误

将预处理层添加到顺序模型时出现 Keras 错误

将成员添加到MailChimp列表时出现400错误的请求

将 navigation.navigate 添加到组件时出现“错误:未定义不是对象”?

将<!DOCTYPE html>添加到JSP页面时出现Javascript错误

将值字段添加到数据透视表时出现错误

在React Native中将{/ * comment * /}添加到返回函数中时出现“ Invariant Violation”错误

将新列添加到数据表时出现 DBNull 错误

尝试将新的List对象添加到预定义的List对象时出现错误