9-degree OJ1511: print the linked list from the end to the end, offeroj1511

Source: Internet
Author: User

9-degree OJ1511: print the linked list from the end to the end, offeroj1511

Question link:
Http://ac.jobdu.com/problem.php? Pid = 1, 1511

Print the linked list from the end to the end

Time Limit: 1 second memory limit: 128 MB Special Judgment: No submitted: 6036 resolution: 1817
Description:
Enter a chain table to print the values of each node in the chain table from the end to the end.
Input:
Each input file contains only one set of test samples.
Each group of test cases contains multiple rows. Each row contains an integer greater than 0, representing a linked list node. The first line is the value of the first node of the linked list, and so on. When the input time is-1, the Table chain table is input. -1 itself does not belong to the linked list.
Output:
For each test case, the values of each node in the chain table are output from the end to the end, and each value occupies one row.
Sample input:
1
2
3
4
5
-1
Sample output:
5
4
3
2
1

Analysis:

Method 1: generate a linked list in reverse order using the stack
The linked list is always accessed from the beginning during access, while accessing the stack. The stack features advanced post-release, so the reverse input of the linked list is used when the stack is output.
Method 2: Recursive Implementation, perfect
Each time a node is accessed, a node following the output is recursively accessed, and then the node itself is output in reverse order.

Method 2 9-degree AC implementation:

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; typedef struct Node {int data; struct Node * next;} Node, * pNode;/* print a single-chain table recursively from the end to the end */void PrintListReverse (pNode pHead) {if (pHead = NULL) return; if (pHead-> next! = NULL) PrintListReverse (pHead-> next); printf ("% d \ n", pHead-> data);} pNode CreateList () {int val; pNode pHead = NULL; pNode pCur = NULL; do {scanf ("% d", & val); if (val! =-1) {pNode pNew = (pNode) malloc (sizeof (Node); pNew-> data = val; pNew-> next = NULL; if (pHead = NULL) {pHead = pNew; pCur = pHead;} else {pCur-> next = pNew; pCur = pCur-> next ;}} while (val! =-1); return pHead;} int main () {pNode pHead = CreateList (); PrintListReverse (pHead); return 0 ;} "data-snippet-id =" ext.74a6fec1324c46e2e1780fa1f59e24c8 "data-snippet-saved =" false "data-csrftoken =" sdo5J4AD-xNLccKJgD_R6uZEKjMnYTCN1cP8 "data-codota-status =" done ">
     /******************************** ------------------------------- [Sword refers to the offline interview questions] print the linked list ----------------------------------------- Author: mu zhidian Date: 2015 Email: bzhou84@163.com **********************************/# include <stdio. h> # include <stdlib. h >#include <cstring> # include <string> # include <iostream> using namespace std; typedef struct Node {int data; struct Node * next;} Node, * pNode; /* recursively print a single-chain table from the end to the header */void PrintListReverse (pNode pHead) {if (pHead = NULL) return; if (pHead-> next! = NULL) PrintListReverse (pHead-> next); printf ("% d \ n", pHead-> data);} pNode CreateList () {int val; pNode pHead = NULL; pNode pCur = NULL; do {scanf ("% d", & val); if (val! =-1) {pNode pNew = (pNode) malloc (sizeof (Node); pNew-> data = val; pNew-> next = NULL; if (pHead = NULL) {pHead = pNew; pCur = pHead;} else {pCur-> next = pNew; pCur = pCur-> next ;}} while (val! =-1); return pHead;} int main () {pNode pHead = CreateList (); PrintListReverse (pHead); return 0 ;}
    
   
  
 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.