The series output in reverse order and the series output in reverse order.

Source: Internet
Author: User

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 );}}}

 

Related Article

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.