Ten years of JAVA moving bricks - data structure linear structure

linear structure

A linear list is a data structure used to store an ordered set of data elements. It is characterized by a one-to-one relationship between data elements, and each element has only one predecessor and one successor (except for the first and last elements). Linear lists can be implemented using arrays or linked lists.

Data refers to the symbolic representation of things , which can be numbers, characters, images, sounds, etc. Data can be organized and stored in linear tables for manipulation and processing. Linear tables provide a simple and effective way to manage and access data, making data storage and retrieval more convenient and efficient.

Linear tables can be implemented in a variety of ways, the two most common of which are using arrays (sequential storage) and using linked lists (linked storage).

**1. Sequential storage: **The sequential storage of linear tables is realized through arrays. Arrays store elements sequentially in memory, and each element occupies a fixed-size memory space. The advantage of sequential storage is that the elements can be directly accessed through the index, and the access speed is fast. However, insertion and deletion operations need to move elements, which is less efficient.

2. Linked storage : The linked storage of the linear table is realized through the linked list. A linked list consists of nodes (Nodes), each node contains data and a pointer (or reference) to the next node. Nodes can be stored non-contiguously in memory, and they are connected by pointers. The advantage of chained storage is that insertion and deletion operations only need to modify the pointer pointing, which is more efficient. However, chained storage does not support random access, and needs to traverse from the head node to the target position to access elements.

Basic operations for sequentially storing arrays:

  1. Insertion operation: To insert a new element in the sequential storage structure, all elements after the insertion position need to be shifted back by one bit, and then the new element is placed in the specified position.

  2. Deletion operation: To delete an element from the sequential storage structure, it is necessary to move all elements after the deletion position forward by one bit, and overwrite the deleted element.

  3. Access operation: access the elements in the sequential storage structure through the index, and directly access the element at the corresponding position through the index value.

  4. Search operation: To search for a specified element in a sequential storage structure, it is necessary to traverse the entire storage structure and compare the values ​​of elements one by one until the target element is found or the traversal ends.

  5. Update operation: Locate an element in the sequential storage structure through the index, and then modify the value of the element.

• Common operations on linear unidirectional linked lists include:

  1. Insert operation:

    • Head Insertion: Create a new node, set its data value, and make its pointer point to the current first node. Update the head pointer to point to the new node.
    • Tail Insert: Create a new node, set its data value, and make its pointer point to null. Update the pointer of the last node to point to the new node.
    • Insert at specified position: Locate to the specified position, create a new node, set its data value, and make its pointer point to the next node at this position. Update the previous node's pointer to point to the new node.
  2. Delete operation:

    • Delete the first node: update the head pointer to point to the second node, and remove the first node from the linked list.
    • Delete the last node: Traverse the linked list to find the penultimate node, and set its pointer to null.
    • Delete the specified node: Locate the node to be deleted, update the pointer of the previous node to skip the node to be deleted, and remove the node from the linked list.
  3. Traverse operation:

    • Starting from the head node, each node is traversed in turn by following the pointer until the end of the linked list is reached. Perform desired operations on each node.
  4. Search operation:

    • Starting from the head node, traverse the linked list and compare each node's data value with the target value. Returns the found node or indicates that the element was not found.

• Linear single circular linked list operation:

Insert operation:
head insert data:

  1. Create a new node and set its data.
  2. If the list is empty (head is empty), set the new node's next pointer to itself and update the head pointer to the new node.
  3. If the list is not empty, set the new node's next pointer to the current head node.
  4. The list is traversed to find the last node (the node whose next pointer points to the current head node).
  5. Update the next pointer of the last node to point to the new head node.
  6. Update the head pointer to point to the new node.
    Insert data in the middle or at the end:
  7. Create a new node with the data to be inserted.
  8. Traverse the linked list until you reach the node in the desired position. Also keep track of the previous node.
  9. Sets the next pointer of the new node to the node next to the previous node.
  10. Sets the next pointer of the previous node to the new node.
  11. If the desired location is at the end of the linked list (i.e. the last node), update the new node's next pointer to point to the head node.

delete operation

  1. Find the node to delete by traversing the linked list and keeping track of the previous node.
  2. Update the next pointer of the previous node, skip the node to be deleted, and point to the next node.
  3. If the node to be deleted is the head node, update the head pointer to point to the next node.
  4. If the node to be removed is the last node, update the next pointer of the previous node to point to the head node.
    Traversal Operation/Search Operation
    To traverse a singly circular linked list, you start at the head node and continue traversing until you reach the head node again. Since it's a circular linked list, you need to
    make sure you don't get stuck in an infinite loop by checking to see if the head node is reached again.

• Doubly linked list operation:
insert operation

  1. Create a new node and store the value to be inserted in it.
  2. If the linked list is empty, set the new node as the head node of the linked list.
  3. If the position to be inserted is the head of the linked list, insert the new node into the head of the linked list, and set the original head node as the successor node of the new node. The successor node of the new node points to the original head node, and the predecessor node of the original head node points to the new node.
  4. If the position to be inserted is not the head of the linked list, it is necessary to traverse the linked list to find the previous node of the position to be inserted.
  5. Sets the new node's successor to the previous node's successor, and sets the new node's predecessor to the previous node.
  6. Sets the successor of the previous node to the new node, and sets the new node to the predecessor of the successor.

delete operation

  1. If the linked list is empty, it cannot be deleted.
  2. If the position to be deleted is the head of the linked list, set the successor node of the head node to the new head node, and set the predecessor node of the new head node to None.
  3. If the position to be deleted is not the head of the linked list, it is necessary to traverse the linked list to find the node at the position to be deleted.
  4. Sets the successor node of the predecessor node of the node to be deleted to the successor node of the node to be deleted.
  5. If the successor node of the node to be deleted is not empty, set the predecessor node of the node to be deleted's successor node to the predecessor node of the node to be deleted

Guess you like

Origin blog.csdn.net/weixin_43485737/article/details/132473378