The chain-storage structure of the linear table

Source: Internet
Author: User
1, Problem Introduction

The reason for developing an array class template is that when you create a linear table based on a sequential storage structure, it is found that such a linear table might be misused because the array access operator is overloaded with the array, but the linear table and arrays are very different, thus triggering new requirements: developing an array class to replace the C + + native array class. Because the native array class also has the very big flaw, the use is inconvenient.

Another disadvantage of linear tables based on sequential storage structures: when inserting or deleting an element, it involves the movement of a large number of data elements, which has a significant impact on efficiency.

A new requirement: a data structure that does not require a large number of moving data elements when inserting or deleting elements, that is, a linear table based on a chain-storage structure

2, the definition of chain-type structure

In order to represent the logical relationship of each data element to its immediate successor, the data element needs to store its direct successor information in addition to the information stored in it itself.

\ (a_i\) and \ (a_{i+1}\) are two contiguous data elements in a linear table, with no neighboring relationships in physical memory.

A data element consists of two parts:\ (a_i\) is the data information of the data element itself, there is an address information, the address is the direct successor of the element \ (i\) , that is, the address information of the element in memory of the first \ (i+1\) . In other words, if you find the element \ (i\) , you can get not only the value of the i\ element itself, but also the position of the first \ (i+1\) element in memory.

3. Chain Storage Logic Structure

In a linear table based on a chained storage structure, each node contains a data field and a pointer field.

    • Data fields: storing data elements themselves
    • Pointer field: The address where adjacent nodes are stored

The two types of linear table names are uniform:

    • Sequential tables: Linear tables based on sequential storage structures
    • Linked list: A linear table based on a chain-store structure
      • Single linked list: Each node contains only the direct successor address information
      • Circular chain list: The direct successor of the last node of a single linked list is the first node.
      • Doubly linked list: nodes in a single linked list contain direct precursors and subsequent address information
      • Two-way loop linked list ...
4. Basic concepts in the list
    • Head node: A secondary node in a linked list that contains a pointer to the first Data element and contains no data information to simplify the code
    • Data node: a node representing data elements in a linked list, expressed as: (data element, address)
    • Tail node: The last data node in a linked list. Separate release reason: The tail node contains the address information directly determines the nature of the list, the address information is empty, is a single linked list, address is the No. 0 element of the address information, is the circular chain list; address information is a random value, is an illegal list
5. Single linked list 5.1 node definition in single linked list
// 用struct定义类,默认属性是public// T是泛指类型,链表可以存储各种类型的数据struct Node : public Object{    T value;    Node* next; // 指向后继结点的指针}
5.2 Internal structure of single-linked list

The meaning of the head node in a single linked list is that the secondary data element is positioned to facilitate insertion and deletion, so the head node does not store the actual data elements.

5.3 Inserting a data element at the target location
    1. Start from the beginning and current navigate to the target position with the pointer.

    2. Request a new node node from the heap space

    3. Perform actions

      node->value = e;node->next = current->next;current->next - node;
5.4 Deleting a data element at the target location
    1. Start from the beginning, previous navigate to the previous address of the target location by the pointer

    2. Use toDel the pointer to point to the node you want to delete

    3. To perform the operation:

      toDel = previous->next;previous->next = toDel->next;delete toDel;
6. Summary

Data elements in a linked list have no adjacent relationships in physical memory

Nodes in a linked list contain data fields and pointer fields

Head node for auxiliary data element positioning for easy insert and delete operations

Insert and delete operations need to ensure the integrity of the linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.