C编程——单向链表

C编程——单向链表

主要概念:

​ 单向链表数据结构及功能图示:
01_single_list

实验目的:

​ 实现一个单向链表,并且实现添加、删除、打印的功能。

主要函数:

node * create_list(void);								//创建链表头
node * create_node(student *per_info);					//创建节点
void print_list(node *phead);							//打印链表
void insert_by_head(node *phead, student *per_info);	//从头部插入节点
void delete_node_by_id(node *phead, int num);			//通过id删除一个节点

代码:

//single_list.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct student{
	char name[20];					
	int id;
	int score;
} student;

typedef struct node{
	student data;
	struct node *pnext;
}node;

node * create_list()
{
	node *phead = (node *)malloc(sizeof(node));
	
	if(NULL == phead)
	{
		printf("malloc error!\n");
		return NULL;
	}
	memset(phead, 0, sizeof(node));
	phead->pnext = NULL;
	
	return phead;
}

node * create_node(student *per_info)
{
	node *newnode = (node *)malloc(sizeof(node));
	
	if (NULL == newnode)
	{
		printf("malloc error!\n");
		return NULL;
	}
	memset(newnode, 0, sizeof(node));
	newnode->data = *per_info;
	newnode->pnext = NULL;
	
	return newnode;
}

void print_list(node *phead)
{
	node *p = phead;
	
	if(p->pnext == NULL)
		printf("print error:the linked list is empty!\n");
	else {
		//printf("姓名\t序号\t分数:\n");
		while(p->pnext != NULL)
		{
			p = p->pnext;
			printf(" %s\t\t%d\t\t%d\n", p->data.name, p->data.id, p->data.score);
		}
	}
}

void insert_by_head(node *phead, student *per_info)
{
	node *newnode = create_node(per_info);
	if(phead->pnext == NULL)
	{
		phead->pnext = newnode;
		newnode->pnext = NULL;
	} else {
		newnode->pnext = phead->pnext;		//注意这两句顺序不能反!
		phead->pnext = newnode;
	}
	//printf("Insert %s to head successfully!\n", newnode->data.name);
}

void delete_node_by_id(node *phead, int num)			
{
	node *pmov = phead->pnext;
	node *pmov_front = phead;
	
	//1.判断是否为空链表
	if(NULL == pmov)
	{
		printf("delete error:the linked list is empty!\n");
	} else {
		while (pmov->data.id != num)
		{
			if(NULL == pmov->pnext)
			{
				printf("error:cannot find this node.please confirm the data's validity!\n");
				return;
			}
			pmov_front = pmov;
			pmov = pmov->pnext;
		}
		pmov_front->pnext = pmov->pnext;
		free(pmov);
	}
}

int main()
{
	node *ph = create_list();
	student init_struc = {{'\0'}, 0, 0};
	student *stu = &init_struc;
	char choice = 0;
	
	while(1)
	{	
		printf("请依次输入学生的 姓名\t序号\t分数: ");
		scanf("%s %d %d", stu->name, &(stu->id), &(stu->score));
		insert_by_head(ph, stu);

		printf("continue?(Y/N)\n");
		setbuf(stdin,NULL);
		choice = getchar();
		if(choice == 'N' || choice == 'n')
			break;
		choice = 0;
	}
	printf("姓名\t\t序号\t\t分数:\n");	
	print_list(ph);
	//delete_node_by_id(ph, 2);
	//print_list(ph);

	return 0;
}

编译运行:

sixer@ubuntu:~/imx_usr/sys_app/test$ gcc -o single_list single_list.csixer@ubuntu:~/imx_usr/sys_app/test$ ./single_list `

实验现象:

请依次输入学生的 姓名 序号 分数: sixer 01 98
continue?(Y/N)
y
请依次输入学生的 姓名 序号 分数: zac 20 76
continue?(Y/N)
n
姓名 序号 分数:
zac 20 76
sixer 1 98

总结:

​ 整体完成了单项链表的基本功能。后续还可以添加搜索,修改等功能,且链表节点数据student结构可用指针替代,写成一个通用的单项链表功能模块。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式小鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值