linked-list 相关问题

链表是一种数据结构,其中元素包含对下一个(以及可选的前一个)元素的引用。链接列表提供O(1)插入和删除具有已知内存位置的任何元素,O(1)列表并置,以及前(和可选后)位置的O(1)访问以及O(1)下一个元素访问。随机访问和随机索引插入/移除具有O(n)复杂性并且通常是未实现的。

LeetCode 问题已从排序列表中删除重复项

我尝试了通过 head 的方法,将其与我制作的新 ListNode 的 curr 进行比较。如果它尚不存在,请添加它。然而,它看起来对于复制品来说不能正常工作......

回答 1 投票 0

为什么删除链表中的元素需要花费O(1)

正如教科书所示,链表适用于频繁插入或删除的情况,因为这些操作的成本为 O(1)。然而,链表的节点不包含任何

回答 1 投票 0

删除LinkedList节点的正确方法是什么?

我已经在C中创建了一个链表。现在我想从任何位置删除节点,例如第一个节点或最后一个节点或任何第n个节点。我写了一段运行良好的代码。但问题是有人...

回答 1 投票 0

为什么Floyd的寻环算法,乌龟和兔子都需要从同一个位置开始?

我明白了,如果有一个循环,为什么兔子以2的速度移动,乌龟以1的速度移动,乌龟和兔子会相遇。因为如果循环长度为 k,则 (2-1)*t (到...之间的距离

回答 1 投票 0

使用哪个参数我可以调用add()函数并正常工作?

我正在学习工程学,正在学习数据结构和算法课程,这是我的作业,但我找不到解决方案。我应该调用 add() 函数的哪个参数? (抱歉我的不好

回答 1 投票 0

使用python3中的heapq模块合并k个排序列表

问题:- 合并 k 个排序列表。 我想使用最小堆来解决这个问题,它可以通过Python中的heapq模块来实现。 以下是该函数的示例代码... heapq.heappush(

回答 4 投票 0

以反向螺旋方式打印链表

给定一个链表,任务是以螺旋方式打印一个单链表。从第一个节点开始,然后是最后一个节点,然后是第二个节点,然后是倒数第二个节点,继续...

回答 1 投票 0

如何实现通用接口?

在类头中,我被要求实现一个名为 AddOnce 的接口。提供了接口,但是 IDE 似乎存在与通用 E 相关的任何问题。我该如何修复...

回答 1 投票 0

从数组列表创建链表

我的老师希望我们从头开始创建一个链表类,其中一部分是编写一个构造函数,该构造函数采用通用数组列表并将其转换为链表,而我在...

回答 1 投票 0

模板类型的链接列表,下一个指针是不同的专业化

我希望有一个带有单个类型参数的类模板,并且在类内部保存一个指向链表中下一个类的指针,但允许该类可能是不同的......

回答 1 投票 0

在 C# 中,为什么我们有一个链表类和节点类?

我在学习 C 后正在学习 C#。 在 C 语言中,我们有一个节点类,我们存储头节点并导航到任何地方。 但在 C# 中,除了节点类之外,我们还有一个链表类。 为什么我们需要链表类...

回答 1 投票 0

双向遍历多态派生类型的链表

考虑以下在 Fortran 中实现简单神经网络的方法:抽象多态类型层 类型,抽象 :: 层 真实的、可分配的 :: A(:,:) 类(l...

回答 1 投票 0

链表中的Python ListNode对象更新无法按预期工作

假设我们正在尝试在Python中实现一个链表,每个ListNode定义如下: 类列表节点: def __init__(self, val=0, next=None): self.val = val 自我.n...

回答 1 投票 0

如何在Rust中实现有序单链表?

我有一个功能齐全的单链表。我唯一想做的额外事情就是添加一个值来排序,这样当你push()一个项目时,它会爬行列表并将节点插入到正确的sp中...

回答 1 投票 0

将单链表转换为双链表

我这里有一个制作拼贴画的程序的单个链接列表。这运行完美,但我想知道如何使其成为双链表。我真的不知道什么是双链接...

回答 4 投票 0

括号问题中的堆栈实现错误

我正在解决leet代码上的堆栈问题。许多测试用例已经通过。它只是失败了“{[]}” que- https://leetcode.com/problems/valid-parentheses/description/ 我的提交- http...

回答 1 投票 0

有什么方法可以将对象变成数组吗?

我正在做这个狂欢编程,它想要将某些东西实现到 MyLinkedList 中 我想我已经明白了它的要点,但我仍然无法弄清楚它,狂欢希望我使用这个:...

回答 1 投票 0

链表第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

将数组全部转换为链表

我目前正在编写一个 JUnit 测试,需要树图作为预期值。 所述 TreeMap 看起来像这样: Map>预期 = new TreeMap<>(); 我填写...

回答 3 投票 0

为什么我无法让基于指针的链表气泡交换工作?

类节点{ 民众: 整数数据; 下一个节点*; Node() { //默认构造函数 数据=空; 下一个=空; } 节点(整数值){ 这->数据=值...

回答 1 投票 0

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