Delete a linear Table 3.8.2 single-chain table in Chapter 3rd of big talk Data Structure

Source: Internet
Author: User

3.8.2 delete a single-chain table

Now let's look at the deletion of a single-chain table. If the node where the storage Element AI is set to Q, to delete the node Q from a single-chain table, it is actually bypassing the pointer of its forward node, point to its successor node (3-8-5 ).

All we need to do is actually a step. P-> next = p-> next, replace P-> next with Q, that is


Q = p-> next; P-> next = Q-> next;

Interpret the two codes, that is, change the successor node of P to the successor node of P. It's a bit difficult. Let me make another image. It was originally my father's mother's hand left, and his baby's hand right took a walk on the road. Suddenly a beautiful girl came along, and my father suddenly looked down. This scene was caught by her mother. So she angrily opened her father's hand, bypassed him, pulled the father and son, pulled the baby's left hand and walked forward quickly. At this time, the mother is the P node, and the mother's successor is the father p-> next, which can also be called the Q node. The mother's successor is the son p-> next, q-> next. When the mother took her son's hand, the father had no contact with the mother and child (3-8-6 ).

An Algorithm for deleting the I data node in a single-chain table:
1. Declare that node P points to the first node of the linked list, and initialize J from 1;
2. When j <I, let the P pointer move backward and keep pointing to the next node. j accumulates 1;
3. If P is null at the end of the linked list, the I-th element does not exist;
4. Otherwise, the query is successful. Assign the node p-> next to Q;
5. Standard statement for deleting a single-chain table p-> next = Q-> next;
6. Assign the data in the Q node to E as the return value;
7. Release the Q node;
8. Return success.

The implementation code algorithm is as follows:

/* Initial condition: the ordered linear table l already exists, 1 ≤ I ≤ listlength (l )*/
/* Operation result: Delete the I-th data element of L and return its value with E. The length of L is reduced by 1 */
Status listdelete (linklist * l, int I, elemtype * E)
{
Int J;
Linklist p, q;
P = * l;
J = 1;
While (p-> next & J <I)/* traverse to find the I-th element */
{
P = p-> next;
++ J;
}
If (! (P-> next) | j> I)
Return Error;/* element I does not exist */
Q = p-> next;
P-> next = Q-> next;/* assign the Q successor to p */
* E = Q-> data;/* send the data in the Q node to E */
Free (Q);/* let the system reclaim this node and release the memory */
Return OK;
}

 

In this algorithm code, we use another c-language standard function free. Its function is to let the system reclaim a node and release the memory.
After analyzing the single-chain table insertion and deletion algorithms we just explained, we found that they are actually composed of two parts: the first part is to traverse and find the I element; the second part is to insert and delete elements. From the perspective of the entire algorithm, we can easily deduce that their time complexity is O (n ). If we do not know the pointer position of the I-th element, the data structure of the single-chain table is in the insert and delete operations, and the sequential storage structure of the linear table is not very advantageous. However, if we want to insert 10 elements from position I, for the sequential storage structure, each insert requires moving n-I elements, every time it is O (n ). For a single-chain table, we only need to find the pointer at the position I at the first time, which is O (n) At this time. Next, we simply move the pointer by assigning values, the time complexity is O (1 ). Obviously, the more frequent data insertion or deletion operations, the more obvious the efficiency advantage of a single-chain table.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.