Learn basic data structures in ten days - Day 4 (Linked List)

Insert image description here

Basic concepts of linked lists

A linked list is a linear data structure. Unlike an array, the elements (nodes) of a linked list are connected to each other through pointers. Linked lists have the following basic concepts:

  • Node : Each data item in the linked list is called a node. Each node contains data and a pointer to the next node.

  • Head node : The first node of the linked list is called the head node, which is usually used to represent the starting position of the entire linked list.

  • Tail node : The last node of the linked list is called the tail node, and its pointer usually points to null.

The difference between singly linked list and doubly linked list

Linked lists can be divided into two main types: singly linked lists and doubly linked lists .

  • Singly linked list : Each node contains only a pointer to the next node. A singly linked list has a small memory overhead, but cannot be easily traversed in reverse.

  • Doubly linked list : Each node contains pointers to the next node and the previous node. Doubly linked lists allow bidirectional traversal, but the memory overhead is large.

Common operations on linked lists

Linked lists support the following common operations:

  1. Inserting a node : Inserting a new node into a linked list usually requires updating the pointers of adjacent nodes.

  2. Deleting a node : Deleting a node from a linked list usually requires updating the pointers of adjacent nodes.

  3. Reverse linked list : Reverse the order of nodes in the linked list.

  4. Get the length of the linked list : Get the number of nodes in the linked list.

The following is a simple C++ example to create a singly linked list, insert and delete nodes, and obtain the length of the linked list:

#include <iostream>

// 链表节点定义
struct Node {
    
    
    int data;     // 节点数据
    Node* next;   // 指向下一个节点的指针
};

int main() {
    
    
    // 创建链表头节点
    Node* head = nullptr;
    
    // 插入节点
    Node* newNode1 = new Node;
    newNode1->data = 1;
    newNode1->next = nullptr;
    head = newNode1;

    Node* newNode2 = new Node;
    newNode2->data = 2;
    newNode2->next = nullptr;
    newNode1->next = newNode2;

    // 删除节点
    delete newNode1;
    head = newNode2;

    // 获取链表长度
    int length = 0;
    Node* current = head;
    while (current != nullptr) {
    
    
        length++;
        current = current->next;
    }

    std::cout << "链表长度:" << length << std::endl;

    return 0;
}

Practice questions:

  1. What are the main differences between singly linked lists and doubly linked lists? Under what circumstances would you choose to use a singly linked list or a doubly linked list?
  2. Describe a situation where linked lists are more suitable than arrays for storing and managing data.
  3. How to insert a new node in a linked list? What is the time complexity of this operation?
  4. How to delete a node in a linked list? What is the time complexity of this operation?

What are the main differences between singly linked lists and doubly linked lists? Under what circumstances would you choose to use a singly linked list or a doubly linked list?

  • Singly linked list : Each node in a singly linked list contains only one pointer, usually a pointer to the next node. This structure has less memory overhead, but can only be traversed in one direction. Singly linked lists are suitable for scenarios where one-way access to data is required when memory overhead is limited.

  • Doubly linked list : Each node in a doubly linked list contains two pointers, one pointing to the next node and the other pointing to the previous node. This allows bidirectional traversal, but at the cost of larger memory overhead. Doubly linked lists are suitable for situations where bidirectional traversal or frequent insertion and deletion of nodes in the middle of the linked list is required.

The choice of using a singly linked list or a doubly linked list depends on the requirements. If only one-way access is required or memory is limited, a singly linked list may be more appropriate. If bidirectional traversal or frequent insertion and deletion of nodes is required, a doubly linked list may be more suitable.

Describe a situation where linked lists are more suitable than arrays for storing and managing data.

Linked lists are more suitable than arrays for the following situations:

  • Dynamic size : When the size of the data set changes at runtime, a linked list is more suitable because it can allocate and free memory dynamically, while the size of an array is usually fixed.

  • Frequent insertion and deletion : If frequent insertion and deletion of data items is required, a linked list is more efficient than an array because the time complexity of insertion and deletion operations is O(1), while the same operation in an array usually requires O(n).

  • No need for random access : If you only need to access data sequentially without random access (using indexes), linked lists are a good choice.

How to insert a new node in a linked list? What is the time complexity of this operation?

The steps to insert a new node are as follows:

  • Create a new node, set its data and pointers.
  • Update the pointer of the new node so that it points to the node at the appropriate location in the original linked list.
  • Update the pointer of the adjacent node in the original linked list to point to the new node.

The time complexity of this operation depends on the insertion position. If the insertion position is known, the time complexity is O(1) because only the pointer needs to be modified. If you need to insert in the middle of the linked list and need to traverse to find the insertion position, the time complexity is O(n) , where n is the length of the linked list.

How to delete a node in a linked list? What is the time complexity of this operation?

The steps to delete a node in a linked list are as follows:

  • Find the node to delete.
  • Update the pointers of adjacent nodes to skip the node to be deleted.
  • Release the memory of the node to be deleted.

The time complexity of this operation depends on the time it takes to find the node to be deleted. If the deletion position is known, the time complexity is O(1) because only the pointer needs to be modified. If you need to traverse the linked list to find the node to be deleted, the time complexity is O(n) , where n is the length of the linked list.

Note, always make sure to free the node's memory before deleting it to avoid memory leaks.

Guess you like

Origin blog.csdn.net/m0_53918860/article/details/133554321