Inverted single-linked list--16

Source: Internet
Author: User

Define a function, enter the head node of a linked list, reverse the list and output the head node of the inverted list.


To reverse a single-linked list is to make the nodes in the list inverse, but not to traverse the list each time the node is recreated for the head plug, so that is to re-create a single-linked list instead of the original single-linked list to reverse, so you can think of multiple pointers to control, to prevent the next node is not found in the problem, Also to solve the link problem of the reverse chain list;


The program is as follows:

#include  <iostream> #include  <assert.h>using namespace std; template  <class t>struct listnode//linked list node structure {    t _data;     listnode<t>* _next;};  template <class t>listnode<t>* buy_node (T data)//Create a linked table node {     listnode<t>* tmp = new listnode<t>;    tmp- >_data = data;    tmp->_next = NULL;     Return tmp;}  template <class t>void init_list (listnode<t>** node, t data)// Initialize the linked list {    *node = buy_node (data);}  template <class t>void push_node (listnode<t>*& head, t  DATA)//Insert list Node {    if (head == null)     {       &nbsP;    init_list (&head, data);         return;    }       listnode<t>* tmp =  head;    while (tmp->_next != null)     {         tmp = tmp->_next;    }     tmp->_next = buy_node (data);}  template <class t>void print_list (listnode<t>* head)//Print List {     while (head != null)     {         cout<


To run the program:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/12/wKioL1c21avgWStzAAAT_VTSEmk406.png "title=" Reverselist.png "alt=" Wkiol1c21avgwstzaaat_vtsemk406.png "/>



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1773330

Inverted single-linked list--16

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.