[Algorithm analysis] how to delete a single-chain table node and a single-chain node in O (1) Time

Source: Internet
Author: User

[Algorithm analysis] how to delete a single-chain table node and a single-chain node in O (1) Time

The question is as follows: give you a single-chain table header, and give you a pointer to a node. to delete this node, the condition is that your program must be in O (1).

Some people are not very familiar with the linked list. This article tries its best to make it easy to understand. Please skip the previous section.

The linked list structure is as follows:

struct node{    int val;    node* next;};

The question is not very difficult, and you will soon be able to think of a good way :)

First, let's review the common deletion method. First, use the header to locate the previous node (set to A) of the node to be deleted (set to B), and change the pointing of, delete node B. Delete the node to be deleted. This is not only a good habit, but also can avoid serious problems that may cause memory leakage in future projects.

void DeleteNode_On(node *LinkList, node *p){    printf("delete:%d\n", p->val);    node *s = LinkList;    while(s->next != p)        s = s->next;    s->next = s->next->next;    delete(p);}

This algorithm mainly takes time to find the previous node, so it is an O (n) algorithm.

Therefore, since the requirement is O (1), it is obvious that the for operation cannot be performed again. Considering the deletion of arrays, this problem is better solved.

First, we can easily get the node to be deleted, that is, the next node C of Node B, and then assign the value of C to the value of Node B, which is equivalent to overwriting when the array is deleted, now Node B and node C are exactly the same. Next we can simply delete node B without deleting Node B. Simply delete node C by using node B. The method is simple and the time is O (1 ).


But think about the obvious defect of this algorithm. What if the node to be deleted is the last node? At this time, it seems that there is no good solution, and it can only be honest O (n. Now let's look at the average time complexity:

Meets the question requirements.

Void DeleteNode_O1 (node * LinkList, node * p) {printf ("delete: % d \ n", p-> val); if (p-> next! = NULL) // If p is not the end node, overwrite p from the last node, and delete the last node {p-> val = p-> next-> val; node * tmp = p-> next; p-> next = p-> next; delete (tmp);} else // If p is the end node, find the previous node of p and delete {node * s = LinkList; while (s-> next! = P) s = s-> next; s-> next = s-> next; delete (p );}}

Complete Test code is attached:

# Include <iostream> using namespace std; struct node {int val; node * next;}; void CreateLinkList (node * LinkList) {node * s = LinkList; for (int I = 0; I <10; I ++) {node * t = new node; t-> val = I; t-> next = NULL; s = s-> next = t;} void ShowLinkList (node * LinkList) {node * s = LinkList-> next; while (s = s-> next) printf ("% d", s-> val); putchar (10);} void DeleteNode_On (node * LinkList, node * p) {printf ("dele Te: % d \ n ", p-> val); node * s = LinkList; while (s-> next! = P) s = s-> next; s-> next = s-> next; delete (p);} void DeleteNode_O1 (node * LinkList, node * p) {printf ("delete: % d \ n", p-> val); if (p-> next! = NULL) // If p is not the end node, overwrite p from the last node, and delete the last node {p-> val = p-> next-> val; node * tmp = p-> next; p-> next = p-> next; delete (tmp);} else // If p is the end node, find the previous node of p and delete {node * s = LinkList; while (s-> next! = P) s = s-> next; s-> next = s-> next; delete (p) ;}} int main () {node * LinkList = new node; CreateLinkList (LinkList); ShowLinkList (LinkList); node * p = LinkList-> next; for (int I = 0; I <3; I ++) p = p-> next; DeleteNode_On (LinkList, p); ShowLinkList (LinkList); p = LinkList-> next; for (int I = 0; I <8; I ++) p = p-> next; DeleteNode_O1 (LinkList, p); ShowLinkList (LinkList); p = LinkList-> next; for (int I = 0; I <4; I ++) p = p-> next; DeleteNode_O1 (LinkList, p); ShowLinkList (LinkList); getchar (); return 0 ;}


Design an algorithm for deleting the I-th node in the single-link table of the lead node.

// Delete a node to delete node I
Int Delete_Positon_LL (LinkList * phead, int I)
{
LinkList p, q; // p indicates the node whose value is x, and q indicates the previous node of p.
Int j;

If (* phead)-> next = NULL) // if the linked list is empty, perform underflow processing.
{
Printf ("single-chain table is empty! \ N ");
Return 0;
}
If (I = 1) // if it is a header, the header is moved back.
{
P = (* phead)-> next;
(* Phead)-> next = (* phead)-> next;
Free (p); // release the header
}
Else // query from the second node with the value of x
{
Q = (* phead)-> next;
P = (* phead)-> next;
J = 2;
// Note p first! = NULL. Otherwise, illegal access will occur.
While (p! = NULL & j <I)
{
Q = p;
P = p-> next;
J ++;
}

If (p! = NULL) // found
{
Q-> next = p-> next; // point the previous node to the successor node of p.
Free (p); // delete node p
}
Else
{
Printf ("deleting node % d out of range. \ n", I );
Return 0;
}
}

Return 1;

}

Algorithm for deleting sequence tables and single-chain tables

The delete operation of a single-chain table is to delete the I-th node and return the value of the deleted node. The delete operation also needs to traverse the single-chain table from the beginning of the reference until the node at the position I is found. If I is 1, you need to delete the first node. You need to assign the address of the direct successor node of this node to the header reference. For other nodes, to delete the node, you need to save the direct precursor of the node to be traversed during the traversal process. After finding the node I, the direct successor of the node serves as the direct successor of the node. Delete operation

Delete a single-chain table

The deletion algorithm is implemented as follows:
Public T Delete (int I)
{
If (IsEmpty () | I <0)
{
Console. WriteLine ("Link is empty or Position is error! ");
Return default (T );
}
Node q = new Node ();
If (I = 1)
{
Q = head;
Head = head. Next;
Return q. Data;
}
Node p = head;
Int j = 1;
While (p. Next! = Null & j <I)
{
++ J;
Q = p;
P = p. Next;
}
If (j = I)
{
Q. Next = p. Next;
Return p. Data;
}
Else
{
Console. WriteLine ("The ith node is not exist! ");
Return default (T );
}
}
Algorithm time complexity analysis: the delete operation on a single-chain table is the same as the insert operation. The time is mainly consumed by node traversal. If the table is empty, no traversal is performed. When the table is not empty, delete the node at the position I. I equals to the minimum number of knots traversed by 1 (1), and I equals to the maximum number of knots traversed by n (n, n is the length of a single-chain table), and the average number of knots traversed is n/2. Therefore, the time complexity of the delete operation is O (n ).... Remaining full text>

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.