数据结构(C语言)-单向链表

本文介绍了一种使用C语言实现单向链表的方法,并提供了多种实用功能,包括初始化、销毁、插入、删除等操作。同时展示了如何通过具体实例进行链表的遍历和元素的获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C语言的单向链表,就是在一个将一些数据放在一个结构体里,然后在结构体里加 struct xxx * next 的成员,用于指向下一结点。

引用时,创建一个临时的结构体变量进行引用。

如原结构体变量为 struct xxx *p , 则 可创建 struct xxx * temp,  然后 for (temp = p; temp->next != NULL;  temp = temp->next) 进行遍历,引用。

下面为个人所写的单向链表的一些功能实现。

# include <stdlib.h>

struct Student
{
	int id;
	struct Student * next;
};

void menu()
{
	struct Student * InitList();
	int DestroyList( struct Student *p);
	int ClearList( struct Student *p);
	int ListEmpty( struct Student *p);
	int ListLength( struct Student *p);
	struct Student * GetElem(int i, struct Studeent *p);
	struct Student * PriorElem(struct Student *p, int i);
	struct Student * NextElem(struct Student *p, int i);
	int ListInsert(struct Student *p, int i, int j);   //在 id 为 i 后插入, id 为 j
	int ListCat(struct Student *p, int i);       //在末尾添加
	int ListDelete(struct Student *p, int i);
	struct Student * ListCopy(struct Student *p);
	struct Student * ListCombOrder(struct Student *p, struct Student *q);   //链表为顺序结构
	struct Student * ListCombDisorder( struct Student *p, struct Student *q);  //链表为非顺序结构
	struct Student ** ListDivOrder(struct Student * p, int i);      //以 id 为 i 分割,第二条从 id 为 i 开始
	struct Student ** ListDivDisorder(struct Student *p, int n);    //第一条链表长度为 n-1
}

struct Student * InitList()
{
	struct Student * p;
	p = (struct Student *) malloc(sizeof(struct Student));
	p->next = NULL;
	return p;
}

int DestoryList( struct Student *p)
{
	free(p);
	return 1;   //返回1表示操作成功
}

int ClearList( struct Student *p)
{
	p->next = NULL;
	return 1;
}

int ListEmpty( struct Student *p)
{
	if (p->next == NULL)
		return 0;
	else return 1;
}

int ListLength( struct Student *p)
{
	int length=0;
	struct Student * tempstu;
	for (tempstu = p; tempstu->next != NULL; tempstu = tempstu->next )
		length++;
	return length;
}

struct Student * GeteElem(int i, struct Student *p)
{
	struct Student * tempstu;
	for (tempstu = p; tempstu->id != i; tempstu = tempstu->next)
		if (tempstu->next == NULL)
			break;
	if ( tempstu->id == i )
		return tempstu;
	else return NULL;
}

struct Student * PriorElem(struct Student *p, int i)
{
	struct Student * tempstu, * ptempstu;
	ptempstu = p;
	tempstu = p->next;
	for (; tempstu->id != i; tempstu = tempstu->next )
	{
		if (tempstu->next == NULL)
			break;
		ptempstu = ptempstu->next;
	}
	
	if (tempstu->id == i)
		return ptempstu;
	else return NULL;
}

struct Student * NextElem(struct Student *p, int i)
{
	struct Student * tempstu;
	for (tempstu = p; tempstu->id != i; tempstu = tempstu->next )
		if (tempstu->next == NULL)
			break;

	if (tempstu->id == i)
		return tempstu->next;
	else return NULL;
}

int ListInsert(struct Student *p, int i, int j)
{
	struct Student * beginstu;
	for (beginstu = p; p->id != i; p = p->next );
	struct Student *q;
	q = p;
	q = q->next;
	q->id = j;
	q->next = p->next;
	p = beginstu;
	return 1;
}

int ListCat(struct Student *p, int i)
{
	int ListLength(struct Student *p);
	int n = ListLength(p);
	struct Student * tempstu = p;

	for (int j = 0; j < n; j++)
		tempstu = tempstu->next;
	tempstu->id = i;
	tempstu->next = NULL;
	return 1;
}

int ListDelete(struct Student *p, int i)
{
	struct Student * tempstu;
	for ( tempstu = p; tempstu->id != i; tempstu = tempstu->next );
	tempstu = tempstu->next->next;
	return 1;
}

struct Student * ListCopy(struct Student *p)
{
	struct Student *tempstu, *q;
	tempstu = p;
	for (q = p; p->next != NULL; p = p->next )
	{
		q->id = p->id;
		q = q->next;
	}
	p = tempstu;
	return q;
}

struct Student * ListCombOrder(struct Student *p, struct Student *q)   //链表为顺序结构
{
	struct Student * TComb, *Comb;
	TComb = Comb;
	struct Student *pComb, *qComb;
	pComb = p;  qComb = q;

	while( p->next == NULL || q->next == NULL)
		if (p->id > q->id)
		{
			TComb->id = p->id;
			TComb = TComb->next;
			p = p->next;
		}
		else
		{
			TComb->id = q->id;
			TComb = TComb->next;
			q = q->next;
		}

	if (p->next != NULL)
		for (; p->next != NULL; p = p->next )
		{
			TComb->id = p->id;
			TComb = TComb->next;
		}
	else 
		for (; q->next != NULL; q = q->next )
		{
			TComb->id = p->id;
			TComb = TComb->next;
		}
	TComb->next = NULL;

	p = pComb;
	q = qComb;
	return Comb;
}

struct Student * ListCombDisorder( struct Student *p, struct Student *q)    //链表为非顺序结构
{
	struct Student * Comb, * TComb;
	TComb = Comb;
	struct Student * pComb, * qComb;
	pComb = p;   qComb = q;

	for (; p->next != NULL; p = p->next)
	{
		TComb->id = p->id;
		TComb = TComb->next;
	}

	TComb->id = p->id;
	TComb->next = q;

	for (; q->next != NULL; q = q->next)
	{
		TComb->id = q->id;
		TComb = TComb->next;
	}
	TComb->next = NULL;

	return Comb;
}


struct Student ** ListDivOrder(struct Student * p, int i)            //链表为顺序结构
{ 
	struct Student * stu[2];
	struct Student * tempstu;
	stu[0] = p;
	for ( tempstu = stu[0]; tempstu->id != i; tempstu = tempstu->next );
	stu[1] = tempstu;
	tempstu->next = NULL;
	return stu;
}

struct Student ** ListDivDisorder(struct Student *p, int n)          //链表为非顺序结构
{
	struct Student *stu[2];
	struct Student * tempstu;
	int count = 0;
	stu[0] = p;
	for (tempstu = stu[0]; count < n; count++ )
		tempstu = tempstu->next;
	stu[1] = tempstu;
	tempstu->next = NULL;
	return stu;
}

(以上功能是我所能想到的所有需要的功能,若有遗漏还请提出)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值