1
Sequence of output in reverse order (10 points)
Topic content:
Your program will read a series of positive integers, without knowing the number of positive integers, and once you read 1, it means that the input ends. Then, output the number read in the reverse order of the input, not including the end of the last identity-1.
Input format:
A series of positive integers, input-1 for the end, 1 is not part of the input data.
Output format:
Output all integers in the reverse order of the input, followed by a space after each integer to distinguish it from subsequent integers, followed by a space after the last integer.
Input Sample:
1 2 3 4-1
Sample output:
4 3 2 1
time limit: 2000ms memory limit: 128000kb
1 #define_crt_secure_no_warnings2#include <stdio.h>3#include <stdlib.h>4 structNum5 {6 intNum//Digital7 structNum *next;//next node.8 };9typedefstructNum num;//ShorthandTen One //4 functions involved ANUM *init ();//Create head node, head node does not store data . - voidADD (NUM *head,intNUM);//inserting data into the trailer - voidPrint (NUM *head);//sequential printing, non-recursive theNum *dao (num *head);//reverse the list, return the new head pointer - - voidMain () - { + intnum =1; -NUM *head = Init ();//Create head node, head node does not store data . + Ascanf"%d", &num); at - while(num! =-1)//input-1 means end - { - ADD (head, num); -scanf"%d", &num); - } in -NUM *newhead = Dao (head);//reverse the list, return the new head pointer to +Print (Newhead);//sequential printing, non-recursive - theSystem"Pause"); * } $ Panax NotoginsengNUM *init ()//Create head node, head node does not store data . - { theNUM *head = (num*)malloc(sizeof(NUM));//Creating a head node + if(Head = =NULL) A { theprintf"failed to allocate memory \ n"); + returnNULL; - } $ Else $ { -Head->next = NULL;//last point to null - returnHead//returns the head pointer the } - }Wuyi the voidADD (NUM *head,intNum//inserting data into the trailer - { WuNUM *p = head;//1th Step, create pointer p, for moving - if(p = =NULL) About { $ return; - } - while(P->next! = NULL)//Traverse - { Ap = p->Next; + } the -NUM *New= (num*)malloc(sizeof(NUM));//2nd step, create pointer new, for storing new data $ if(New==NULL) the { theprintf"failed to allocate memory \ n"); the return; the } - New->num = num;//School Number in New->next = NULL;//last point to null the theP->next =New;//3rd step, p points to new About } the the voidPrint (NUM *head)//sequential printing, non-recursive the { +NUM *p = head;//Create a pointer for moving - intCount =0;//counter the Bayi while(P->next! =NULL) the { the if(count) - { -printf" "); the } theprintf"%d", p->next->num); thep = p->Next; thecount++; - } the } the thenum * DAO (num *head)//reverse the list, return the new head pointer94 { theNUM * p = NULL;//Create pointer P theNUM * q = head->next;//Create pointer Q theNUM * r = NULL;//Create pointer R98 About while(q) - {101r = q->next;//Pointer r points to the next node of Q102Q->next = p;//pointer Q points to the front node103p = q;//Move Pointer P104Q = r;//Move Pointer Q the }106 107num *newhead = (num *)malloc(sizeof(NUM));//Create pointer Newhead as the new header pointer108Newhead->next = p;//The new head pointer points to the pointer p109 returnNewhead;//returns the new head pointer the}
NetEase Cloud Classroom _c language programming fifth week: Recursion and simple algorithm: recursion, search, binary search, simple sort _1 sequence of output in reverse order