标签: doubly-linked-list

双链表:不兼容的指针类型

目前我正在研究C中平衡B树的实现.我决定使用双链表但我遇到了一些问题.目前我收到第94,95和96行的警告,因为显然指针类型不兼容.我真的不知道如何和任何帮助将不胜感激.

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

typedef struct {
    int data1;
    int data2;
    int data2exists; // no: 0 , yes: 1
    struct node * parent;
    struct node * left;
    struct node * middle;
    struct node * right;
} node;

node * insert(int *, node *, node *);
void getInput(int *);
node * createNode(int *);
void quickSwap(node *, int *, int *, int *, int *);
node * splitLeaf(int *, int *, int *, node *, node *);
void printTree(node *);

void …
Run Code Online (Sandbox Code Playgroud)

c struct pointers doubly-linked-list

2
推荐指数
1
解决办法
128
查看次数

冒泡排序双链表

我的双向链表的泡泡分类功能有问题.当我以单链接的方式对节点进行排序时(仅使用 - > next),它正在工作,但我无法使用 - > prev指针.这是我正在使用的代码:

void sort(int count)
{
    struct data *tmp,*current,*nextone;
    int i,j;
    for(i=0;i<count;i++)
    {
        current = first;
        for(j=0;j<count-1-i;j++ )
        {
            if(current->number > current->next->number)
            {
                nextone = current->next;
                current->next = nextone->next;
                nextone->next = current;
                if(current == first)
                {
                    first = nextone;
                    current = nextone;
                }
                else
                {
                    current = nextone;
                    tmp->next = nextone;
                }
            }
            tmp = current;
            current = current->next;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的结构(使用列表的第一个和最后一个元素的全局变量):

struct data    
{
    int id;
    char name[20];
    int number;
    struct data *next; …
Run Code Online (Sandbox Code Playgroud)

c sorting list bubble-sort doubly-linked-list

2
推荐指数
1
解决办法
2万
查看次数

为什么std :: list :: splice不是自由函数?

Splice是一个成员函数,可在恒定时间内将链接列表的一部分放入另一个链接列表。

为什么它需要成为成员函数?我希望我可以在列表本身上有一个句柄,而只将迭代器拼接到列表中。为什么要拼接的列表除了开始和结束迭代器之外还需要作为参数?

为了进行测试,我列出了三个列表,并混合了容器和迭代器。请参见下面的接头,其中的容器(空)与迭代器(test0和test1)不匹配:

list<int> test0;
list<int> test1;
list<int> empty;
test0.push_back(1);
test0.push_back(2);
test0.push_back(3);
test1.push_back(4);
test1.push_back(5);
test1.push_back(6);
empty.splice(test0.end(), empty, test1.begin(), test1.end());
printf("empty size: %ld\n", empty.size());
printf("test0 size: %ld\n", test0.size());
printf("test1 size: %ld\n", test1.size());
for (const auto& i : test0) {
  printf("%d\n", i);
}
Run Code Online (Sandbox Code Playgroud)

出乎意料的是,一切都很好,甚至大小!

empty size: 0
test0 size: 6
test1 size: 0
1
2
3
4
5
6
Run Code Online (Sandbox Code Playgroud)

我可以稍微了解一下迭代的工作原理,因为它一直运行到next为null为止,而与容器的前/后指针无关。但是它如何使尺寸正确呢?也许大小是动态计算的?

编辑:根据对大小的这种解释,大小在线性时间内动态计算列表的。因此,容器实际上只是一个虚拟参数。也许仅在添加新元素时才需要它,因为它具有用于在列表中创建新节点的分配器?

c++ doubly-linked-list

2
推荐指数
1
解决办法
453
查看次数

超过递归深度 - Python双向链表

我正在创建一个实现为双向链表的 FIFO,但我不知道为什么我会收到递归错误。我已经发布了我的代码和我在下面收到的错误。任何帮助将非常感激!

"""DLList.py"""

class DLList(object):
    """A doubly-linked list

    attributes: nl = node list"""
    def __init__(self, nl=[]):
        self.nl = nl

    def __str__(self):
        return str(self.nl)

    def __len__(self):
        return len(self.nl)

    def append(self, node):
        if not isinstance(node, DLNode):
            raise TypeError
        try:
            node.pn = self.nl[-1]
            node.pn.nn = node
            self.nl.append(node)
        except:
            self.nl.append(node)

    def pop(self):
        rn = self.nl.pop(0)
        try:
            self.nl[0].pn = None
        except:
            pass
        return rn


class DLNode(object):
    """A node in a doubly-linked list.

    attributes: self.pn = previous node, self.data = data, self.nn = next node"""
    def …
Run Code Online (Sandbox Code Playgroud)

python fifo doubly-linked-list

1
推荐指数
1
解决办法
542
查看次数

插入双向链表的尾部

我第一次使用链表并且必须创建一个可以在双向链表的末尾插入节点的函数.到目前为止我有

void LinkedList::insertAtTail(const value_type& entry) {
    Node *newNode = new Node(entry, NULL, tail);
    tail->next = newNode;
    tail = newNode;
    ++node_count;
}
Run Code Online (Sandbox Code Playgroud)

Node类接受要存储的值,指向下一个指针的值,以及该顺序中前一个指针的值.每当我尝试在此处插入节点时,我都会收到错误消息,指出存在未处理的异常,并且写入位置0x00000008时存在访问冲突.

我不完全确定这里出了什么问题,但我认为它与根据错误消息解除引用空指针有关.我真的很感激帮助解决这个问题.

编辑:

我应该早点澄清,tail是一个指向列表中最后一个节点的指针.Tail-> next访问最后一个节点的下一个变量,该函数在函数运行之前指向NULL,但在执行之后应该指向创建的新节点.

c++ linked-list nodes doubly-linked-list

1
推荐指数
1
解决办法
6201
查看次数

Java的LinkedList与Scala的DoubleLinkedList

在Scala项目中,我需要一个简单的,可变的队列数据结构,我可以在一端附加项目并在另一端取出项目(即FIFO).现在,我不确定是否应该使用LinkedListJava中的普通旧版本或Scala版本DoubleLinkedList.这两者的相对优势是什么?我应该总是喜欢DoubleLinkedList,还是有充分的理由使用LinkedList?此外,还有其他值得考虑的选择吗?

queue scala linked-list fifo doubly-linked-list

1
推荐指数
1
解决办法
277
查看次数

我应该释放用于遍历链表的临时指针吗?

如果我有类似的东西:

function(DLL *dll) {
    DLL_Node *temp = dll->head

    // do stuff with temp like traverse list with temp=temp->next

    // say when done, temp = dll->tail, should I free(temp) or leave it alone?
}
Run Code Online (Sandbox Code Playgroud)

我对指针仍然不是很有经验,似乎我创建了一个额外的指针,但它只是指向列表中的一个位置,所以我不应该释放它,它会在函数结束时被清除?

顺便说一下,我定义DLLDLL_Node喜欢这样:

typedef struct dll_node {
    int data;
    struct dll_node *prev;
    struct dll_node *next;
} DLL_Node;

typedef struct DLL_ {
    int count;
    DLL_Node *head;
    DLL_Node *tail;
} DLL;
Run Code Online (Sandbox Code Playgroud)

我看到了程序valgrind的建议,用于测试内存泄漏,它表示肯定丢失和间接丢失都是0字节,但可能丢失的是2,064字节.不确定这究竟意味着什么,或者它是否相关.

c free pointers doubly-linked-list

1
推荐指数
1
解决办法
127
查看次数

通过改变连接从单链表转换为双重表

我有一个doubly linked list模拟a的行为singly linked list.所以我有一个左,右和信息,但左边总是空的.像这样的东西:

1 -> 2 -> 3 -> 4
Run Code Online (Sandbox Code Playgroud)

我想要的是在不重新创建节点的情况下将其重新变为双重,只需解析列表并重新构建连接即可.

1 -> <- 2 -><- 3 -><- 4
Run Code Online (Sandbox Code Playgroud)

我有

class Node {
    private int info;
    private Node left;
    private Node right;
Run Code Online (Sandbox Code Playgroud)

我的方法:

static Node toDoublyLinked(Node root) {
        if (root.getRight() != null) {
            root.getRight().setLeft(root);
            return toDoublyLinked(root.getRight());
        }
        return root;
    }
Run Code Online (Sandbox Code Playgroud)

哪个不起作用.它使我的程序抛出堆栈溢出,因为当我连接2到1时,1已经连接到2-> 3 - > 4所以它将开始复制那段列表而不是我想要的.

解决这个问题有什么办法吗?

void add(int info) {
        Node s = new Node();
        if (this.info == 0) {
            this.info = info;
        } …
Run Code Online (Sandbox Code Playgroud)

java linked-list doubly-linked-list

1
推荐指数
1
解决办法
50
查看次数

在链接列表中查找最小值

我试图在Vertices的链表中找到min.这就是我写的,但它是错误的.我没有收到错误,但我的程序不起作用,我认为这是错误的来源.我究竟做错了什么?

      Iterator itr = vertices.iterator();
      Vertex smallest= getVertex(s);
      Vertex temp;
      while (itr.hasNext()){
          smallest=(Vertex)itr.next();
           if(itr.hasNext() && vertices.size()> 1 ){//there are at least 2 vertices left
                temp = (Vertex)itr.next();
                if (temp.distance< smallest.distance){
                    smallest = temp;
                }
          }
     }
Run Code Online (Sandbox Code Playgroud)

java linked-list doubly-linked-list

1
推荐指数
1
解决办法
51
查看次数

使用 remove(Object) 从 LinkedList 中删除整数

我想使用 items 值从 Integer LinkedList 中删除一个项目。但相反,我得到了 ArrayIndexOutOfBoundException。

public static void main(String[] args) {

    List<Integer> list = new LinkedList<Integer>();
    list.add(10);
    list.add(20);
    list.add(5);

    list.remove(10);
    System.out.println("Size: " + list.size());

    for (Integer integer : list) {
        System.out.println(integer);
    }
}
Run Code Online (Sandbox Code Playgroud)

我期望的输出是一个只有 20 和 5 2 个元素的列表。但我得到以下异常:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 3
at java.base/java.util.LinkedList.checkElementIndex(LinkedList.java:559)
at java.base/java.util.LinkedList.remove(LinkedList.java:529)
at Collections.LinkedListTest.main(LinkedListTest.java:15)
Run Code Online (Sandbox Code Playgroud)

LinkedList 将我传递的数字视为索引而不是值。那么如何在不使用其索引号的情况下将项目 10 作为值删除。

java collections linked-list doubly-linked-list

1
推荐指数
2
解决办法
604
查看次数