The basic operation of a single linked list (b)

First, delete:

a   b   c  

 

 

To delete a single list element at the specified position, the same as insert elements, should first find the position of the predecessor node; b delete elements in a single linked list, its precursor should first find the node a ,. In order to achieve a single element in a linked list, b, and changes the logical relation between C, the target domain is only necessary to junction point (a); p is assumed that a pointer to the node, then the pointer is modified statement: p -> next = p-> next-> next; but when deleting the node b, in addition to modifying a node pointer outside, but also to free up space occupied by the node b, the pointer before modifying it, should be introduced into another pointer q, temporarily holds the address of the node b for future release.

Algorithm steps:

1, by a node to find a pointer pointing to the node p

2, the address to be deleted is temporarily stored in the node q and b, in preparation for the release of

3, the node pointer field points * p direct successor of the node b

4, the release space of the node b

ListDelete the Status (LinkList L &, int i) 
{ 
     // the First Node L single list, delete the i-th element 
    P = L; 
    J = 0 ;  
     the while ((p-> Next) && (J <I- . 1 )) 
   { 
        P = p-> Next;
         ++ J; // Find the first i-1 nodes, p points to the node 
       IF ((p-> Next) || (J> I-! . 1 )) return ERROR; // if i> n or i <1, delete location unreasonable 
       Q = p-> Next; // temporarily holds the address of the node to be deleted to prepare for release of 
      p-> = Q- Next> Next; // remove precursor node changes the node pointer field 
     delete Q; // release space deleted node
     return  ok;
    }
}

Second, create a list:

Chain sequence and different tables, it is a dynamic linked list structure. The entire available storage space may be a common access to a plurality of lists, each list occupies the space need not be preallocated, but by the overall system demand distribution in time; different, linked list creation method according to the insertion position of the node can be divided into method forward runs and after interpolation;

a, forward runs to create a single list of law:

Algorithm steps:

1. Create an empty list only a head node

2, to be created according to the number of elements included in the list n, n times the following cycle:

Generating a new node ----- * p

----- input node element value assigned to a new data field * p

After the new node ----- * p inserted into the head node

void CreateList_H (LinkList L &, int n) 
{ 
    // Inverse-bit input values of n elements, with the establishment of a single list header node L 
   L = new new LNode; 
   L -> Next = NULL; // first create a First Node point empty list 
   for (I = 0 ; I <n-; ++ I) 
   { 
       P = new new LNode; // create a new node P * 
       CIN >> p-> Data; // input values assigned to a new node element * p data field 
       p-> Next = L-> Next; 
        L -> P = Next; // the new node inserted into the head node * p after 
    } 
}

b, after interpolation to create a single list:

Algorithm steps:

1. Create an empty list only a head node

2, the tail pointer r initialized to point to the head node

3, according to the number of elements including the created list of n, n times the following cycle:

Generating a new node ----- * p;

----- input element assigned to the new node value * p data field;

---- After the new node is inserted into the tail * p * r node

---- r tail pointer points to the new end node * p

void CreateList_R (LinkList L &, int n) 
{ 
     // anteroposterior sequence input value of n elements, with the establishment of a single list header node L 
    L = new new LNode; 
   L -> Next = NULL; // first create a lead empty linked list of nodes 
  r = L; // tail pointer pointing to the first node r 
  for (I = 0 ; I <n-; ++ I) 
 { 
     P = new new LNode; // create a new node 
     cin >> p-data ; // input node element value assigned to a new data field * p 
     p-> Next = NULL; R-> Next = P; // the new node is inserted into the end node * p * after R & lt 
     R & lt = P; / / R & lt tail point to the new node P * 
} 
}

 

Guess you like

Origin www.cnblogs.com/1121518wo/p/11221856.html