Deletes a single-linked table node.

Source: Internet
Author: User

Deletes a single-linked table node.
Question: Delete the node p in the single-chain table L of the lead node. p is not the last node and requires the time complexity to be O (1 ).

Solution:
If the time complexity is not required to be O (1), we can find the precursor node of p and then delete p easily.
The time complexity is O (1). Because p is not the last node, we can delete the successor node of p when we know node p, then, we can assign the value of the successor node element of p to the value of the element of the p node.

ADT is defined as follows:
# Define ElemType int
Typedef struct LNode {
ElemType data;
LNode * next;

} LNode, * LinkList;


Algorithm Implementation:

Void deleteNode (LNode * & p) {if (p = NULL | p-> next = NULL) return; // If p is null or the last node in the single-chain table does not match the meaning of the question, the LNode * q = p-> next is returned; // q is the successor node p-> data = q-> data; p-> next = q-> next; // Delete the node qfree (q) from the single-chain table ); // release node qq = NULL; // set node q to NULL to prevent it from being a wild pointer}

Extended: If the deleted node p may be the last node, what should I do.

Solution: At this time, we can only ensure that the average time complexity of deleting nodes is O (1). When p is not the last node, the time complexity is O (1 ), when p is the last knot point, the time complexity is O (n ).

Algorithm Implementation:

Void deleteNode (LinkList & L, LNode * & p) {if (p = NULL | L = NULL) return; // if p is null or the single-chain table L is empty, if (p-> next) is returned directly) // If p is not the last node {LNode * q = p-> next; // If q is p, p-> data = q-> data; p-> next = q-> next; // Delete the node q free (q) from the single-link table; // release the node q = NULL; // leave node q empty to prevent it from becoming a wild pointer} else // If p is the last node {LNode * pre = L; // use pre to represent the p's precursor node while (pre-> next) // keep the pre with a successor node {if (pre-> next = p) // If pre is the precursor node of p {pre-> next = p-> next; // delete node p from the single-chain table free (p ); // release node p = NULL; // set node p Null to prevent it from becoming a wild pointer} if (! P) // p is not empty, meaning p is not in L {return ;}}}


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.