Class notes: sequence list, singly linked list

The definition of a linear list (Linear List) is a finite sequence of zero or more data elements of the same type. The number of data elements is defined as the length of the linear table. When the length is equal to zero, it is called an empty table. A non-empty table is usually written as L = (a 1, a 2, ..., an), where ai (1 ≤ i ≤ n) is called a data element, and the subscript i indicates the element In the position or sequence number in the linear table, the element ai is said to be at the i-th position in the table, or ai is the i-th element in the table.
Sequential storage structure of the linear table-Features of the sequential table: The sequential storage of the linear table refers to the sequential storage of each element in the linear table with a group of consecutive address storage units.
Function: The data elements that are adjacent in the logical structure in the linear table are stored in the adjacent physical storage unit, that is, the logically adjacent relationship between the data elements is reflected by the adjacent relationship of the physical storage of the data element.
Implementation of sequential storage: One-dimensional arrays store data in sequential tables.
Features of sequence table The
advantages of linear table sequence representation are: (1) There is no need to add additional storage space to represent the logical relationship between nodes (because logically adjacent elements are stored in adjacent physical locations); ( 2) Any element in the table can be easily accessed randomly.
Disadvantages of linear table order representation: (1) Insert or delete operations are inconvenient. Except for the position of the tail of the table, inserting or deleting operations at other positions of the table must move a large number of nodes, which is less efficient; (2 ) Since the sequence table requires continuous storage space, storage allocation can only be done in advance by static allocation. Therefore, when the table length changes greatly, it is difficult to determine the appropriate storage size.
Chained storage structure and implementation of
linear table The characteristics of chained storage allocation: dynamically apply for storage space according to the length of the linear table to solve the problem of difficulty in determining the storage space in sequential storage.
The realization of chain storage structure: single linked list, doubly linked list, circular linked list, etc.
The characteristics of pointer variables:
the three elements of the variable: name, memory address, value.
The lvalue and rvalue of the variable. The lvalue
refers to the memory address of the variable, rvalue: value.
The rvalue of the pointer variable itself is an lvalue.
Single linked list
head node with head node: If the linked list has a head node, the first node in the chain structure is called the head node: its data field can store some additional information, such as the length of the linked list; its pointer field points to the linked list The first node.
Construction of singly linked list-head interpolation

template <class T>  
LinkList<T>:: LinkList(T a[ ], int n) 
{
    first=new Node<T>; 
    first->next=NULL;   
    Node<T> *s;   
    for(int i=0;i<n;i++){
        s=new Node<T>;           
        s->data=a[i];       
        s->next=first->next;          
        first->next=s;
    }
}

Construction of single linked list-tail interpolation

template <class T>  
LinkList<T>:: LinkList(T a[ ], int n) {
    Node<T> *r,*s; 
    first=new Node<T>; 
    r=first;              
    for(int i=0;i<n;i++){
        s=new Node<T>;
        s->data=a[i];        
        r->next=s; r=s; 
    }
    r->next=NULL; 
}

Construction of a singly linked list without a
head node

{    
    first=NULL;
    for(int i=0;i<n;i++)
    {
        s=new node<T>; 
        s->data=a[i];         
        s->next=first;         
        first=s;       
    }
}

Tail interpolation:

node<T> *r;     
head=NULL;    
if(n<=0)return;    
s=new node<T>;    
s->data=a[0];    
s->next=head;    
head=s;       
r=head;
for(int i=1;i<n;i++) {          
    s=new node<T>;         
    s->data=a[i];         
    r->next=s;         
    r=s;      
}

Destructor

template <class T>
LinkList<T>::~LinkList()
{
   Node<T> *q;   
   while(first) 
   {
       q=first->next;       
       delete first;       
       first=q;   
   }
}

If the operation of the linear table is mainly for searching, and when insertion and deletion are rarely done, the sequential table should be used as the storage structure.
For linear tables that are frequently inserted and deleted, a linked list should be used as the storage structure.
When the length of the linear table does not change much and it is easy to determine its size in advance, in order to save storage space, the sequence table should be used as the storage structure.

Published 48 original articles · Like 25 · Visit 2453

Guess you like

Origin blog.csdn.net/qq_43628959/article/details/101177664