链表及其相关操作(C语言)

创建结构体

  • 定义结点的结构
  • 创建一个有哨兵结点的一个链表
typedef struct NODE{
		int calue;
		NODE*nextPtr;
	}storeNODE;
typedef struct LIST{
		storeNODE*head;
		storeNODE*tail;
	}storeLIST;
  • 创建一个有空结点的链表
void add_node(storeLIST*index)
{
	int num;
	scanf("%d",&num);
	index->head=(storeNODE*)malloc(sizeof(storeNODE));
	index->head->nextPtr=NULL;
	index->tail=index->head;
	while(num!=-1)
	{
		storeNODE*last=index->tail;
		storeNODE*p;
		p=(storeNODE*)malloc(sizeof(storeNODE));
		p->value=num;
		p->nextPtr=NULL;
		last->nextPtr=p;
		index->tail=p;
		scanf("%d",&num);
	}
}
  • 打印有空结点的链表
void print_node(storeLIST*index)
{
	storeNODE*p;
	for(p=index->head->nextPtr;p;p=p->nextPtr)
		printf("%d",p->value);
}
  • 修正链表的尾指针
void mend_Ptr_node(storeLIST*index)
{
	storeNODE*p;
	for(p=index->head->nextPtr;p;p=p->nextPtr)
		index->tail=p;
  • 创建一个空链表
void creat_vacant_node(storeLIST*index)
{
	index->head=(storeNODE*)malloc(sizeof(storeNODE));
	index->head->nextPtr=NULL;
	index->tail=index->head;
}
  • 判断链表是否为空,不空返回1
int judge_ifvacant_node(storeLIST*index)
{
	return (index->head->nextPtr!=NULL);
}
  • 释放链表的所有链接
void over_node(storeLIST*index)
{
	storeNODE*p,*q;
	for(p=index->head;p;p=q)
	{
		q=p->nextPtr;
		free(p);
	}
}
  • 给链表冒泡排序(从小到大)
void bubblesort_node(storeLIST*index)
{/*算法同数组的冒泡算法*/
	storeNODE*pfirst=NULL,storeNODE*pend=NULL;
	int temp;
	pfirst=index->head;
	while(pfirst!=NULL)
	{
		while(pfirst->nextPtr!=NULL)
		{
			if(pfirst->value<pfirst->nextPtr->value)
			{
				temp=pfirst->value;
				pfirst->value=pfirst->nextPtr->value;
				pfirst->nextPtr->value=temp;
			}
			pfirst=pfirst->nextPtr;
		}
	pend=pfirst;
	pfirst=pfirst->nextPtr;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值