Interview question 5: print the linked list from the end to the end

Source: Internet
Author: User
Question: Enter the head node of a linked list and print the value of each node from the end to the end.

We know the characteristics of the linked list. The time complexity of finding a node is O (n). Unlike arrays, we can find the specified Element in the time of O (1) in the table below. Therefore, if you want to search for linked list elements, you must start from the node and start searching. Now we need to output the values of each node in the linked list, but it must be from the end to the header, that is, to traverse the node first and then output, a typical "advanced and then out" data structure, this reminds us of the stack structure. If we store the traversal results in a stack when we are in a traversal table, the element values in the stack will be output after the traversal ends, it is what we need to print the linked list node value from the end to the header.

BelowCodeInstance. The Code instance also includes the createlistnode () method for creating a single-chain table node, and the addtotail () method for adding a node to the end of the chain table (), printlistinreversedorder (). It is noted that since stack is used for implementation, recursive methods can also be used. The essence of recursion is a stack structure, so there are two ways to output the Linked List Value in reverse order.

View code

# Include <iostream> # Include <Stdlib. h> # Include <Stack> Using   Namespace  STD; //  Linked List Structure  Struct  Listnode {  Int  M_nvalue; listnode * M_pnext ;};  //  Create a linked list Node Listnode * createlistnode ( Int  Value) {listnode * Pnode = New  Listnode (); pnode -> M_nvalue = Value; pnode -> M_pnext =NULL;  Return  Pnode ;}  Void Printlistnode (listnode * Pnode ){}  //  All nodes in the traversal chain table  Void Printlist (listnode * Phead) {listnode * Pnode = Phead;  While (Pnode! = Null) {cout <Pnode-> m_nvalue < "   " ; Pnode = Pnode-> M_pnext;} cout < Endl ;}  //  Add a node to the end of the linked list  /*  Note that phead is a pointer to a pointer, which is typically referenced in the main function. If you want to add nodes to the linked list, the structure of the linked list will be modified. Therefore, you must pass a reference to save the modified structure.  */  Void Addtotail (listnode ** phead, Int  Value) {listnode * Pnew = New Listnode (); // Newly inserted Node Pnew-> m_nvalue = Value; pnew -> M_pnext = NULL;  If (* Phead = NULL) //  Empty linked list  { * Phead = Pnew ;}  Else  {Listnode * Pnode = * Phead;  While (Pnode-> m_pnext! = Null) pnode = Pnode->M_pnext; pnode -> M_pnext = Pnew ;}}  Void Printlistinreversedorderstack (listnode * phead) //  Stack usage  {Listnode * Pnode = Phead; stack <Listnode *> Nodes;  While (Pnode! = Null) {nodes. Push (pnode); pnode = Pnode-> M_pnext ;}  While (!Nodes. Empty () {pnode = Nodes. Top (); cout <Pnode-> m_nvalue < "   "  ; Nodes. Pop ();} cout < Endl ;}  Void Printlistinreversedorderrecursion (listnode * phead) //  Use recursion  {Listnode * Pnode = Phead;  If (Pnode! = NULL)//  Judge whether the current node is empty  {  If (Pnode-> m_pnext! = NULL) //  Determines whether the next node is empty. If not empty, recursive call Printlistinreversedorderrecursion (pnode-> M_pnext);} cout <Pnode-> m_nvalue < "   "  ;}  Void  Main () {listnode * Pnode1 = createlistnode ( 1 );//  Create a node Printlist (pnode1 ); //  Print  Addtotail ( & Pnode1, 2 ); //  Add a node to the linked list Addtotail (& pnode1, 3 ); //  Add a node to the linked list Addtotail (& pnode1, 4 ); //  Add a node to the linked list Addtotail (& pnode1, 5 ); //  Add a node to the linked list Addtotail (& pnode1, 6 ); //  Add a node to the linked list Addtotail (& pnode1, 7 ); //  Add a node to the linked list Printlist (pnode1 ); //  Print  Printlistinreversedorderstack (pnode1); printlistinreversedorderrecursion (pnode1); System ( "  Pause  "  );} 

Output result:

1
1 2 3 4 5 6 7
7 6 5 4 3 2 1
7 6 5 4 3 2 1

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.