[Data Structure] Insert and deletion of sequence table, single-link table, loop chain list

  • written in front
  • Sequence table
    • Insert
    • Delete
    • Locate
  • Single-linked list
    • Insert
    • Delete
  • Two-way circular linked list
    • Delete
    • Insert

    < /li>

  • Summary

written at the front

< p> In the process of reviewing the data structure, it’s always easy to forget the operations of the linked list. From time to time, I don’t know how to do the specific operations. Summarize the more details, deepen your impression, and serve as a reference for later learning.

Next, I will summarize the single linked list and circular linked list Insert and delete methods and specific codes. The map is as follows
Write the picture description here< /p>

Order table

insert

  • Steps:First move the node one element backward in turn, so that the i-th one is vacated The position of the data element; then insert the data x into this position, and finally increase the length of the table by 1.

  • The algorithm is as follows:

void InsertSeqlist(SeqList L,DataType x,int i){ //Insert the element x into the i-th of the sequence table L Before the data element if(L.length==Maxsize) exit("The linked list is full!"); if(i<1); span> || i>L.length + 1) exit("Insert an illegal position!"); for(j=L.length,j>=i;j--) {L.data[j]=L.data[j-1]; //Move back in turn} L.data[i-1]=x; //Insert x into the position where the subscript is i-1 L.length++; //table Long plus 1}
  • Legend explanation:< /p>

    Write the picture description here

    < p>After the element is moved:

    Write the picture description here

    Finally insert:

    Write the picture description here

Delete

  • Steps:The node moves one element position to the left one by one; the length of the table is reduced by one. Different from inserting, there is no need to consider overflow here, just judge whether the parameter i is legal.

  • The algorithm is as follows:

void DeleteSeqList(SeqList L, int i){ //Delete the i-th node in the linear table L/ span> if(i<1 || i>L.length) exit("Illegal location!"); for(j=i; jlength; j++) //The subscript of the i-th element is i-1 { L.data[j-1]=L.data[j]; //Move left in turn} L.length--;}

positioning

  • Algorithm description:i starts from 0 as the scan order table The following table. If there is a node in the table whose value is equal to x, or i is equal to L.length, the loop is terminated. If the loop ends at i=L.length, x is not found and 0 is returned, otherwise the position of x is returned.

  • The algorithm is as follows:

int LocateSeqList(SeqList L, DataType x){  int i=0; while((ilength) && (L.data[i]!=x)) //Find the node with value x in the sequence table { i++;} if(ilength) return  i+1; else return 0;}

single-linked list

insert h2>

  • Steps: Insert the element of the given x before the i-th node of the linked list. First find the i-1th node q of the linked list, and then generate a new node p with a value of x. The pointer of p points to the immediate successor point of q, and the pointer of q points to p, thus completing the insertion operation of the singly linked list.

  • The map is as follows:

Write the picture description here

  • The algorithm is as follows:
void InsertLinklist(LinkList head, DataType x, int i){ Node *p, *q; if(i== 1) q=head; else q =GetLinklist(head,i-1 ); //Find the i-1th data element node if(q==NULL) exit( "Cannot find the inserted position! "); //The i-1th node does not exist else {p=malloc(sizeof(Node)); p->data< /span>=x; //Generate a new node p ->next=q->next; //The new street point chain points to the successor node of *q q->next= p; //Modify the chain domain of *q }}" link operation p->next=q->next and q ->next=p The execution order of the two statements cannot be reversed, otherwise the chain domain value of the node *q (pointer to the i-th node in the original table) will be lost."

Delete

  • Steps: Given a value i, remove the i-th node in the linked list from the linked list, and modify the pointers of related nodes to maintain the link relationship of the remaining nodes.< /font>

  • The map is as follows:
    < img src ="/wp-content/uploads/images/os/data-structure/1626797664152.net/20160919223734069" alt="Write the picture description here" title="" >

  • The algorithm is as follows:

void DeleteLinklist(LinkList head, int i){ Node *q;  if (i==1) q =head; else q=GetLinklist(head, i-1); //First find the immediate predecessor of the node to be deleted < span class="hljs-keyword">if(q!=NULL && q->next!=NULL) //If the direct predecessor exists, the node to be deleted exists {p=q->< /span>next; //p points to the node to be deleted q->next=p->next; //Remove the node to be deleted free(p); //Release the space of the removed node p} else exit( "Cannot find the node to be deleted! ")}

Two-way circular linked list

Delete

  • < font face="Microsoft Yahei" size="4">Because the doubly linked list has pointers prior and next, the operation is more complicated than other forms of tables. Basic The syntax is as follows:
p->prior->next=p->next; //The following chain of the predecessor node of p points to the successor node of p Point p->next->prior=p->prior; //The front chain of the successor node of p points to the predecessor node of p free(p); //Release the space of *p "The first two lines can be reversed"

The map is as follows: < br> Write the picture description here

Insert

  • The basic syntax is as follows:
  • < /ul>

    t->prior=p;t->next =p->next;p->next->prior=t;p->next=t ;
    • The map is as follows:
      Write the picture description here

    Summary

    • By summarizing the linked list operations in the data structure, it is very obvious what the pointers in the table are in the specific exchange.” Sports”. And the sequence of pointer movement must be clear, otherwise the entire linked list is very easy to lose, and the details determine success or failure!

    < li>Write in front
  • Sequence table
    • Insert
    • Delete
    • Locate
  • Single-linked list
    • Insert
    • Delete
  • Two-way circular linked list
    • Delete
    • < li>Insert

  • Summary

written at the front

In the process of reviewing the data structure, it’s easy to forget the operations of the linked list, and I don’t know it from time to time. How to do it in detail, so I summarize these few more details to deepen my impression and serve as a reference for later learning.

Next, I will summarize the single linked list and circular linked list Insert and delete methods and specific codes. The map is as follows
Write the picture description here< /p>

Order table

insert

  • Steps:First move the node one element backward in turn, so that the i-th one is vacated The position of the data element; then insert the data x into this position, and finally increase the length of the table by 1.

  • The algorithm is as follows:

void InsertSeqlist(SeqList L,DataType x,int i){ //Insert the element x into the i-th of the sequence table L Before the data element if(L.length==Maxsize) exit("The linked list is full!"); if(i<1); span> || i>L.length + 1) exit("Insert an illegal position!"); for(j=L.length,j>=i;j--) {L.data[j]=L.data[j- 1]; //Move back in turn} L.data[i-1]= x; //Insert x into the position where the subscript is i-1 L.length++; //Add 1 to the table length }
  • Legend explanation:

    Write the picture description here

    < font face="Microsoft Yahei" size="4">After the element is moved:

    Write the picture description here

    Finally insert:

    Write the picture description here

Delete

  • Steps :The nodes turn to the left in turn Move one element position; reduce the length of the table by one. Different from inserting, there is no need to consider overflow here, just judge whether the parameter i is legal.

  • The algorithm is as follows:

void DeleteSeqList(SeqList L, int i){ //Delete the i-th node in the linear table L/ span> if(i<1 || i>L.length) exit("Illegal location!"); for(j=i; jlength; j++) //The subscript of the i-th element is i-1 { L.data[j-1]=L.data[j]; //Move left in turn} L.length--;}

positioning

  • Algorithm description:i starts from 0 as the scan order table The following table. If there is a node in the table whose value is equal to x, or i is equal to L.length, the loop is terminated. If the loop ends at i=L.length, x is not found and 0 is returned, otherwise the position of x is returned.

  • The algorithm is as follows:

int LocateSeqList(SeqList L, DataType x){  int i=0; while((ilength) && (L.data[i]!=x)) //Find the node with value x in the sequence table { i++;} if(ilength) return  i+1; else return 0;}

single-linked list

insert h2>

  • Steps: Insert the element of the given x before the i-th node of the linked list. First find the i-1th node q of the linked list, and then generate a new node p with a value of x. The pointer of p points to the immediate successor point of q, and the pointer of q points to p, thus completing the insertion operation of the singly linked list.

  • The map is as follows:

Write the picture description here

  • The algorithm is as follows:
void InsertLinklist(LinkList head, DataType x, int i){ Node *p, *q; if(i== 1) q=head; else q =GetLinklist(head,i-1 ); //Find the i-1th data element node if(q==NULL) exit("Cannot find the inserted position! "); //The i-1th node does not exist else {p=malloc(sizeof(Node)); p->data< /span>=x; //Generate a new node p ->next=q->next; //The new street point chain points to the successor node of *q q->next= p; //Modify the chain domain of *q }}" link operation p->next=q->next and q ->next=p The execution order of the two statements cannot be reversed, otherwise the chain domain value of the node *q (pointer to the i-th node in the original table) will be lost."

Delete

  • Steps: Given a value i, remove the i-th node in the linked list from the linked list, and modify the pointers of related nodes to maintain the link relationship of the remaining nodes.< /font>

  • The map is as follows:
    < img src="/w p-content/uploads/images/os/data-structure/1626797664164.net/20160919223734069" alt="Write the picture description here" title="" >

  • The algorithm is as follows:

void DeleteLinklist(LinkList head, int i){ Node *q; if (i==1) q=head; else q=GetLinklist(head, i -1); //First find the immediate predecessor of the node to be deleted if(q!=NULL && q->next!=NULL) // If the direct precursor exists, the node to be deleted exists {p=q->next; //pPoint to the node to be deleted q->next =p->next; //Remove the node to be deleted free(p) ; //Release the space of the removed node p} else exit( "Cannot find the node to be deleted! ")}

Two-way circular linked list

Delete

  • < font face="Microsoft Yahei" size="4">Because the doubly linked list has pointers prior and next, the operation is more complicated than other forms of tables. Basic The syntax is as follows:
p->prior->next=p->next; //The following chain of the predecessor node of p points to the successor node of p Point p->next->prior=p->prior; //The front chain of the successor node of p points to the predecessor node of p free(p); //Release the space of *p "The first two lines can be reversed"

The map is as follows: < br> Write the picture description here

Insert

  • The basic syntax is as follows:
  • < /ul>

    t->prior=p;t->next =p->next;p->next->prior=t;p->next=t ;
    • The map is as follows:
      Write the picture description here

    Summary

    • By summarizing the operation of the linked list in the data structure, it is very obvious how the pointer in the table “moves” in a specific exchange. And the sequence of pointer movement must be clear, otherwise the entire linked list is very easy to lose, and the details determine success or failure!
    • written in front
    • Sequence table
        < li>Insert
      • Delete
      • Locate
    • Single-linked list
      • Insert
      • Delete
    • Two-way circular linked list
      • Delete
      • Insert
    • Summary

    • Write in front
    • Sequence table
      • Insert
      • < li>Delete

      • Locate
    • Single-linked list
      • Insert
      • Delete
    • Two-way circular linked list
      • Delete
      • Insert
    • Summary

Leave a Comment

Your email address will not be published.