C语言链表的实验有问题

该段代码调试了N次,还是存在问题,不知道该怎么改了。

/*编写一个建立考生链表的函数 creat() .每个考生的数据包括准考证号
(8字符)   姓名(16字符)  总分三个部分*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NULL 0


struct student
{
char Num[9];
char Name[16];
int score ;
struct student *next;
};


struct student  *creat(  )
{
struct student *head , *tail , *New ;
char Num[9];
char Name[16];
int score;
int size = sizeof ( struct student );
head = tail = NULL ;
printf ( "please input the first DATA : /n  Number  Name Score /n");
scanf ( " %s ", Num );
scanf ( " %s ", Name );
scanf ( " %d ", &score );
while ( score != 0 )
{
New = ( struct student *)malloc ( size );  // 为新结点开辟内存空间
strcpy ( New -> Num , Num );
strcpy ( New -> Name, Name );
    New -> score = score ;
New -> next = NULL ;
if ( head == NULL )
head =New ;
else
tail -> next = New ;
tail =New;
printf ( "Please input the next DATA : /n  Number Name Score /n");
scanf ( " %s " ,Num );
scanf ( " %s " ,Name );
scanf ( " %d" ,&score );
}
return head ;
}




/*编写一个输出考生链表的函数 print () */


void print ( struct student *head )
{
struct student *p = head ;
printf ( "Number Name Score /n");
while ( p != NULL  )
{
printf ( " %s " ,  p -> Num );
printf ( " %s " ,  p -> Name );
printf ( " %d ",  p -> score );
printf ( "/n" ) ;
p = p -> next ;
}
}



/*调用1, 2 两个函数, 并对其验证正确性



void main ()
{
struct student *head ;
head = creat ( ) ;
print ( head ) ;

} */



/*编写一个函数belongTo() 函数,它负责检查一个指定的准考正号
是否已经存在链表中 ,如果已经存在,则返回在第几个节点,否则
返回0 */


int belongTo ( char Num[] )
{
struct student *p , *head ;
int count = 0 ;
p = head ;
while ( p && strcmp ( p -> Num ,Num))
{
p = p -> next ;
count++;
}
if ( !p )
return 0;
return count;
}



/*第5题  */

struct student *insert ( struct student *head )
{
struct student *p1 ,*min ,*p2 ,*New ;
char Num[9];
int size = sizeof ( struct student );
p1 = head ;
strcpy ( min -> Num , p1 -> Num );
p1 = p1 -> next ;
while ( p1 -> next )
{
if ( strcmp ( min -> Num , p1 -> Num ) > 0 )
{
strcpy ( p2-> Num , min -> Num );
strcpy ( min -> Num , p1 -> Num );
strcpy ( p1 -> Num , min -> Num ) ;
}
p1 = p1 -> next ;
}
printf ( " please input a new data : /n " );
scanf ( " %s " ,&Num ) ;
New = ( struct student * ) malloc ( size ) ;
p1 = head ;
strcpy ( New -> Num , Num );
while ( p1 -> next )
{
if ( belongTo ( p1 -> Num ) != 0 )
{
printf (" This number is not existed ,please reinput :/n ");
scanf ( " %s " ,&Num );
strcpy ( p1 -> Num   , Num );
}
p1 = p1 -> next ;
}
p1 = head ;
if ( head == NULL )
{
head = New ;
New -> next = NULL ;
}
else
{
while ( (p1 -> next) && ( New -> Num > p1 -> Num ) )
{
p2 = p1 ;
p1 = p1 -> next ;
}
if ( New -> Num <= p1 -> Num )
{
if ( head == p1 )
head = New ;
else
p2 -> next = New ;
New -> next = p1 ;
}
else
{
p1 -> next = New ;
New -> next = NULL ;
}
}
return head ;
}



/*NO.6  */

struct student *Delete ( struct student *head )
{
struct student *p1 ,*p2 ,*p3;
char Num[9];
if ( head == NULL)
{
printf ( "/nlist null");
return head;
}
p1 = head ;
printf ("please input a number : /n");
scanf ( "%s" , Num);
if ( belongTo ( Num ) == 0)
{
printf ( "This number is not existed ,please reinput /n");
scanf ( "%s" , Num);
}     //利用belongto函数进行检查
while ( strcmp ( p1 -> Num ,Num) && ( p1 -> next))
{
p2 = p1 ;
p1 = p1 -> next ;
}
if ( strcmp ( p1 -> Num ,Num) == 0)   //find the site
{
if ( head == p1 )
head = p1 -> next ;
else
p2 -> next = p1 -> next ;
printf ( "Delete : %s /n" , Num);
free ( p1 );
}
return head ;
}





/*NO.7 */


void main ()
{
    struct student *head;
    int Num ,i ;
    head = creat ( );
    for ( i = 0 ; i < 35 ; i++ )  // to make the print beautiful
printf ("*");
printf ("/n");
printf ( "  0 : Exit /n ");
printf ( " 1 : Insert /n ");
printf ( " 2 : Delete  /n ");
printf ( " 3 : Display /n ");
printf ( "Please input your choice ;/n ");
scanf ( "%d", Num );
printf ( "/n" );
for ( i = 0 ; i < 35 ; i++ )
printf ("*");
switch ( Num )
{
case 0 : exit ( 0 );
break;
case 1 : insert ( head );
break;
case 2 : Delete ( head );
break;
case 3 : print ( head );
break;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值