头指针单链表的C语言代码实现

point_list.h

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

typedef int ElemType; 
typedef struct Node 
{
	ElemType data;
	struct Node *next; 
}PointList;

void InitPointList(PointList** pp); // 初始化 

bool InsertPointList(PointList **pp, ElemType val, int pos); // 按位置插入 
bool InsertHead(PointList **pp, ElemType val); // 头插 
bool InsertTail(PointList **pp, ElemType val); // 尾插 

bool DeletePonitList(PointList **pp, int pos); // 按位置删除 
bool DeleteHead(PointList **pp); // 头删 
bool DeleteTail(PointList **pp); // 尾删 
bool DeleteValue(PointList **pp, ElemType val); // 按值删除 

void DestroyPointList(PointList **pp); // 销毁

void ShowList(PointList** head);

PointList* ListQueryByVal(PointList** head, int val);

point_list.c

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

static PointList* ApplyNode(ElemType val, PointList* nt) 
{ 
	PointList* s = (PointList*)malloc(sizeof(PointList)); 
	if (s == NULL) return NULL; 
	s->data = val; 
	s->next = nt; 
	return s; 
}

void InitPointList(PointList** pp) 
{ 
	if (pp == NULL) exit(0); 
	*pp = NULL; 
}

bool InsertHead(PointList** pp, ElemType val) 
{ 
	if (pp == NULL) exit(0); 
	*pp = ApplyNode(val, *pp); 
	if (*pp == NULL) return false; 
	return true; 
}
bool InsertPointList(PointList** pp, ElemType val, int pos)
{
	if (pp == NULL) exit(0);
	if (pos < 0) return false;
	if (*pp == NULL || pos == 0) return InsertHead(pp, val);
	PointList* p = *pp;
	while (pos > 1 && p != NULL)
	{
		pos--;
		p = p->next;
	}
	if (p == NULL) return false;
	PointList* newNode = ApplyNode(val, p->next);
	if (newNode == NULL) return false;
	p->next = newNode;
	return true;
}

bool InsertTail(PointList** pp, ElemType val) 
{ 
	if (pp == NULL) exit(0); 
	if (*pp == NULL) return InsertHead(pp, val); 
	PointList * p = *pp; 
	while (p->next != NULL) p = p->next; 
	p->next = ApplyNode(val, NULL); 
	return false; 
}

bool DeleteHead(PointList** pp) 
{ 
	if (pp == NULL) exit(0); 
	if (*pp == NULL) return false; 
	PointList * q = *pp; 
	* pp = q->next; 
	free(q); 
	return true; 
} 
bool DeletePonitList(PointList * *pp, int pos) 
{ 
	if (pp == NULL) exit(0); 
	if (*pp == NULL || pos < 0) return false; 
	if (pos == 0) 
	{ 
		return DeleteHead(pp); 
	} 
	PointList * p = *pp; 
	while (pos > 1 && p->next != NULL) 
	{ 
		pos--; 
		p = p->next; 
	} 
	if (p->next == NULL) return false; // pos越界 
	PointList *q = p->next; 
	p->next = q->next; 
	free(q); 
	return true; 
} 

bool DeleteTail(PointList **pp) 
{ 
	if (pp == NULL) exit(0); 
	if (*pp == NULL) return false; 
	if ((*pp)->next == NULL) return DeleteHead(pp); 
	PointList *p = *pp; 
	PointList *q = p->next; 
	while (q->next != NULL) 
	{ 
		p = q; 
		q = q->next; 
	} 
	p->next = NULL; 
	free(q);
	return true; 
} 

bool DeleteValue(PointList * *pp, ElemType val) 
{ 
	if (pp == NULL) exit(0); 
	if (*pp == NULL) return false; // 进来就是空链表 
	while (*pp != NULL && (*pp)->data == val) 
	{ 
		DeleteHead(pp); 
	} 
	if (*pp == NULL) return true; // 将链表删除成空链 
	PointList *p = *pp; 
	PointList *q = p->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 DestroyPointList(PointList** pp) 
{ 
	if (pp == NULL) exit(0); 
	while (*pp != NULL) 
	{ 
		DeleteHead(pp); 
	} 
}

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


PointList* ListQueryByVal(PointList** head, int val)
{
	if (head == NULL) exit(0);
	PointList* p = *head;
	while (p->data != val && p)
	{
		p = p->next;
	}
	return p;
}

test_main.c

#include "point_list.h"

void InsertPointListTest()
{
	PointList* head;
	InitPointList(&head);

	for (int i = 0; i < 10; ++i)
	{
		InsertPointList(&head, i, 0);
	}
	ShowList(&head);
}

void InsertHeadTest() 
{
	PointList* head;
	InitPointList(&head);
	
	for(int i = 1; i <= 10; ++i)
	{
		InsertHead(&head, i);
	}
	ShowList(&head);
}

void InsertTailTest()
{
	PointList* head;
	InitPointList(&head);

	for (int i = 1; i <= 10; ++i)
	{
		InsertTail(&head, i);
	}
	ShowList(&head);
}

int main()
{
	InsertPointListTest();
	InsertHeadTest();
	InsertTailTest();
}

O(1)时间下删除链表的结点.c

#include "point_list.h"
#include <stdlib.h>
void DeleteNode(PointList** head, PointList* node);
void InsertTailTest()
{
	PointList* head;
	InitPointList(&head);

	for (int i = 1; i <= 10; ++i)
	{
		InsertTail(&head, i);
	}
	ShowList(&head);

	PointList* node = ListQueryByVal(&head, 6);
	DeleteNode(&head, node);

	ShowList(&head);
}
void DeleteNode(PointList** head, PointList* node)
{
	if (!head || !node)
		return;

	if (node->next != NULL)
	{
		PointList* pNext = node->next;
		node->data = pNext->data;
		node->next = pNext->next;

		free(pNext);
		pNext = NULL;
	}
	else if (node == *head)
	{
		free(node);
		node = NULL;
		*head = NULL;
	}
	else {
		DeleteTail(head);
	}
}
int main()
{
	InsertTailTest();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_200_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值