C ++ create/destroy/reverse/print a single-chain table

Source: Internet
Author: User

Note: The linked list created in this example contains the header node. for destruction, each node is destroyed, so it doesn't matter whether the linked list is

The printed linked list contains header nodes. That is to say, no data is printed at the head node. The experiment proves that the data at the head node is an abnormal value with a very large absolute value, that is, the random value without a value assignment.

For reverse configuration, no matter whether the initial linked list contains the header node or not, the reverse linked list does not contain the header node. the header node of the original linked list (if any) serves as the last node, it turns out that the last node is the first node and cannot be the header node, because this node has meaningful data. When printing the function in this example to output the reverse linked list data. For example, if the node value of the chain table containing the original header node is: 1 2 3 4 5, the print after the reverse setting is supposed to be 5 4 3 2 1, however, since the print function in this example is printed from the next node of the original node, the output is: 4 3 2 1-987453212, the last one is actually an unassigned memory space.

So be sure to include the header node !!!

# Include <iostream>

Using namespace STD;

 

Typedef struct _ Node

{

Int data;

Struct _ node * next;

} Node;

 

 

// Destroy the linked list

Bool destroy (node ** head)

{

Cout <"Destroy begin ----" <Endl;

If (null = * head)

{

Cerr <"link is null! "<Endl;

Cout <"Destroy over ----" <Endl;

Return false;

}

// Destroy the node from the beginning

While (* head! = NULL)

{

Node * q = (* head)-> next;

Free (* head );

* Head = Q;

}

* Head = NULL;

Head = NULL;

 

Cout <"Destroy over ----" <Endl;

Return true;

}

 

 

// Create a linked list

Node * Create ()

{

Node * head = NULL;

Node * P = NULL;

Node * s = NULL;

Head = (node *) malloc (sizeof (node); // create a header node. The data of the header node is not assigned a value.

If (null = head)

{

Cerr <"create error! "<Endl;

Return NULL;

}

 

P = head;

Int num = 0;

Cout <"Please intput :";

While (CIN> num)

{

If (num! = 0)

{

S = (node *) malloc (sizeof (node ));

If (null = s)

{

Destroy (& head); // if a new node fails to be allocated, the linked list is destroyed.

Return head;

}

Else

{

S-> DATA = num;

P-> next = s;

P = s;

}

}

Else

{

Break;

}

}

P-> next = NULL;

Return head;

}

 

// Print the linked list

Void print (node * head)

{

If (null = head)

{

Cerr <"head is null! "<Endl;

Return;

}

 

Node * P = head-> next; // print data from the first node to the next node.

Cout <"Print link :";

While (P! = NULL)

{

Cout <p-> data <"";

P = p-> next;

}

Cout <Endl;

}

 

// Reverse configuration of the linked list

Node * reverse (node * head)

{

Node * P1, * P2, * P3;

If (null = head | null = head-> next)

{

Cout <"link is null" <Endl;

Return head;

}

 

P1 = head;

P2 = p1-> next;

While (P2! = NULL)

{

P3 = P2-> next;

P2-> next = p1;

P1 = P2;

P2 = P3;

}

Head-> next = NULL;

Head = p1; // The head Pointer Points to the last node of the original linked list.

Return head;

}

 

 

Int main ()

{

Node * head = create ();

Cout

Print (head); // print the linked list

Node * head2 = reverse (head );

Print (head2 );

Destroy (& head2 );

Print (head2 );

 

Cout <Endl;

Return 0;

}

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.