C语言实现链表

链表的概念

                概念:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的 逻辑顺序
通过链表 中的指针链接次序实现的 。

        1.从上图可以看出链表的结构在逻辑上时连续的,在物理上是非连续的。

        2.现实中的结点一般都是从堆上申请出来的。

        3.从堆上申请的空间有一定的规则,两次申请的空间可能是连续的,也可能是非连续的。 

链表的分类

                实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:

1. 单向或者双向
        

2. 带头或者不带头

 

3. 循环或者非循环

 

虽然有这么多的链表的结构,但是我们实际中最常用还是两种结构:

 

1. 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的

子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。 

2. 带头双向循环链表: 结构最复杂 ,一般用在单独存储数据。实际中使用的链表数据结构都
是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多
优势,实现反而简单。

链表的实现 (无头单向非循环)

结点类型声明:

typedef int SLTDateType;
typedef struct SListNode
{
	SLTDateType data;
	struct SListNode* next;
}SListNode;

动态申请一个结点  

                malloc申请空间,将数据存入,下个结点的指针指向空即可;

// 动态申请一个节点
SListNode*  BuySListNode(SLTDateType x)
{
	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

单链表打印 

                访问完一个结点后用结点中next下个结点的指针找到下个结点依次访问即可;

// 单链表打印
void SListPrint(SListNode* plist)
{
	assert(plist);


	SListNode* phead = plist;
	while (phead)
	{
		printf("%d->", phead->data);
		phead = phead->next;
	}
	printf("NULL");
	return;
}

单链表尾插 

                !!!单链表的尾插在单链表本身为空的时候,会对传入的结构体的指针本身进行赋值,而函数中的指针只是对原指针的临时拷贝,因此要传入结构体的二级指针。

                首先动态开辟一个结点。若链表本身为空,则将开辟的结点的指针赋给链表的头结点指针;若链表不为空,则进行找尾,将开辟的结点指针与尾结点的next连接起来;

// 单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x)
{

	assert(pplist);

	SListNode* newnode = BuySListNode(x);

	//链表本身为空:
	if ((*pplist) == NULL)
	{
		*pplist = newnode;
	}

	//不为空;
	//找尾;
	else
	{
		SListNode* tail = *pplist;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		//插入;
		tail->next = newnode;

	}
	
}

单链表的头插

                !!!头插操作要对所给结构体指针进行赋值,因此要传入结构体的二级指针;

        动态开辟一个结点newnode,newnode的next指向原先链表的头结点指针,newnode作为新的链表的头结点。

// 单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x)
{
	SListNode* newnode = BuySListNode(x);
	newnode->next = *pplist;
	*pplist = newnode;
}

单链表的尾删

         !!!尾删操作可能要对所给结构体指针进行赋值,因此要传入结构体的二级指针;

        若只有一个结点释放结点空间,然后将链表的头结点指针置为空,若结点更多,则找尾结点的前一个结点,释放它的next(即尾结点的空间),再将next置为NULL;

// 单链表的尾删
void SListPopBack(SListNode** pplist)
{
	assert(pplist);
	//不能为空的链表;
	assert(*pplist != NULL);

	//1个结点;
	if ((*pplist)->next == NULL)
	{
		free(*pplist);
		*pplist = NULL;
	}
	//两个或者两个以上;
	
	//找尾的前一个;
	SListNode* tail = *pplist;
	while (tail->next->next != NULL)
	{
		tail = tail->next;
	}
	free(tail->next);
	tail->next = NULL;

}

单链表头删

                注意事项与上面几个同样。

储存头结点下个结点的指针,释放头结点的空间,下个结点成为新的头结点;

// 单链表头删
void SListPopFront(SListNode** pplist)
{
	assert(pplist);
	//链表不能没有结点;
	assert(*pplist);
	
	//储存头结点下个结点的指针;
	SListNode* cur = (*pplist)->next;
	//释放头结点的空间;
	free(*pplist);
	//头结点变为下个结点。
	*pplist = cur;
}

单链表查找 

        遍历链表进行查找即可;

// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
	assert(plist);

	SListNode* pos = plist;

	while (pos)
	{
		if (pos->data == x)
		{
			return pos;
		}
		pos = pos->next;
	}

	return NULL;

}

 单链表在pos位置之后插入x

        创建结点,然后插入到pos的后面即可;


// 单链表在pos位置之后插入x
void SListInsertAfter(SListNode* pos, SLTDateType x)
{
	assert(pos);
	SListNode* newnode=BuySListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

单链表删除pos位置之后的值

        释放空间后,再与后面的结点连接起来即可; 

// 单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos)
{

	assert(pos);
	//pos的下个结点不为空;
	assert(pos->next != NULL);

	//记录下pos下个结点的下个结点的指针;防止释放下个结点的空间后找不到;
	SListNode* cur = pos->next->next;
	free(pos->next);

	pos->next = cur;
}

单链表的销毁

        记录下头结点的位置,头节点变为下个结点,释放头节点前一个结点的空间。

// 单链表的销毁
void SListDestroy(SListNode* plist)
{
	assert(plist);

	while (plist)
	{
		//记录下头结点的位置;
		SListNode* cur = plist;
		//头节点变为下个结点;
		plist = plist->next;
		//释放头节点前一个结点的空间;
		free(cur);
	}
	plist = NULL;

}

链表的实现(双向带头链表的实现

类型结点声明:
        

typedef int listdate;
typedef struct listnode
{
	    struct listnode* prev;
		struct listnode* next;
		listdate date;

}listnode;

                里面包含一个数据,前后两个指针;

创建返回链表的头结点.

        开辟一个头结点的空间,然后使它的首位都指向自己即可;

listnode* ListCreate()
{
	listnode* head = (listnode*)malloc(sizeof(listnode));
	if (head == NULL)
	{
		perror("malloc");
		exit(-1);
	}

	head->next = head;
	head->prev = head;
	return head;
}

创建链表的非头节点; 

        开辟一个结点的空间,存入数值即可;

listnode* newnodeCreate(listdate x)
{
	listnode* newnode = (listnode*)malloc(sizeof(listnode));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	newnode->date = x;
	return newnode;
}

双向链表打印

        遍历一遍链表打印,因为头结点不储存数据,因此从头结点的下个结点打印即可;因为此时链表的首位是相连的,只要等链表遍历回到头结点的位置停止即可;

void ListPrint(listnode* pHead)
{
	assert(pHead);
	printf("head");
	listnode* cur = pHead->next;
	while (cur != pHead)
	{
		printf("<-%d->", cur->date);
		cur = cur->next;
	}
}

双向链表的尾插;

        首先动态开辟一个结点,再找到尾结点,然后连接起来,作为新的尾结点,然后使新的尾结点与头结点连接起开即可;

void ListPushBack(listnode* pHead, listdate x)
{
	assert(pHead);
	listnode* newnode = newnodeCreate(x);

	//找尾;
	listnode* tail = pHead->prev;

	tail->next = newnode;
	newnode->prev = tail;

	newnode->next = pHead;
	pHead->prev = newnode;

}

 双向链表的尾删;

                首先找到尾结点,再找到尾结点的前一个结点作为新的尾结点,释放尾结点的空间,将新的尾结点与头结点连接起来;

void ListPopBack(listnode* pHead)
{
	assert(pHead);

	//找尾;
	listnode* tail = pHead->prev;
	//找尾的前一个结点;
	listnode* prevtail = tail->prev;
	//释放尾结点;
	free(tail);

	//连接新的尾结点与头结点;
	prevtail->next = pHead;
	pHead->prev = prevtail;

}

双向链表的头增;

        首先创建空间,然后连接新的结点与头结点后的下一个结点,最后新的结点与头结点连接起来即可;

void ListPushFront(listnode* pHead, listdate x)
{
	assert(pHead);
	//创建空间;
	listnode* newnode = newnodeCreate(x);

	//新的结点与头结点后的下一个结点连接;
	newnode->next = pHead->next;
	pHead->next->prev = newnode;

	//新的结点与头结点连接起来;
	pHead->next = newnode;
	newnode->prev = pHead;

}

双向链表的头删;

        首先找到头结点后的第二个结点,再释放头结点后的第一个结点,最后连接头结点与头结点后的第二个结点;


void ListPopFront(listnode* pHead)
{

	assert(pHead);
	//找到头结点后的第二个结点;
	listnode* headnext = pHead->next->next;

	//释放头结点后的第一个结点;
	free(pHead->next);

	//连接头结点与头结点后的第二个结点
	pHead->next = headnext;
	headnext->prev = pHead;

}

双向链表的查找;

                遍历一遍链表查找即可,与打印的过程基本类似;

listnode* ListFind(listnode* pHead, listdate x)
{
	assert(pHead);
	listnode* cur = pHead->next;
	while (cur != pHead)
	{
		if (cur->date == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

双向链表在pos的前面进行插入  

        首先创建结点的空间,再找到pos的前一个结点,然后将pos与新的结点连接起来,最后将pos的前一个结点与新结点连接起来;

void ListInsert(listnode* pos, listdate x)
{
	assert(pos);

	//创建空间;
	listnode* newnode = newnodeCreate(x);

	//找到pos的前一个结点;
	listnode* posprev = pos->prev;

	//将pos与新的结点连接起来;
	newnode->next = pos;
	pos->prev = newnode;

	//将pos的前一个结点与新结点连接起来;
	posprev->next = newnode;
	newnode->prev = posprev;

}

双向链表删除pos位置的结点  

        连接pos的首尾,再释放pos即可;

void ListErase(listnode* pos)
{
	assert(pos);
	
	//连接pos的首尾
	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;

	free(pos);

}

顺序表和链表的区别

        我们用两幅图来总结它们之间的区别;

 

 

 

 

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
以下是C语言实现链表归并的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表结构体 struct ListNode { int val; struct ListNode *next; }; // 合并两个有序链表 struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { // 定义哨兵节点 struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode)); dummy->next = NULL; struct ListNode *cur = dummy; // 遍历两个链表,将较小的节点加入新链表中 while (l1 && l2) { if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; } else { cur->next = l2; l2 = l2->next; } cur = cur->next; } // 将剩余的节点加入新链表中 if (l1) { cur->next = l1; } else { cur->next = l2; } return dummy->next; } // 归并排序 struct ListNode* sortList(struct ListNode* head) { if (!head || !head->next) { return head; } // 快慢指针找到链表中点 struct ListNode *slow = head, *fast = head->next; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } // 分割链表 struct ListNode *mid = slow->next; slow->next = NULL; // 递归排序左右两个链表 struct ListNode *left = sortList(head); struct ListNode *right = sortList(mid); // 合并两个有序链表 return mergeTwoLists(left, right); } // 创建链表 struct ListNode* createList(int arr[], int n) { if (n == 0) { return NULL; } struct ListNode *head = (struct ListNode *)malloc(sizeof(struct ListNode)); head->val = arr[0]; head->next = NULL; struct ListNode *cur = head; for (int i = 1; i < n; i++) { struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode)); node->val = arr[i]; node->next = NULL; cur->next = node; cur = cur->next; } return head; } // 打印链表 void printList(struct ListNode *head) { while (head) { printf("%d ", head->val); head = head->next; } printf("\n"); } int main() { int arr[] = {4, 2, 1, 3}; int n = sizeof(arr) / sizeof(int); struct ListNode *head = createList(arr, n); printf("原链表:"); printList(head); head = sortList(head); printf("排序后的链表:"); printList(head); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

(int*) nightfall

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

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

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

打赏作者

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

抵扣说明:

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

余额充值