c语言程序设计基础谢谢帮忙一下
输入若干个正整数(输入-1为结束标志),要求按输入数据的逆序建立一个链表,并输出。输入输出示例:
1 2 3 4 5 6 7 -1
7 6 5 4 3 2 1
#include <stdio.h> #include <stdlib.h> struct node { int value; struct node* next; }; void list_push_front( struct node** plist, int value ) { struct node* tmp = *plist; (*plist) = malloc( sizeof(struct node) ); (*plist)->value = value; (*plist)->next = tmp; } int main( void ) { struct node* list = NULL; for( int val; scanf("%d",&val)==1 && val!=-1; ) list_push_front( &list, val ); for( struct node* p=list; p; p=p->next ) printf( "%d ", p->value ); }
#include <stdio.h> void foo( void ) { int val; if( scanf("%d",&val)==1 && val!=-1 ) { foo(); printf( "%d ", val ); } } int main( void ) { foo(); }