简体   繁体   English

"单链表时间"

[英]Singly Linked List Time

Given a Singly linked list that contains a pointer to the head, and one to the tail.给定一个单链表,其中包含一个指向头部的指针和一个指向尾部的指针。 Which would be the most time consuming?哪个最耗时?

  • Inserting node at beginning在开头插入节点

  • Inserting node at the end在末尾插入节点

  • Deleting node at the beginning在开头删除节点

  • Deleting node at the end最后删除节点

I think inserting node at the end because to do this we have to loop through the whole linked list, then insert the data of the node, then link the node correspondingly.我认为在最后插入节点,因为要这样做,我们必须遍历整个链表,然后插入节点的数据,然后相应地链接节点。

However with the research I've done deleting node at the end also seems to be a valid options, which would you say takes the most amount of time?但是,通过我所做的研究,最后删除节点似乎也是一个有效的选择,你会说哪个需要最多的时间?

最后删除节点:需要遍历所有链表,因为它需要更新倒数第二个节点以将下一个指针更新为NULL。

Let's look at what it takes for each让我们看看每个人需要什么

Inserting node at beginning : Constant time在开始处插入节点:恒定时间

inserted.next = head
head = inserted

Inserting node at the end : Constant time最后插入节点:恒定时间

tail.next = inserted
tail = inserted

Deleting node at the beginning : Constant time开始删除节点:恒定时间

old_head = head
head = head.next
delete old_head

Deleting node at the end : linear time <-- most expensive最后删除节点:线性时间<--最昂贵

old_tail = tail
new_tail = head
while new_tail.next is not None:
    new_tail = new_tail.next
tail = new_tail
delete old_tail

Analysing the complexity here.分析这里的复杂性。

Assuming the list structure is as below假设列表结构如下

node1-> node2-> node3-> node4-> node5 node1->node2->node3->node4->node5

head--------------------------------------------tail头-------------------------------------尾巴

Inserting node at the end : tail-> next = newNode;在末尾插入节点:tail-> next = newNode; O(1) O(1)

Deleting node ate the end : Don't have the prev pointer for tail (as it is singly linked list) so have to traverse the entire list to find secondlast node.删除节点结束:没有尾部的 prev 指针(因为它是单链表)所以必须遍历整个列表以找到第二个节点。 O(n)在)

secondlast_node->next = Null;
free(tail);
tail = secondlast_node;

(The answer may change if you managed to construct the list differently) (如果您设法以不同的方式构建列表,答案可能会改变)

If you have constructed the linked list as in the below example如果您已经按照以下示例构建了链表

node1<-node2<-node3<-node4<-node5节点1<-节点2<-节点3<-节点4<-节点5

head---------------------------------------------------tail头 - - - - - - - - - - - - - - - - - - - - - - - - - - 尾巴

Deleting node at the beginning becomes the most expensive operation一开始删除节点成为最昂贵的操作

but in both the cases inserting node at the end is O(1) complexity但在这两种情况下,最后插入节点都是 O(1) 复杂度

取决于你的指针在哪里。如果它在开头,它将在最后一个节点插入和删除。

if you have two pointers which pointing to head and tail 1. inserting : both takes O(1) as just modifying head and tail pointers.如果您有两个指向头和尾的指针 1. 插入:都将 O(1) 视为只是修改头和尾指针。 2. Deleting: head side deleting takes O(1) time , however , tail needs previous pointer which requires O(n) time complexity. 2. 删除:头侧删除需要 O(1) 时间,但是尾需要前一个指针,需要 O(n) 时间复杂度。

Assume: it is Single Linked List.假设:它是单链表。 for DLL all takes O(1)对于 DLL 都需要 O(1)

Case study: A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations.案例研究:链表是一种线性数据结构,其中元素不存储在连续的内存位置。 The elements in a linked list are linked using pointers as shown in the below image: Each element in the link is called a node which is composed of two parts: a.链表中的元素使用指针链接,如下图所示: 链接中的每个元素称为一个节点,它由两部分组成: a. Data stored by the node b.节点存储的数据 b. Link pointed to the next node in the linked list In this assessment 2, you are required to develop a Linked List in visual studio called LinkedList.cpp which uses a Linked List to store the browsing history in the web browser following aspects using C++:链接指向链表中的下一个节点 在此评估 2 中,您需要在 Visual Studio 中开发一个名为 LinkedList.cpp 的链表,它使用链表将浏览历史存储在 Web 浏览器中,使用 C++ 的以下方面:

  1. Node in the linked list with appropriate data types: each node stores in the URL (web address), its current position (index) in the Linked List and link to the next node in the Linked List.链表中具有适当数据类型的节点:每个节点存储在 URL(网址)中,它在链表中的当前位置(索引)以及链接到链表中的下一个节点。
  2. Insertion − Adds a node at the beginning of the lined list: method signature is void insertFirst(String newURL)插入 - 在行列列表的开头添加一个节点:方法签名是 void insertFirst(String newURL)
  3. Deletion − Deletes a node at the beginning of the linked list: method signature is void deleteFirst()删除 - 删除链表开头的节点:方法签名是 void deleteFirst()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM