Differences between linear tables, linear tables, and linked lists
Introduction
A logical structure,Same Data TypeOf n data elementsFinite Sequence, Except the first element, each elementYes and onlyA direct precursor. Except for the last element, each element has only one direct successor.
Features of a linear table:
(1) Limited number of elements (2) Logically, elements are sequential.
(3) identical data types (4) only discuss the logical relationship between elements
Note: Linear tables are logical structures and sequential tables and linked lists are storage structures.
1. Sequential Storage
An ordered table is implemented using arrays. It is a group of address-contiguous storage units. The array size can be specified in two ways: static allocation and dynamic expansion.
Note: The linear table starts from 1, and the array starts from 0.
Advantages: Random Access, search for O (1) time, high storage density, logically adjacent elements, physically adjacent;
Disadvantage: a large number of elements need to be moved during insertion and deletion.
Operations related to sequence tables are related to arrays, which are generally moving array elements.
Here we will talk about the boundary condition during insertion and deletion. First, the linear table starts from 1 and the array starts from 0. The simple file description is not straightforward enough. Let's look at the illustration.
Insert: For a linear table, the minimum insert position is 1 and the maximum insert position is 8 (= 7 + 1 ), so 1 <= index <= (7 + 1); pay attention when moving array elements, for (int I = count; I> = index; I --) {items [I] = items [I-1];}
When deleting: you can only find a node to delete between the blue blocks, that is, 1 <= index <= 7. Move element, for (I = index; I <count; I ++) {items [I-1] = items [I];}
2. chained Storage
The definition of a linked list is recursive. It can be null or reference to another node. This node contains references of the next node or linked list.
Compared with sequential storage, the storage space is not consecutive. You do not need to move a large number of elements during insertion or deletion. You only need to modify the pointer. However, to find an element, you can only traverse the entire linked list from the beginning.
In Java, nested classes are used to define abstract data types of nodes:
Private class Node {// nested class T item of the linked list Node; // Node content Node next; // successor Node}
2.1 single-chain table
Any storage unit is used to store data elements in a linear table. The node type is as follows.
A single-chain table can be divided into two types: The leading node and the non-leading node. No matter whether there are any header nodes, the header Pointer Points to the first node of the linked list (with the header node points to the header node ).
Header node: the numeric field does not contain any information. The pointer field of the header node points to the first element of the linked list.
Advantages of leading nodes:
(1) The operations on the first node of the linked list are the same as those on other nodes.
(2) No matter whether the linked list is empty or not, the header Pointer Points to the header node (not empty). The processing of empty tables and non-empty tables is the same.
(I have not used the header node here)
Note: The trouble with the linked list is that the pointer is modified during insertion and deletion to ensure continuous link. Generally, the chain is broken first.
Basic operations
1. header Insertion Method
Insert a new node to the table header of the current linked list (after the header node). The insertion sequence is the opposite to the sequence in the linked list. The key point is to remember the old table header, generate a new header before the old header,
Core code: public void headInsert (T item) {Node old = first; first = new Node (); first. item = item; first. next = old; count ++ ;}
2. Tail insertion
Add a tail pointer to insert the new node to the end of the linked list. The insertion sequence is the same as that of the linked list,
Core code: public void tailInsert (T item) {Node old = last; last = new Node (); last. item = item; last. next = null; if (isEmpty () {first = last;} else {old. next = last;} count ++ ;}
Key Points of node insertion and deletion are:Disconnect before connectionThe key is not to break the chain. Taking insertion as an example (inserting s between p and q), the first disconnection means that p-> q is broken first and then s-> q is changed, then connect p and s.
3. Insert a node
If the node to be inserted is s, a post-insertion method is generally used. That is, the frontend node of the inserted node is first found, and then inserted. the time complexity is O (n ).
Core code: p = getNodeByIndex (I-1); s. next = p. next; p. next = s;
Another method is to insert the data directly to the end of the position (the forward insertion method), then exchange the values of the two nodes, and insert the nodes to the specified position. The time complexity is O (1 ):
Core code: s. next = p. next; p. next = s; temp = p. item; // exchange content p. item = s. item; s. item = temp;
4. delete a node
If the node to be deleted is q, you must first find the precursor node and modify the pointer field. The time complexity is O (n ).
Core code: P = getNodeByIndex (I-1); q = p. next; p. next = q. next; q = null;
Deleting a node can also directly delete its successor node, and then assign the content of the successor node to itself. The time complexity is O (1 ):
Core code: q = p. next; p. item = p. next. item; p. next = q. next; q = null;
2.2 double-stranded table
The disadvantage of a single-chain table node is that there is only one slave node, and the access to the front-end node can only be traversed from the beginning (such as insertion or deletion), the time complexity is O (n ). A dual-Link Table is used to add a node pointing to the precursor. The node type is as follows:
Private class Node {// nested class T item of the linked list Node; // Node content: Node prior, next; // precursor Node and successor Node}
The search for a double-linked table is the same as that for a single-linked table. The structure of a double-linked table is also divided into header insertion and tail insertion. The only difference from a single-linked table is to modify the prefix pointer prior. For details, see the source code. The insert and delete operations are different because two pointers need to be modified. If the nodes to be operated are given, the time complexity of the insert and delete operations is O (1 ).
Note: The insert/delete operation also breaks down and then connects.
1. Insert a node
After the p node is inserted to the s node, the p node is disconnected and then connected. The p and the chain of the original successor node are broken so that the successor node is only related to the s node:
① S. next = p. next; // the successor of p is broken first. ② p. next. prior = s; // The precursor ③ s after p is disconnected. prior = p; // Let s's forward to p ④ p. next = s; // p points to s and reconnects the chain. This step must be after ① ②.
2. delete a node
Delete the successor node q of node p, which is also disconnected and connected first, and transfer the relationship between q and its successor node to p:
① P. next = q. next; // The q successor is disconnected first. ② q. next. prior = p; // Delete the precursor node p of the q node after the q node is disconnected, and transfer the relationship between p and the deprecated node to q: ① q = p. prior. next; // change the successor of the p precursor node to q ② q. prior = p. prior; // change the q precursor node to the p precursor node.
2.3 circular linked list
1. Circular Single-chain table
The difference with a single-chain table is that the pointer to the last node in the table is not null, but instead is directed to the header node (the first node) to form a ring for the entire linked list. Determines whether the cyclic single-chain table is empty and whether it is equal to the header pointer.
A single example table with only one tail pointer can be easily operated on the header and the end of the table, because the successor of the tail pointer is the header pointer O (1 ).
2. Cyclic double-stranded table
The difference between a table and a double-stranded table is that the prior pointer of the header node points to the end node, and the next pointer of the End Node points to the header node.
2.4 static linked list
Static linked lists use arrays to describe the chain storage structure of linear tables. nodes also have data fields and pointer fields. Here the pointer is the relative address of the node (array subscript ), you also need to allocate a continuous memory space in advance.
Feature: insert/delete is the same as the Dynamic Linked List, and ends with next =-1.
2.5 Comparison Between Sequence tables and linked lists
1. ordered tables can be accessed sequentially or randomly. linked lists can only be accessed sequentially.
2. the sequence table is physically adjacent to each other logically. Instead, the linked list uses pointers to describe the relationship between elements.
3. insert and delete sequence tables to move a large number of elements. You only need to modify the pointer to the linked list.