Li Haha's data structure and algorithm notes [part 2: linear table]

Li Haha's data structure and algorithm notes [part 2: linear table]

Feel the existence of linear tables

Just line up and remember the person in front of you.
Linear table definition:
a finite sequence consisting of zero or more data elements.
A few highlights:

  • It's a sequence, first come first.
  • If there are multiple elements, the first has no predecessor, the last has no successor, and the other elements have only one predecessor and successor.
  • The linear table is limited.

Abstract data type of linear table (ADT)

Abstract data type: abstract date type
bundles data type and related operations together.
The abstract data type definition of the linear table:

  • ADT Linear Table List

  • The data object collection of DATE linear table is {a1 a2 a3 …… an}, and each element type is DateType
  • Operation
    InitList(*L): Initialize the operation and create an empty linear list L
    ListEmpty(L): Determine whether the list is empty, return true if it is empty, otherwise return false
    ClearList(*L): Clear the linear list
    GetElem(L, i, e): return the i-th position element in the linear table L to an e
    LocateElem (L, e): find the element equal to the given value e in the linear table L, and return the sequence number in the table if successful, otherwise return 0 means failure.
    ListInsert(*L,i,e): Insert an element
    at the i-th position in the linear table L. ListDelete(*L,i,*e): Delete the element at the i-th position in the linear table L and use e Return its value
    ListLength (L): return the number of elements in the linear table L
  • endADT

The above sub-functions can be written by users.

Sequential storage structure of linear table

classification:

  • Sequential storage structure: a segment of storage units with consecutive addresses store data elements in the linear table in sequence
  • Chain storage structure: simultaneously store data element information and the storage address of subsequent elements

There is a problem with the sequential storage structure: when an element needs to be added or deleted, the time required to process is complicated is O(n), which takes up a lot of resources, so a chain storage structure is created.
Chain storage structure:
data domain: the domain that stores the information of the data element.
Pointer domain: the domain that stores the immediate successor position. The storage information is called a pointer or chain
storage image: a data element composed of these two parts of information, also called a node

Single list

The storage location of the first node is called the head pointer, and the last node pointer is NULL.
The similarities and differences between the head pointer and the head node: the
head node data field generally does not store any information

Head pointer:

  1. The head pointer is a pointer to the first node of the linked list, if the linked list has a head node, it is a pointer to the head node
  2. The head pointer has an identification function, and the commonly used head pointer is prefixed with the name of the linked list (pointer variable name)
  3. Regardless of whether the linked list is empty, the head pointer is not empty
  4. The head pointer is a necessary element of the linked list

Head node:

  1. Established for operation uniformity and convenience, it is placed before the node of the first element. The data field is generally meaningless, but it can also be used to store the length of the linked list.
  2. With the head node, the operations of inserting and deleting the first node before the first element node are unified with those of other nodes.
  3. The head node is not an essential element of the linked list

Reading a singly linked list starts from the beginning until the i-th element is found. In the worst case, the time complexity is O(n).
Core idea: work pointers move backward

Entire table creation of singly linked list

What is the whole table creation?
The head insertion method creates a singly linked list:
put the new element at the first position after the head of the table, first
let the next of the new node point to the head node,
and then let the next of the table head point to the new node.
Key code:

p->next = (*L)->next;
(*L)->next = p;

Where p is a new element created each time

Create a singly linked list with tail interpolation:
put the new element at the end of the list.
Key code:

r->next = p;
r = p;

Here p is also a new element created each time.

Delete the entire table of a singly linked list

Idea:
Declare the nodes p and q to
assign the first node to p, and the next node to q
. The operation
code for releasing p and assigning q to p is as follows:

q = p->next;
delete(p);
p = q;

The q here is used to store the next node, otherwise the pointer will also be cleared when deleting (p).

Static list

Definition: A linked list despised by an array is called a static linked list.
This description method is called a cursor implementation method: (The E in the figure below should be 0)
Insert picture description here
As can be seen from the example:
The cursor stored by the 0 subscript is the last data cursor, which is actually the free subscript position
The last element cursor is the subscript of the beginning of the data. The
rest of the cursors are all subscripts plus one
specific usage:
use the cursor to access the subscript, which is the meaning of the third sentence above.

Guess you like

Origin blog.csdn.net/qq_41883714/article/details/109505907