nameList

本文最后更新于:2 小时前

功能:输入、遍历、输出

#include<stdio.h>
#include<string.h>
#include<malloc.h>

struct Student
{
	char name[10] ;
	struct Student *next ;
} ;


struct Student *create()
{
	struct Student *head ,*next, *current ;
	char str[10] ;
	char flag ;
	
	printf( "Please input name:\n" ) ;
	scanf( "%s" , str ) ;
	getchar() ;/* 清空回车缓存 */ 
	
	head = (struct Student *) malloc ( sizeof(struct Student) ) ;
	strcpy( head->name , str ) ; 
	
	current = head ;
	
	printf( "continue?(Y/N)\n" ) ;
	scanf( "%c" , &flag ) ;
	
	
	while( flag != 'N' )
	{
		printf( "Please input name:\n" ) ;
		scanf( "%s" , str ) ;
		getchar() ;/* 清空回车缓存 */ 
		
		next = ( struct Student * ) malloc ( sizeof(struct Student) ) ;
		strcpy( next->name , str ) ;
		
		current->next = next ;
		current = next ;
		
		printf( "continue?(Y/N)\n" ) ;
		scanf( "%c" , &flag ) ;
	}
	
	current->next = NULL ;
	
	return head ;
} 


void list( struct Student *p )
{
	while( p )
	{
		printf( "%s\n" , p->name ) ;
		p = p->next ;
	}
}


void main()
{
	struct Student *p ;
	p = create() ;
	list( p ) ;
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!