singly-linked-list 相关问题

一个链表,其中每个节点仅指向列表中的下一个节点,而不是双链表,其中每个节点都指向下一个节点和前一个节点。

链表第n次插入失败

我写了一段代码在LinkedList中插入数据。但是输出数据有问题。这是代码。 #包括 #包括 #包括 #包括 我写了一段代码来在LinkedList中插入数据。但是输出数据有问题。这是代码。 #include <stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> struct node { char *name; int age; struct node *next; }; struct node *linkeslist1head= NULL; struct node *linkedlist1tail= NULL; void llinsertend(const char *a,const int *b){ struct node *current = malloc(sizeof(struct node)); if(current == NULL){ printf("Current creation failed.\n"); } current->name = malloc(strlen(a)+1); if(current->name == NULL) { printf("String allocation failed\n"); } strcpy(current->name,a); current->age = *b; if(linkeslist1head == NULL){ linkeslist1head = current; linkedlist1tail = current; }else{ //If the list is not empty, append the new node to the end linkedlist1tail->next = current; // Update tail to point to the new last node linkedlist1tail = current; } } void llinsertbegin(const char *a, const int *b) { struct node *newnode = malloc(sizeof(struct node)); if (newnode == NULL) { printf("Memory allocation failed\n"); return; } newnode->name = malloc(strlen(a) + 1); if (newnode->name == NULL) { printf("String allocation failed\n"); free(newnode); return; } strcpy(newnode->name, a); newnode->age = *b; if (linkeslist1head == NULL) { // If the list is empty newnode->next = NULL; linkeslist1head = newnode; linkedlist1tail = newnode; } else { // If the list is not empty newnode->next = linkeslist1head; linkeslist1head = newnode; } } void llinsertaftern(const char *a, const int *b, int n) { struct node *current = linkeslist1head; int i; for (i = 1; current != NULL && i < n; i++){ current = current->next; // Iterate until the (n-1)th node or until current becomes NULL } if (current == NULL){ printf("LL short\n"); return; // Exit the function if current is NULL } printf("Reached node %d\n", i); struct node *newnode = malloc(sizeof(struct node)); if (newnode == NULL) { printf("Memory allocation failed\n"); return; } newnode->name = malloc(strlen(a) + 1); if (newnode->name == NULL) { printf("String allocation failed\n"); free(newnode); return; } strcpy(newnode->name, a); newnode->age = *b; if (current == NULL) { printf("LL is shorter than %d\n", n); free(newnode->name); free(newnode); return; } newnode->next = current->next; current->next = newnode; } void outputLinkedList(struct node *head){ struct node *p = head; while(p != NULL){ printf("Name:%s Age:%d\n",p->name,p->age); p = p->next; } printf("\n"); } int main() { printf("How many persons' details you want to add\n"); int t; scanf("%d",&t); getchar(); for(int i=1;i<=t;i++){ int x; char name[50]; scanf("%s",name); getchar(); scanf("%d",&x); llinsertend(name,&x); } int x=10,y=20,z=30; llinsertbegin("facebook",&x );//(const int *) 10 llinsertbegin("instragram", &y); llinsertbegin("whatsapp", &z); outputLinkedList(linkeslist1head); int code=1200,pos = 3; llinsertaftern("Dhaka",&code,pos); outputLinkedList(linkeslist1head); } 在第二个输出中,LinkedList 在我的 CLion 和 Codeblocks 中都不起作用,但它在编译器资源管理器中起作用。我的意思是通过 llinsertend() 函数添加一些元素然后 llinsertbegin() 输出函数完美运行。但是当我使用 llinsertaftern 函数时,它在调试器中显示分段错误。假设如果我通过 insertend 函数添加 3 个元素,并通过 insertbegin 函数添加 3 个元素,那么 pos = 3 的 inseraftern 的问题是什么。它应该工作得很好。 您的代码包含许多错误。例如,让我们考虑函数 llinsertend void llinsertend(const char *a,const int *b){ struct node *current = malloc(sizeof(struct node)); if(current == NULL){ printf("Current creation failed.\n"); } current->name = malloc(strlen(a)+1); if(current->name == NULL) { printf("String allocation failed\n"); } strcpy(current->name,a); current->age = *b; if(linkeslist1head == NULL){ linkeslist1head = current; linkedlist1tail = current; }else{ //If the list is not empty, append the new node to the end linkedlist1tail->next = current; // Update tail to point to the new last node linkedlist1tail = current; } } 首先,如果未分配新节点,则继续处理空指针 if(current == NULL){ printf("Current creation failed.\n"); } current->name = malloc(strlen(a)+1); //... 同样,如果未分配字符数组,您将再次继续使用空指针 if(current->name == NULL) { printf("String allocation failed\n"); } strcpy(current->name,a); //... 并且您忘记将新创建节点的数据成员next设置为NULL。 所以只有这一个函数包含三个错误。 注意函数llinsertaftern可以将新节点追加到列表的尾部。然而该函数不会改变指针linkedlist1tail。

回答 1 投票 0

Java:在 LinkedList 末尾插入节点

我目前正在学习Java和数据结构,我正在尝试使用插入将双精度数组中的值插入到LinkedList中,只需在列表末尾插入每个元素。我已经...

回答 1 投票 0

为什么这个数据结构会出现分段错误?

鉴于数据结构 #定义数据结构_H #定义最大索引 307 typedef enum {So, Mo, Di, Mi, Do, Fr, Sa} eDayofTheWeek; 类型定义结构{ 国际日; 月份; 年份;

回答 1 投票 0

这些链表指针声明的差异

有什么区别 struct LinkedList *current = malloc(sizeof(struct LinkedList)); 和 结构链表*当前; 在哪里 结构体链表{ 整数数据; 结构链表*下一个; } 什...

回答 2 投票 0

这些链表指针的区别

有什么区别 1struct LinkedList *current = malloc(sizeof(struct LinkedList)); 和 2struct LinkedList *当前; 在哪里 结构体链表{ 整数数据; 结构链表*下一个; } 当...

回答 1 投票 0

对链表进行排序最简单的方法

我正在尝试为链表编写非常基本的排序方法。我遇到了未处理的异常。我犯了什么错误?这是我的代码:- struct LinkedNode // 链表结构 { ...

回答 3 投票 0

CompareTo 方法:返回类型:GREATER、EQUAL、LESS

我收到以下 4 个错误: 字符预期错误 字符预期错误 我正在做一个 LinkedList java 项目,需要实现 CompareTo 方法。我对 Java 很生疏,

回答 1 投票 0

DSA、链表、交换节点

我正在尝试解决成对的交换节点(链表)。我有正确的代码,但在解释交换步骤时我陷入困境。 这是代码: def swapPairs(头): pre = 列表节点(0) 预。

回答 1 投票 0

为什么单链表中的 head 为空?

为什么当我们尝试创建一个单链表时,我们将类中的 Head 设为 NULL ,而不将 Head 的 Next 设为 Null 。在有关链表的函数中,为什么我们要制作 Ne...

回答 2 投票 0

将二进制从链表转换为整数

我正在解决一个将链表中的二进制数转换为整数的问题。当我尝试 console.log 时,我得到“未定义”。有任何想法吗? 我对 JavaScript 还很陌生。我想我被困住了

回答 1 投票 0

链表用户输入

我正在尝试创建一个链接列表,该列表将接受用户的输入,即他想要创建链接列表的数量,而不使用内置的 LinkedList 函数。但如果用户输入负数...

回答 1 投票 0

不明白将新项目附加到链接列表(在开始和结束时)时的错误

如上所述,我似乎无法理解代码中的错误,我在这段特定的代码中做错了什么: Node的写法是这样的: 结构节点{ 整数数据;

回答 2 投票 0

如何在Python中创建链表

我正在尝试解决Python中的链表编码挑战。我只给出了以下课程来创建链接列表 # 单链表的定义。 类列表节点(对象): 定义

回答 4 投票 0

为什么 while( curr && curr->next) 不等于 while( curr->next && curr )?

我试图解决从leetcode中的排序列表中消除重复的问题,我找到了一个解决方案,但是当我尝试更改while循环中条件的位置时,它给了我nullptr错误。我不明白...

回答 1 投票 0

Python - LinkedList 混乱

类节点: def __init__(self, data=None, next=None): self.data = 数据 self.next = 下一个 类链表: def __init__(自身): self.head = 无 def insert_a...

回答 1 投票 0

找到倒数第k个节点

我编写了下面的函数来查找倒数第 k 个节点。然而,一个隐藏的测试用例失败了。 请让我知道下面的代码有什么问题。 def find_kth_from_end(l, k): 慢=l.head ...

回答 1 投票 0

LeetCode160-两个链表的交集

这是LeetCode网页上第一个两个链表相交的例子。 第一个链表 A 是 [4,1,8,4,5],第二个链表 B 是 [5,6,1,8,4,5]。我找到了...

回答 3 投票 0

LeetCode - 2. 两个数字相加

我正在尝试解决这个 Leetcode 问题,我已经实现了一个似乎适用于大多数情况的解决方案,但它在特定情况下失败了。这是提供的代码: 类列表节点: ...

回答 1 投票 0

删除并返回索引链接列表中的项目

包linkedList.list; 导入 linkedList.node.ListNode; 公共接口 LinkedList,T> { 公共布尔 isEmpty(); 公共 int 大小();

回答 2 投票 0

运行时错误 - “引用绑定到‘ListNode *’类型的空指针 (stl_vector.h)”

ListNode* mergeKLists(向量&列表) { ListNode* newNode = 列表[0]; ListNode* 小 = NULL; ListNode* 大 = NULL; for(int i = 1; i < lists.s...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.