The series output in reverse order and the series output in reverse order.
This topic is the fifth week of programming assignments in NetEase cloud's C language programming advanced (Weng Kai). It is completed by referring to the demo given by the teacher in the courseware. The List struct and no sentinel nodes are defined, the question requirements and Code are as follows:
/* Name: Copyright: Author: Date: 30/03/15 Description: subject content: Your program will read a series of positive integers. You do not know the number of positive integers in advance. Once you read-1, the input ends. Then, output the number in the Inverse Order of the input, excluding-1 of the last mark. Input Format: a series of positive integers. Input-1 indicates the end.-1 is not part of the input data. Output Format: All integers are output in the opposite order of input. Each integer is followed by a space to distinguish it from the following integer. The last integer is followed by a space. Input example: 1 2 3 4-1 Output example: 4 3 2 1 */# include <stdio. h> # include <stdlib. h> typedef struct Node {int value; struct Node * next;} Node; Node * add (Node * head, int number); Node * inverse (Node * head ); void print (Node * head); int main () {// freopen ("in.txt", "r", stdin); // for test Node * head; int number; head = NULL; while (scanf ("% d", & number) & number! =-1) head = add (head, number); head = inverse (head); print (head); // fclose (stdin); // for test return 0 ;} node * add (Node * head, int number) {// add to linked-list Node * p = (Node *) malloc (sizeof (Node )); p-> value = number; p-> next = NULL; // find the last Node * last = head; if (last) {while (last-> next) last = last-> next; // attach last-> next = p;} else head = p; return head;} Node * inverse (Nod E * head) {if (head! = NULL & head-> next! = NULL) {Node * p, * q, * tmp; p = head; q = p-> next; while (q-> next) {tmp = q-> next; q-> next = p; p = q; q = tmp;} q-> next = p; head-> next = NULL; head = q;} return head ;} void print (Node * head) {Node * p, * tmp; p = head; if (p) {while (p) {printf ("% d ", p-> value); tmp = p; p = p-> next; if (p) printf (""); else printf ("\ n "); free (tmp );}}}