Beauty of programming-3.4-delete a node from a single-chain table without a header-reverse placement of a single-chain table

Source: Internet
Author: User

1. Brief Introduction

Suppose there is a single-chain table without a header pointer. A pointer points to a node (not the first or the last node) in the middle of the single-chain table ). Delete the node from the single-chain table.
Extended question: Given a linked list header pointer, the single-chain table is reversed.

2. Ideas

For the first problem, first switch the content of the node to be deleted to the subsequent node, and then delete the subsequent node. Note: If the deleted node is the last node, it cannot be done.

Assert (Curr! = NULL & Curr-> Link! = NULL );
Swap (Curr-> value, Curr-> Link-> value );
Tmp = Curr-> Link;
Curr-> Link = Tmp-> Link;
Delete Tmp;

For the second question, it is also very simple. Three pointers: Prev, Head, And Next point to the previous element, the current element, and the following element respectively. Head is the Head pointer, and finally the Head points to the header after the list is reversed. Note: If the Head is passed using function parameters, the pointer or pointer reference is required.

Node * Prev = NULL;
Node * Next;
While (Head ){
Next = Head-> Link;
Head-> Link = Prev;
If (Next = NULL) break;
Prev = Head;
Head = Next;
}

If the if judgment is not added, the final head is moved to the front of the reverse direction, and the NULL pointer at the end goes up. In fact, in the prev, the positive direction is backward, and a node is obtained at the end. In the above implementation, each loop should judge an IF. This implementation only needs to assign values for the last time, so this implementation is better.

Void reverse (Node * & Head ){
Node * Prev = NULL;
Node * Next;
While (Head ){
Next = Head-> link;
Head-> link = Prev;
Prev = Head;
Head = Next;
}
Head = Prev;
}

3. Implementation and Testing

# Include <iostream>
Using namespace std;

Struct node {
Int value;
Node * link;
};

Void reverse (node * & head ){
Node * Prev = NULL;
Node * next;
While (head ){
Next = head-> link;
Head-> link = Prev;
If (next = NULL) // this step is very important to avoid moving the head above the NULL pointer !!
Break;
Prev = head;
Head = next;
}
}

Int main (){
Node * head;
Head = new node; head-> value = 0;
Head-> link = new node; head-> link-> value = 1; head-> link = NULL;
// Print Head
Node * TMP = head;
While (TMP ){
Cout <TMP-> value <"";
TMP = TMP-> link;
}
Cout <Endl;
// Reverse
Reverse (head );
// Print Head
TMP = head;
While (TMP ){
Cout <TMP-> value <"";
TMP = TMP-> link;
}
Cout <Endl;
// Wait
System ("pause ");
Return 0;
}

4. Reference

The beauty of programming: 3.4. Delete nodes from a single-chain table without headers.

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.