linked-list 相关问题

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

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

我正在解决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

将函数作为函数参数传递给链表排序

我正在编写一个程序,它提供了创建和操作链接列表的工具。我已经完成了添加、删除节点等方法。但是,为了创建排序的方法...

回答 1 投票 0

LinkedList定义

我无法理解 LinkedList。当我遇到这个定义时,我正在练习 leetcode: 单链表的定义。 类列表节点{ 整数值; 列表下一个节点;

回答 1 投票 0

每次在链接列表中添加新的值时,之前的值都会被覆盖

我正在尝试将原始类“day”的对象添加到链接列表中,这些对象具有称为“classment”的整数值,以便我可以跟踪哪一天先到来。问题是,e...

回答 1 投票 0

增强可截断素数的排序链表的性能

我目前正在解决欧拉计划的第 37 个问题(“可截断素数”)。本质上,该任务涉及识别 11 个素数,这些素数具有独特的属性,当任何 d...

回答 1 投票 0

使用 Java 从队列中删除 var

我正在尝试编写一个获取队列和参数(int)的函数,该函数删除队列中等于参数的var。这是我的代码: 公共静态无效remove_it(队列<

回答 1 投票 0

删除最后第N个

如果我的链表有10000个节点,那么如何在不一次又一次遍历的情况下从最后一个节点删除第N个节点? 我一次又一次地遍历链表以找到长度,然后遍历...

回答 1 投票 0

将 Data 实例表示为 C++ 链表中的指针

假设我们是一家汽车制造商,我们有一个抽象的 Part 类,如下所示: 类部分{ // 请注意,所有方法都是 const 方法。 民众: 部分():

回答 1 投票 0

合并排序链表时出现无限递归错误

我正在链表上应用合并排序。 这是问题:https://leetcode.com/problems/sort-list/ void mergesort(ListNode* head,ListNode* low, ListNode* high){ ListNode*慢=低;

回答 1 投票 0

尝试在Java中反转队列

我正在尝试使用递归来反转队列。我已经使用队列和堆栈实现了该过程,但我也想学习递归方式。 以下是我们的代码...

回答 1 投票 0

如何将数字添加到排序列表中?

所以我学校给我布置了一个作业,要在排序列表中添加一个数字,而不仅仅是一个列表-IntNode列表,如果我做对了,那就是一个链接列表(也许我错了,我不知道这是什么事情是

回答 1 投票 0

链表返回空列表

我在链表的头部插入节点,并使用 print() 方法打印每个节点。 每个节点由两 (2) 个字符串和一 (1) 个整数组成。 链表插入的工作原理似乎是......

回答 1 投票 0

C:二分查找和二分插入

我目前正在解决欧拉项目上的不同幂问题 (29)。该问题涉及查找 (a^b) 的不同乘积的数量,其中 (2 < a < 100) and (2 < b &...

回答 2 投票 0

Rust链表push_sort函数

我正在尝试用 Rust 实现我自己的链表(出于学习目的)。 我使用的结构: 结构节点 { 值:T, 下一个: 选项>>, } 暗示 我正在尝试用 Rust 实现我自己的 Linkedlist(出于学习目的)。 我使用的结构: struct Node<T> { value: T, next: Option<Box<Node<T>>>, } impl<T> Node<T> { pub fn new(val: T) -> Self { return Node { value: val, next: None, }; } pub fn new_with_next(val: T, next: Option<Box<Node<T>>>) -> Self { return Node { value: val, next: next, }; } } struct List<T> { head: Option<Box<Node<T>>>, } 我已经实现了一个函数,可以将项目推送到排序列表中的正确位置(我们假设是这样)。它工作得很好(经过长时间的努力),但作为一个 rust 初学者,并且因为 rust 看起来与我使用的其他语言非常不同,我想知道是否有更惯用的方法在 Rust 中实现这一点。 fn push_sort(&mut self, val: T) where T: std::cmp::PartialOrd { match &mut self.head { Some(head) => { if head.value >= val { self.head = Some(Box::new(Node::new_with_next(val, self.head.take()))); return; } }, None => { self.head = Some(Box::new(Node::new(val))); return; } }; let mut current = &mut self.head; while let Some(node) = current { if node.next.is_none() { node.next = Some(Box::new(Node::new(val))); return; } if val < node.next.as_ref().unwrap().value { node.next = Some(Box::new(Node::new_with_next(val, node.next.take()))); return; } current = &mut node.next; } } 您不需要 match 和 while let ,它们都可以在单个循环中完成,与检查循环中的节点是否为 Some 或 None 及其条件相同: fn push_sort(&mut self, val: T) where T: std::cmp::PartialOrd, { let mut current = &mut self.head; while let Some(c) = current { if c.value >= val { let old = std::mem::replace(c, Box::new(Node::new(val))); c.next = Some(old); return; } current = &mut c.next; } *current = Some(Box::new(Node::new(val))); }

回答 1 投票 0

连接后保持链表对象不同

嗨,我正在努力实现一个链表,而不导入任何Java库。我已经实施了以下课程。我有几个测试用例来确保链接列表对象确实......

回答 1 投票 0

为什么创建指向堆栈上节点的指针会导致我的链表显示函数在两次插入后进入无限循环?

我正在学习链表,并试图理解为什么在链表中插入节点时使用“new”运算符。具体来说,我想知道为什么以下实施...

回答 1 投票 0

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