带头结点单链表的C语言代码实现

测试代码仅测试插入,删除请自行测试

head_list.h

typedef int ElemType;

typedef struct Node {
	ElemType data; 
	struct Node *next;
}HeadList;

void InitHeadList(HeadList* head);

int GetLength(HeadList* head);
void ShowList(HeadList* head);

bool InsertOFPos(HeadList* head, ElemType val, int pos);
bool InsertOFHead(HeadList* head, ElemType val);
bool InsertOFTail(HeadList* head, ElemType val);

bool DeleteOFPos(HeadList* head, int pos);
bool DeleteOFHead(HeadList* head);
bool DeleteOFTail(HeadList* head);
bool DeleteOFValue(HeadList* head, ElemType val);

void ClearHeadList(HeadList* head);
void DestroyHeadList(HeadList* head);

HeadList* HeadListQueryByVal(HeadList* head, ElemType val);

head_list.c

#include "head_list.h"
#include <stdlib.h>
#include <stdio.h>

static HeadList* ApplyNode(ElemType val, HeadList* ht)
{
	HeadList* s = (HeadList*)malloc(sizeof(HeadList));
	if (s == NULL) return NULL;

	s->data = val;
	s->next = ht;
}

void InitHeadList(HeadList* head) 
{ 
	if (head == NULL) exit(0);
	head->next = NULL; 
}

bool InsertOFPos(HeadList* head, ElemType val, int pos) 
{ 
	if (head == NULL) exit(0); 
	if (pos < 0) return false; 
	HeadList* p = head; 
	while (pos && p != NULL) 
	{ 
		pos--; p = p->next; 
	}
	if (p == NULL) return false; 
	HeadList* newNode = ApplyNode(val, p->next); 
	if (newNode == NULL) return false; 
	p->next = newNode; return true; 
}
bool InsertOFHead(HeadList* head, ElemType val) 
{ 
	if (head == NULL) exit(0); 
	return InsertOFPos(head, val, 0); 
}
bool InsertOFTail(HeadList* head, ElemType val) 
{ 
	if (head == NULL) exit(0);
	HeadList* p = head; 
	while (p->next != NULL) p = p->next; 
	p->next = ApplyNode(val, NULL); 
	return true; 
}

int GetLength(HeadList* head) 
{  
	if (head == NULL) exit(0); 
	int length = 0; 
	HeadList * p = head->next; 
	while (p != NULL) 
	{ 
		length++; 
		p = p->next; 
	} 
	return length; 
} 
void ShowList(HeadList * head) 
{ 
	if (head == NULL) exit(0); 
	HeadList * p = head->next; 
	while (p != NULL) 
	{ 
		printf("%d ", p->data); 
		p = p->next; 
	} 
	printf("\n"); 
}

bool DeleteOFPos(HeadList* head, int pos) 
{ 
	if (head == NULL) exit(0); 
	HeadList * p = head; 
	while (pos && p->next != NULL) 
	{ 
		pos--; 
		p = p->next; 
	} 
	if (p->next == NULL) return false; 
	HeadList * q = p->next; 
	p->next = q->next; 
	free(q); 
	return true; 
} 
bool DeleteOFHead(HeadList * head) 
{ 
	if (head == NULL) exit(0); 
	return DeleteOFPos(head, 0); 
} 
bool DeleteOFTail(HeadList * head) 
{ 
	if (head == NULL) exit(0); 
	if (head->next == NULL) return false; 
	HeadList * p = head, *q = head->next; 
	while (q->next != NULL) 
	{ 
		p = q; 
		q = q->next; 
	}
	p->next = NULL; 
	free(q); 
	return true;
}
bool DeleteOFValue(HeadList* head, ElemType val) 
{ 
	if (head == NULL) exit(0); 
	HeadList* p = head, * q = head->next; 
	while (q != NULL) 
	{ 
		if (q->data == val) 
		{ 
			p->next = q->next; 
			free(q); 
			q = p->next; 
		} 
		else 
		{ 
			p = q;
			q = q->next;
		} 
	}
	return true; 
}
void ClearHeadList(HeadList* head) 
{ 
	if (head == NULL) exit(0); 
	while (head->next != NULL) 
		DeleteOFHead(head); 
}
void DestroyHeadList(HeadList* head) 
{ 
	if (head == NULL) exit(0); 
	ClearHeadList(head); 
}

HeadList* HeadListQueryByVal(HeadList* head, ElemType val)
{
	if (head == NULL) exit(0);
	while (head->next != NULL)
	{
		if (head->next->data == val)
			return head->next;
		else head = head->next;
	}
	return NULL;
}

test_main.c

#include "head_list.h"

void InsertOFPosTest()
{
	HeadList head;
	InitHeadList(&head);

	InsertOFPos(&head, 1, 0);
	InsertOFPos(&head, 2, 1);
	InsertOFPos(&head, 3, 2);
	InsertOFPos(&head, 4, 3);
	InsertOFPos(&head, 5, 4);
	InsertOFPos(&head, 6, 5);

	ShowList(&head);
}



void InsertOFHeadTest() 
{
	HeadList head;
	InitHeadList(&head);

	InsertOFHead(&head, 1);
	InsertOFHead(&head, 2);
	InsertOFHead(&head, 3);
	InsertOFHead(&head, 4);
	InsertOFHead(&head, 5);
	InsertOFHead(&head, 6);

	ShowList(&head);
}

void InsertOFTailTest()
{
	HeadList head;
	InitHeadList(&head);

	InsertOFTail(&head, 1);
	InsertOFTail(&head, 2);
	InsertOFTail(&head, 3);
	InsertOFTail(&head, 4);
	InsertOFTail(&head, 5);
	InsertOFTail(&head, 6);

	ShowList(&head);
}

int main() 
{
	InsertOFHeadTest();
	InsertOFTailTest();
	InsertOFPosTest();
}

单链表中倒数第K个结点测试.c

#include "head_list.h"
#include <stdlib.h>
#include <stdio.h>
void InsertOFTailTest()
{
	HeadList head;
	InitHeadList(&head);

	InsertOFTail(&head, 1);
	InsertOFTail(&head, 2);
	InsertOFTail(&head, 3);
	InsertOFTail(&head, 4);
	InsertOFTail(&head, 5);
	InsertOFTail(&head, 6);

	ShowList(&head);
	HeadList* p = HeadListQueryByVal(&head, 6);
	if (p)
		printf("%d\n", p->data);
}


int main() 
{
	InsertOFTailTest();
}

链表中环的入口结点.c

#include "head_list.h"
#include <stdlib.h>
#include <stdio.h>

HeadList* MeetingNode(HeadList* head)
{
	if (head == NULL) exit(0);

	HeadList* pSlow = head->next;
	HeadList* pFast = NULL;
	if (pSlow)
		pFast = head->next->next;

	while (pSlow != NULL && pFast != NULL && pFast->next != NULL)
	{
		if (pSlow == pFast)
			return pSlow;

		pSlow = pSlow->next;
		pFast = pFast->next->next;
	}
	return NULL;
}

HeadList* EntryNodeOfLoop(HeadList* head)
{
	HeadList* meetingNode = MeetingNode(head);
	if (meetingNode == NULL) return NULL;

	int nodeInLoop = 1;
	HeadList* p1 = meetingNode;
	while (p1->next != meetingNode)
	{
		p1 = p1->next;
		nodeInLoop++;
	}
	p1 = head;
	for (int i = 0; i < nodeInLoop; ++i)
		p1 = p1->next;

	HeadList* p2 = head;
	while (p1 != p2)
	{
		p1 = p1->next;
		p2 = p2->next;
	}
	return p1;
}

void InsertOFTailTest()
{
	HeadList head;
	InitHeadList(&head);

	InsertOFTail(&head, 1);
	InsertOFTail(&head, 2);
	InsertOFTail(&head, 3);
	InsertOFTail(&head, 4);
	InsertOFTail(&head, 5);
	InsertOFTail(&head, 6);

	ShowList(&head);
	HeadList* p1 = HeadListQueryByVal(&head, 6);
	HeadList* p2 = HeadListQueryByVal(&head, 3);
	p1->next = p2;

	HeadList* p3 = EntryNodeOfLoop(&head);
	if (p3)
		printf("%d\n", p3->data);
}

int main() 
{
	InsertOFTailTest();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_200_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值