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;
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言链表实现设备管理系统的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 设备结构体 typedef struct Device { char name[20]; // 设备名称 int id; // 设备编号 struct Device *next; // 指向下一个设备的指针 } Device; // 创建设备链表 Device *createDeviceList() { Device *head = (Device *)malloc(sizeof(Device)); // 创建头结点 head->next = NULL; return head; } // 添加设备 void addDevice(Device *head, char *name, int id) { Device *newDevice = (Device *)malloc(sizeof(Device)); // 创建新设备结点 strcpy(newDevice->name, name); newDevice->id = id; newDevice->next = NULL; Device *p = head; while (p->next != NULL) { p = p->next; } p->next = newDevice; } // 删除设备 void deleteDevice(Device *head, int id) { Device *p = head->next; Device *pre = head; while (p != NULL) { if (p->id == id) { pre->next = p->next; free(p); return; } pre = p; p = p->next; } printf("设备编号为%d的设备不存在\n", id); } // 修改设备 void modifyDevice(Device *head, int id, char *newName) { Device *p = head->next; while (p != NULL) { if (p->id == id) { strcpy(p->name, newName); return; } p = p->next; } printf("设备编号为%d的设备不存在\n", id); } // 查找设备 void searchDevice(Device *head, int id) { Device *p = head->next; while (p != NULL) { if (p->id == id) { printf("设备编号:%d,设备名称:%s\n", p->id, p->name); return; } p = p->next; } printf("设备编号为%d的设备不存在\n", id); } // 输出设备列表 void printDeviceList(Device *head) { Device *p = head->next; while (p != NULL) { printf("设备编号:%d,设备名称:%s\n", p->id, p->name); p = p->next; } } // 清空设备列表 void clearDeviceList(Device *head) { Device *p = head->next; while (p != NULL) { Device *temp = p; p = p->next; free(temp); } head->next = NULL; } int main() { Device *head = createDeviceList(); // 创建设备链表 // 添加设备 addDevice(head, "设备1", 1); addDevice(head, "设备2", 2); addDevice(head, "设备3", 3); // 输出设备列表 printf("设备列表:\n"); printDeviceList(head); // 修改设备 modifyDevice(head, 2, "修改后的设备2"); printf("修改后的设备列表:\n"); printDeviceList(head); // 删除设备 deleteDevice(head, 3); printf("删除设备后的设备列表:\n"); printDeviceList(head); // 查找设备 searchDevice(head, 2); // 清空设备列表 clearDeviceList(head); printf("清空设备列表后的设备列表:\n"); printDeviceList(head); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值