数据结构之单链表(带头结点)

LinkList.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1

typedef int ElementType;

typedef struct ListNode
{
	ElementType data;
	struct ListNode* next;
}ListNode;


// 初始化链表
void ListInit(ListNode** pphead);

// 创建一个新的节点
ListNode* BuyNewnode(ElementType e);

// 打印链表
void ListPrint(ListNode* phead);

// 尾插
void ListPushBack(ListNode* phead, ElementType e);

// 头插
void ListPushFront(ListNode* phead, ElementType e);

// 尾插法建立一个链表
void CreatList_T(ListNode* phead, int n);

// 头插法建立一个链表
void CreatList_H(ListNode* phead, int n);

// 判断链表是否为空
int ListEmpty(ListNode* phead);

// 销毁链表
int ListDestroy(ListNode** pphead);

// 清空链表
int ListClear(ListNode* phead);

// 链表表长
int ListLength(ListNode* phead);

// 获取第pos个元素值
ListNode* ListGetElem(ListNode* phead, int pos);

// 按值查找,找到后返回该节点
ListNode* ListSearchByVal(ListNode* phead, ElementType e);

// 按值查找,找到后返回是链表中的第几个
int ListSearchByVal_pos(ListNode* phead, ElementType e);

// 删除第pos个节点位置
int ListDeleteByPos(ListNode** pphead, int pos, ElementType* e);

// 删除第pos个节点位置(第二种方式)
int ListDeleteByPos_second(ListNode* phead, int pos);

// 按节点删除,删除节点p
int ListDeleteByNode(ListNode* phead, ListNode* p);

// 按值删除
int ListDeleteByVal(ListNode* phead, ElementType e);

// 指定位置插入,在第pos个位置插入一个新节点
int ListInsert(ListNode* phead, int pos, ElementType e);

LinkList.c

#define _CRT_SECURE_NO_WARNINGS
#include "LinkList.h"

// 初始化链表
void ListInit(ListNode** pphead)
{
	*pphead = (ListNode*)malloc(sizeof(struct ListNode));

	if (*pphead == NULL)
	{
		exit(OVERFLOW);
	}

	(*pphead)->next = NULL;
}

// 创建一个新的节点
ListNode* BuyNewnode(ElementType e)
{
	ListNode* newnode = (ListNode*)malloc(sizeof(struct ListNode));
	// 申请空间失败
	if (newnode == NULL)
	{
		exit(OVERFLOW);
	}

	newnode->data = e;
	newnode->next = NULL;

	return newnode;
}

// 打印链表
void ListPrint(ListNode* phead)
{
	assert(phead);
	ListNode* cur = phead->next;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

// 尾插
void ListPushBack(ListNode* phead, ElementType e)
{
	assert(phead);

	ListNode* newnode = BuyNewnode(e);

	// 找尾部
	ListNode* tail = phead;
	while (tail->next != NULL)
	{
		tail = tail->next;
	}
	tail->next = newnode;
}


// 头插
void ListPushFront(ListNode* phead, ElementType e)
{
	assert(phead);

	// 创建一个新的节点
	ListNode* newnode = BuyNewnode(e);
	newnode->next = phead->next;
	phead->next = newnode;
}

// 尾插法建立一个链表
void CreatList_T(ListNode* phead, int n)
{
	ListNode* tail = phead;
	int i = 0;
	for (i = 0; i < n; i++)
	{
		ElementType e;
		scanf("%d", &e);
		ListNode* newnode = BuyNewnode(e);

		tail->next = newnode;
		tail = newnode;
	}
}

// 头插法建立一个链表
void CreatList_H(ListNode* phead, int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		ElementType e;
		scanf("%d", &e);
		ListNode* newnode = BuyNewnode(e);

		newnode->next = phead->next;
		phead->next = newnode;
	}
}

// 判断链表是否为空
int ListEmpty(ListNode* phead)
{
	
	if (phead->next)
	{
		return FALSE; // 非空
	}
	else
	{
		return TRUE; // 为空
	}
}

// 销毁链表
int ListDestroy(ListNode** pphead)
{
	assert(pphead);

	ListNode* p;
	while (*pphead)
	{
		p = *pphead;
		*pphead = (*pphead)->next;
		free(p);
		p = NULL;
	}
	return OK;
}

// 清空链表(头结点还在)
int ListClear(ListNode* phead)
{
	assert(phead);
	ListNode* p, *q;
	p = phead->next;
	while (p)
	{
		q = p->next;
		free(p);
		p = q;
	}
	phead->next = NULL;

	return OK;
}

// 链表表长
int ListLength(ListNode* phead)
{
	assert(phead);

	ListNode* p = phead->next;
	int i = 0;
	while (p)
	{
		i++;
		p = p->next;
	}

	return i;
}

// 获取第pos个元素值
ListNode* ListGetElem(ListNode* phead, int pos)
{
	assert(phead);
	assert(pos >= 1 && pos <= ListLength(phead));

	ListNode* p = phead->next;
	int i = 1;
	while (i != pos)
	{
		p = p->next;
		i++;
	}
	return p;
}


// 按值查找,找到后返回该节点
ListNode* ListSearchByVal(ListNode* phead, ElementType e)
{
	ListNode* p = phead->next;

	while (p && p->data!=e)
	{
		
		p = p->next;
	}

	return p;
}

// 按值查找,找到后返回是链表中的第几个
int ListSearchByVal_pos(ListNode* phead, ElementType e)
{
	ListNode* p = phead->next;
	int i = 1;
	while (p && p->data != e)
	{
		p = p->next;
		i++;
	}
	if (p)
	{
		return i;
	}
	else
	{
		return 0;
	}

}


// 删除第pos个节点位置
int ListDeleteByPos(ListNode** pphead, int pos, ElementType* e)
{
	assert(pphead);
	assert(pos >= 1 && pos <= ListLength(*pphead));

	ListNode* p = *pphead, *q;
	while (pos-1)
	{
		p = p->next;
		pos--;
	}

	q = p->next;
	p->next = q->next;
	*e = q->data;
	free(q);
	q = NULL;

	return OK;

}

// 删除第pos个节点位置(第二种方式)
int ListDeleteByPos_second(ListNode* phead, int pos)
{
	ListNode* p = phead, *q;
	int i = 0;
	while (p->next && i < pos - 1)
	{
		p = p->next;
		i++;
	}

	// 判断位置是否合理
	if (p->next == NULL || i > pos - 1)
	{
		return ERROR;
	}

	q = p->next;
	p->next = q->next;
	free(q);
	q = NULL;

	return OK;
}

// 按节点删除,删除节点pos
int ListDeleteByNode(ListNode* phead, ListNode* pos)
{
	assert(phead);
	ListNode* prev = phead;
	while (prev->next != pos)
	{
		prev = prev->next;
	}
	prev->next = pos->next;
	free(pos);
	pos = NULL;

	return OK;
}

// 按值删除
int ListDeleteByVal(ListNode* phead, ElementType e)
{
	ListNode* prev = phead, *p;
	while (prev->next && prev->next->data != e)
	{
		prev = prev->next;
	}

	if (prev->next == NULL)
	{
		return 0;
	}

	p = prev->next;
	prev->next = p->next;
	free(p);
	p = NULL;

	return OK;
}

// 指定位置插入,在第pos个位置插入一个新节点
int ListInsert(ListNode* phead, int pos, ElementType e)
{
	ListNode* p = phead;
	int i = 0;
	while (p && i < pos - 1)
	{
		p = p->next;
		i++;
	}

	if (p == NULL || i > pos - 1)
	{
		return ERROR;
	}

	// 创建一个新的节点
	ListNode* newnode = BuyNewnode(e);

	newnode->next = p->next;
	p->next = newnode;

	return OK;
}

test.c

#define _CRT_SECURE_NO_WARNINGS
#include "LinkList.h"

// 测试尾部插入
void Test01()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	ListPushBack(list, 1);
	ListPushBack(list, 2);
	ListPushBack(list, 3);
	ListPushBack(list, 4);
	ListPrint(list);


}

// 测试头部插入
void Test02()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	ListPushFront(list, 1);
	ListPushFront(list, 2);
	ListPushFront(list, 3);
	ListPushFront(list, 4);

	ListPrint(list);
}

// 测试尾插法建立一个链表
void Test03()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	CreatList_T(list, 5);
	ListPrint(list);
}

// 测试头插法建立一个链表
void Test04()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	CreatList_H(list, 5);
	ListPrint(list);
}


// 测试销毁链表
void Test05()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	ListPushBack(list, 1);
	ListPushBack(list, 2);
	ListPushBack(list, 3);
	ListPushBack(list, 4);
	ListPrint(list);

	ListDestroy(&list);
	ListPrint(list);

}

// 测试清空链表
void Test06()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	ListPushBack(list, 1);
	ListPushBack(list, 2);
	ListPushBack(list, 3);
	ListPushBack(list, 4);
	ListPrint(list);

	ListClear(list);

	ListPrint(list);
}

// 测试获取第pos个元素
void Test07()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	ListPushBack(list, 1);
	ListPushBack(list, 2);
	ListPushBack(list, 3);
	ListPushBack(list, 4);
	ListPrint(list);

	ListNode* ret = ListGetElem(list, 4);
	printf("%d\n", ret->data);
	
}

// 测试按值查找,找到后返回该节点
void Test08()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	ListPushBack(list, 1);
	ListPushBack(list, 2);
	ListPushBack(list, 3);
	ListPushBack(list, 4);
	ListPrint(list);

	ListNode* p = ListSearchByVal(list, 1);
	if (p == NULL)
	{
		printf("没有找到!\n");
	}
	else
	{
		printf("%d\n", p->data);
	}
}

// 测试按值查找,找到后返回是链表中的第几个
void Test09()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	ListPushBack(list, 1);
	ListPushBack(list, 2);
	ListPushBack(list, 3);
	ListPushBack(list, 4);
	ListPrint(list);

	int pos = ListSearchByVal_pos(list, 3);
	if (pos)
	{
		printf("%d\n",pos);
	}
	else
	{
		printf("没有找到\n");
	}
}

// 测试删除第pos个节点位置
void Test10()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	ListPushBack(list, 1);
	ListPushBack(list, 2);
	ListPushBack(list, 3);
	ListPushBack(list, 4);
	ListPrint(list);

	ElementType e;
	ListDeleteByPos(&list, 3, &e);
	printf("删除的元素是:%d\n", e);
	ListPrint(list);
}

// 测试删除第pos个节点位置(第二种方式)
void Test11()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	ListPushBack(list, 1);
	ListPushBack(list, 2);
	ListPushBack(list, 3);
	ListPushBack(list, 4);
	ListPrint(list);

	ListDeleteByPos_second(list, 3);
	ListPrint(list);
}

// 测试按节点删除,删除节点pos
void Test12()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	ListPushBack(list, 1);
	ListPushBack(list, 2);
	ListPushBack(list, 3);
	ListPushBack(list, 4);
	ListPrint(list);

	ListNode* pos = ListSearchByVal(list, 3);
	if (pos)
	{
		printf("%d\n", pos->data);
		ListDeleteByNode(list, pos);
		ListPrint(list);
	}
	else
	{
		printf("要删除的节点不存在!\n");
	}
	

}

// 测试按值删除
void Test13()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	ListPushBack(list, 1);
	ListPushBack(list, 2);
	ListPushBack(list, 3);
	ListPushBack(list, 4);
	ListPrint(list);

	int ret = ListDeleteByVal(list, 3);
	if (ret)
	{
		ListPrint(list);
	}
	else
	{
		printf("要删除的值不存在!\n");
	}
	
}


// 测试指定位置插入,在第pos个位置插入一个新节点
void Test14()
{
	// 初始化链表
	ListNode* list;
	ListInit(&list);

	ListPushBack(list, 1);
	ListPushBack(list, 2);
	ListPushBack(list, 3);
	ListPushBack(list, 4);
	ListPrint(list);

	int ret = ListInsert(list, 5, 22);
	if (ret)
	{
		ListPrint(list);
	}
	else
	{
		printf("插入位置不合法!\n");
	}


}



int main()
{
	Test14();

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值