Print linked list from tail to head

Source: Internet
Author: User

Title: Enter the head node of a linked list, which in turn prints the value of each node from the end of the head.

1. Definition of structure, construction of linked list and output of linked list data
structlistnode{intdata; ListNode*next;};//using the tail interpolation method to construct the linked listListNode *init () {ListNode*head= (ListNode *)malloc(sizeof(ListNode)); Head->next=NULL; ListNode*last=Head; intdata; cout<<"Please enter: (Press-1 to end the list)"<<Endl; CIN>>data;  while(data!=-1) {ListNode*temp= (ListNode *)malloc(sizeof(ListNode)); Temp->data=data; Temp->next=last->next;//the next of last is nulllast->next=temp; Last=temp; cout<<"Please enter: (Press-1 to end the list)"<<Endl; CIN>>data; }    returnhead;}//Print linked listvoidPrint (ListNode *head) {ListNode*temp=head->Next;  while(temp!=NULL) {cout<<"  "<<temp->data; Temp=temp->Next; }}


2. Using the Stack implementation
//use stacks to print linked lists from tail to headvoidUsestack (ListNode *head) {    if(head!=NULL) {Stack<int> s=stack<int>(); ListNode*temp=head->Next;  while(Temp!=null)//loop into the stack{s.push (temp-data); Temp=temp->Next; }         while(S.size ()! =0)//loop out the stack{cout<<"  "<<S.top ();        S.pop (); }    }}

3. Reverse linked list (implemented with 3 pointers)
//Reverse the linked list to achieve inverse outputListNode *resert (ListNode *head) {ListNode*now=head->Next; ListNode*pre=NULL; ListNode*newhead=NULL;  while(now!=NULL) {ListNode*next=now->next;//records the next node of the current node so that it reverses the pointer        if(Next==null)//if the next node of the pointer to the current node is empty, the current node is logged and returned .Newhead=Now ; now->next=pre;//Invert Pointer//pointer moves backPre=Now ; now=Next; } ListNode*hhead= (ListNode *)malloc(sizeof(ListNode));//since the first number is empty, this adds a shorthhead->next=Newhead; returnHhead;}

Test code:

int Main () {    *head=init ();    Print (head);    cout<<Endl;    Usestack (head);    cout<<Endl;    Head=Resert (head);    Print (head);}

Screen:

Print linked list from tail to head

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.