Enter the head node of a linked list and print the value of each node in turn from the end of the tail--5

Source: Internet
Author: User

Generally such a problem, the list is certainly not a doubly linked list is also a loop or something, that is, to give a single-linked list of the head node, and then output from the end of each node value;

If you look for the last node from the back, you will find the output and then there is no way to go back to the head, because it is just a single linked list, so you can think of it with recursion:

#include  <iostream>using namespace std;template <class T>            //defines the node of a linked list as a template class, which implements the reusability of the Code struct listnode{     T _data;    ListNode<T>* _next;}; Template <class t>listnode<t>* buy_node (T data)          //Creating a 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)   Initialization of the  //list {    *node = buy_node (data);} Template <class t>void push_node (Listnode<t>*& head, t data)       //to ChainTable Insert Node {    if (head == null)     {            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 destroy_list (Listnode<t>*& head)    / /Destroy linked list {    if (head != null)     {         ListNode<T>* cur = head;         listnode<t>* tmp = head;&nBsp;       while (cur != null)          {            tmp = cur;             cur = cur->_next;             delete tmp;         }        head = NULL;     }}template <class t>void print_list (Listnode<t>* head)     //the data of the positive sequence print List {    while (head != null)     {         cout<

The above pest only to complete the problem request and did not implement the list of other operations, such as pop data and find delete insert functions, run the program can have the following results:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7F/7A/wKioL1cgUc_zP30sAAAVdBxIpak003.png "title=" List1.png "alt=" Wkiol1cguc_zp30saaavdbxipak003.png "/>


From the above program can be known, the reverse of the list output is actually after the insertion of the node output, the first place to put the last node output, so that is the principle of LIFO, you can use the stack to achieve, from the beginning to traverse the linked list, the node one by one push_back into the stack, and then from the top of the stack to remove data, The last node of the linked list is taken, and then the pop data is then taken to the top of the stack;

The above program is a non-class variable, you can define a list class to implement, and when the program is finished running, do not manually call the destructor to free space:

#include  <iostream> #include  <vector>using namespace std;template < class t>struct listnode            // Linked list node structure body {    t _data;    listnode<t>* _next;     listnode (T data)         :_data (data)          ,_next (NULL)     {}  };template  <class T>class List                //implements a list class {public:    list ()                  //Default Constructors          : _head (NULL)     {}      list (t data)               //constructor         :_head with parameters (New ListNode <T> (data))     {}      ~list ()                //destructor, releasing the Link table node space     {            if (_head != null)          {             listnode<t>* tmp = _head;             ListNode<T>* cur = _head;             while (cur != null)              {                 tmp = cur;                cur = cur->_next;                 delete  tmp;            }             _head = NULL;         }    }    void _push (T data)                          //push data     {        if at the tail of the linked list (_head ==  null)         {             _head = new ListNode<T> (data);             return;        }         listnode<t>* tmp = _head;        while (tmp- >_next != null)             tmp =  tmp->_next;        tmp->_next = new  listnode<t> (data);     }    void print_list ()                 //positive sequence output chain list      {        listnode<t>* tmp = _head;         while (tmp != null)          {            cout<<tmp->_data< < ";   "         tmp = tmp->_next;         }        cout<< "NULL" <<endl;     }    void reverseprintlist ()                //Reverse output Chain list     {         vector<T> list;        ListNode< T>* tmp = _head;        while (Tmp != NULL)              //link table nodes into the stack in turn          {             List.push_back (tmp->_data);             tmp  = tmp->_next;        }        cout<< " Reverse print list: "<<endl;        while (!list.empty () &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//continues to take the top element of the stack, releasing the top element of the stack          {             Cout<<list.back () <<;             list.pop_back ();        }         cout<< "NULL" <<endl;    }private:    listnode <T>* _head;}; Int main () {    list<int> list (1);     list._push (2);     list._push (3);     list._push (4);     list._push (5); &nbsP;   list._push (6);     list._push (7);     list._push (8);     list._push (9);     list.print_list ();     list. Reverseprintlist ();     return 0;}


Running the program can have the following results:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/7E/wKiom1cgd8ryTugJAAAIV2OqMMY154.png "title=" List2.png "alt=" Wkiom1cgd8rytugjaaaiv2oqmmy154.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/1768268

Enter the head node of a linked list and print the value of each node in turn from the end of the tail--5

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.