一道习题:链表的创建、输出、删除、插入

要求:多个学生,每个学生有学号和成绩,用链表处理:创建、输出、删除、插入,其中,插入结点之后,按照学号的升序排列;

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

static int TOTALSTUDENTS;

struct student 
{
	long  student_number;
	float student_score;
	struct student *next;
};


/*******************************************************************************************
** 函数名称: CreatLink()                                         
** 作    者:  zyh                                           
** 版    本: 1.0.0                                            
** 日    期:  2011-7-8                                              
** 功能描述:  创建链表                                        
** 被本函数调用的函数清单:     
** 调用本函数的函数清单:                
** 被访问的表:     
** 被修改的表 :                                    
** 输           入:   无
** 输           出:   无
** 返     回    值:   创建链表的头指针   
** 其           他:     
** 修  改   历  史:                        
                                                            
		1. 修改时间:
			修  改  者:
			版        本:
			修改简述:
        2. ......
*********************************************************************************************/

struct student *CreatLink(void)
{
	struct student *ptr_new;  /* 指向新开辟的结点 */
	struct student *ptr_last; /* 指向链表的最后一个元素 */
	struct student *head;     /* 链表的头指针 */

	TOTALSTUDENTS = 0;        /* 节点数初始化 */

	ptr_new = ptr_last = (struct student *)malloc(sizeof(struct student));   /* 开辟一个新的结点 */
	scanf("%ld,%f",&ptr_new->student_number,&ptr_new->student_score);
	head = NULL;         /* 初始化为空链表 */

	while (ptr_new->student_number != 0)
	{
		TOTALSTUDENTS++;   /* 结点数增加 */
		if (TOTALSTUDENTS == 1)  /* 如果开辟的是第一个结点,则将这个结点作为头结点 */
		{
			head = ptr_new;
		}
		else
		{
			ptr_last->next = ptr_new;  /* 将新开辟的结点连接在最后一个结点之后 */
		}
		ptr_last = ptr_new;  /* 最后一个指针位置后移 */

		ptr_new = (struct student *)malloc(sizeof(struct student));
		scanf("%ld,%f",&ptr_new->student_number,&ptr_new->student_score);
		ptr_new->next = NULL;  /* 养成一个好习惯 */

	}
	ptr_last->next = NULL; 
	return(head);

}


/*******************************************************************************************
** 函数名称: PrintLink()                                         
** 作    者:  zyh                                           
** 版    本: 1.0.0                                            
** 日    期:  2011-7-8                                              
** 功能描述:  打印链表中的节点数据                                        
** 被本函数调用的函数清单:     
** 调用本函数的函数清单:                
** 被访问的表:     
** 被修改的表 :                                    
** 输           入:   待打印的链表的头指针
** 输           出:   无
** 返     回    值:   无   
** 其           他:     
** 修  改   历  史:                        
                                                            
		1. 修改时间:
			修  改  者:
			版        本:
			修改简述:
        2. ......
*********************************************************************************************/

void PrintLink(struct student *head)
{
	struct student *p;  /* 用来控制待打印的结点位置 */
	p = head;  /* 刚开始的时候指向头结点 */

	printf("====== There are  %d  records ======\n",TOTALSTUDENTS);
	
	if (head != NULL)  /* 不是空链表 */
	{
		while (p != NULL)
		{
			printf("%ld %5.1f\n",p->student_number,p->student_score);
			p = p->next;
		}
	}

}


/*******************************************************************************************
** 函数名称: DeleteLink()                                         
** 作    者:  zyh                                           
** 版    本: 1.0.0                                            
** 日    期:  2011-7-8                                              
** 功能描述:   删除链表中指定的结点                                      
** 被本函数调用的函数清单:     
** 调用本函数的函数清单:                
** 被访问的表:     
** 被修改的表 :                                    
** 输           入:   1. 待删除链表的头指针
                      2.待删除的结点的关键字
** 输           出:   无
** 返     回    值:   删除节点后的链表的头指针   
** 其           他:     
** 修  改   历  史:                        
                                                            
		1. 修改时间:
			修  改  者:
			版        本:
			修改简述:
        2. ......
*********************************************************************************************/

struct student *DeleteLink(struct student *head, long key)
{

	struct student *ptr_curr;  /* 指向当前结点 */
	struct student *ptr_prev;  /* 指向当前结点的上一个结点 */

	if (head == NULL)
	{
		printf("\n List Null!\n");
		exit(0);
	}

	ptr_curr = head;  /* 当前指针指向头结点 */
	
	while (key != ptr_curr->student_number && ptr_curr->next != NULL)  /* 指向的不是所要的结点,并且不是尾结点 */
	{
		ptr_prev = ptr_curr;
		ptr_curr = ptr_curr->next;   /* 指针后移,一直到找到指定结点位置 */
	}

	if (key == ptr_curr->student_number)  /* 结点找到了 */
	{
		if (ptr_curr == head)   /* 如果是头结点,就要删除头结点 */
		{
			head = ptr_curr->next;
		    TOTALSTUDENTS--;              /* 节点数减1 */
			printf("Delete:%ld\n",key);   /* 删除成功,给出信息 */
		}
		else
		{
			ptr_prev->next = ptr_curr->next;
			TOTALSTUDENTS--;              /* 节点数减1 */
			printf("Delete:%ld\n",key);   /* 删除成功,给出信息 */
		}
	}
	else
	{
		printf("%ld not been found!\n",key);
	}

	return head;  /* 给出删除成功后的链表的头结点指针 */
}


/*******************************************************************************************
** 函数名称: InsertLink()                                         
** 作    者:  zyh                                           
** 版    本: 1.0.0                                            
** 日    期:  2011-7-8                                              
** 功能描述:   插入结点(插到当前指针所指的前一个结点),并按照一定方式排序                                      
** 被本函数调用的函数清单:     
** 调用本函数的函数清单:                
** 被访问的表:     
** 被修改的表 :                                    
** 输           入:   1. 待插入链表的头指针
                      2. 待插入的结点
** 输           出:   无
** 返     回    值:   插入节点后的链表的头指针   
** 其           他:     
** 修  改   历  史:                        
                                                            
		1. 修改时间:
			修  改  者:
			版        本:
			修改简述:
        2. ......
*********************************************************************************************/
struct student *InsertLink(struct student *head, struct student *insert_node)
{
	struct student *ptr_curr;  /* 当前结点 */
	struct student *ptr_prev;  /* 当前结点的上一个结点 */
	struct student *ptr_insert; /* 指向待插入的结点 */

	ptr_curr = head;
	ptr_insert = insert_node;

	if (head == NULL)   /* 如果是空链表,则把插入的结点当做头结点 */
	{
		head = ptr_curr;
		ptr_curr->next = NULL;
	}
	else  /* 不是空链表 */
	{
		/* 如果找不到结点,就一直往后移动,一直到找到该节点为止 */
		while ((ptr_insert->student_number > ptr_curr->student_number) && (ptr_curr->next != NULL))
		{
			ptr_prev = ptr_curr;
			ptr_curr = ptr_curr->next;
		} 
		
		if (ptr_insert->student_number <= ptr_curr->student_number)  /* 结点找到了 */
		{
			if (ptr_curr == head)   /* 如果找到的是头结点,插在头结点的前面 */
			{
				head = ptr_insert;
				ptr_insert->next = ptr_curr;
			}
			else                    /* 插在当前结点的前面 */
			{
				ptr_prev->next = ptr_insert;
				ptr_insert->next = ptr_curr;
			}
		}
		else  /* 结点在链表中没有找到,只能插在链表尾 */
		{
			ptr_curr->next = ptr_insert;
			ptr_insert->next = NULL;
		}
	}

	TOTALSTUDENTS++;  /* 成功插入之后,结点数加1 */
	return head;   /* 返回新的链表的头指针 */

}


 

#include "chapter.h"

void main()
{
	struct student *head;        /* 指向链表头 */
	struct student *insert_node; /* 指向待插入结点 */
	long delete_number;          /* 链表中待删除结点的关键字 */

	/* 创建、输出链表 */
	printf("\nPlease input records:\n");
	head = CreatLink();
	PrintLink(head);

	/* 删除指定结点,直到输入的关键字为0 */
	printf("\nPlease in put the DELETE number:\n");
	scanf("%ld",&delete_number);
	while (delete_number != 0)
	{
		head = DeleteLink(head,delete_number);
		PrintLink(head);
		printf("\nPlease in put the DELETE number:\n");
	    scanf("%ld",&delete_number);
	}

	/* 插入结点,直到插入的关键字为0*/
	printf("\nPlease in put the INSERT record:\n");
	insert_node = (struct student*)malloc(sizeof(struct student));
	scanf("%ld,%f",&insert_node->student_number,&insert_node->student_score);
	while (insert_node->student_number != 0)
	{
		head = InsertLink(head,insert_node);
		PrintLink(head);
		printf("\nPlease in put the INSERT record:\n");
     	insert_node = (struct student*)malloc(sizeof(struct student));
    	scanf("%ld,%f",&insert_node->student_number,&insert_node->student_score);
	}

}


/*

  
Please input records:
1000,99
1001,98
1002,95
1003,94
1004,93
1005,97
0,0
====== There are  6  records ======
1000  99.0
1001  98.0
1002  95.0
1003  94.0
1004  93.0
1005  97.0

Please in put the DELETE number:
1000
Delete:1000
====== There are  5  records ======
1001  98.0
1002  95.0
1003  94.0
1004  93.0
1005  97.0

Please in put the DELETE number:
1004
Delete:1004
====== There are  4  records ======
1001  98.0
1002  95.0
1003  94.0
1005  97.0

Please in put the DELETE number:
0

Please in put the INSERT record:
1008,98
====== There are  5  records ======
1001  98.0
1002  95.0
1003  94.0
1005  97.0
1008  98.0

Please in put the INSERT record:
1009,99
====== There are  6  records ======
1001  98.0
1002  95.0
1003  94.0
1005  97.0
1008  98.0
1009  99.0

Please in put the INSERT record:
1000,94
====== There are  7  records ======
1000  94.0
1001  98.0
1002  95.0
1003  94.0
1005  97.0
1008  98.0
1009  99.0

Please in put the INSERT record:
1011,93
====== There are  8  records ======
1000  94.0
1001  98.0
1002  95.0
1003  94.0
1005  97.0
1008  98.0
1009  99.0
1011  93.0

Please in put the INSERT record:
1004,92
====== There are  9  records ======
1000  94.0
1001  98.0
1002  95.0
1003  94.0
1004  92.0
1005  97.0
1008  98.0
1009  99.0
1011  93.0

Please in put the INSERT record:
0,0
Press any key to continue

*/


 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值