带头节点的单链表的基本操作

List.h

#ifndef AAA
#define AAA

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

typedef int DataType;

typedef struct Node
{
	struct Node *next;
	DataType data;
}pNode, *pList;

void lnitLinkList(pList *pplist);
pList BuyNode(DataType d);
void DestroyLinkList(pList plist);
void PushBack(pList* pplist, DataType d);
void PopBack(pList *pplist);
void PushFront(pList* pplist, DataType d);
void PopFront(pList *pplist);
void PrintfList(pList plist);
pList Find(pList plist, DataType d);
void Insert(pList *pplist,pList pos, DataType d);
pList SListBack(pList plist);

#endif AAA

函数.c

#include"List.h"

//初始化单链表
void lnitLinkList(pList *pplist)
{
	//头节点
	*pplist = BuyNode(0);
}
//创建一个节点
pNode* BuyNode(DataType d)
{
	pList NewNode = (pList)malloc(sizeof(pNode));
	if(NewNode == NULL)
	{
		assert(NULL);
		return NULL;
	}
	NewNode->data = d;
	NewNode->next = NULL;
	return NewNode;
}
//销毁单链表
void DestroyLinkList(pList plist)
{
	pList cur = NULL;
	cur = plist;
	while(cur)
	{
		pList ret = cur;
		cur = cur->next;
		free(ret);
		ret =NULL;
	}
}
//尾插法
void PushBack(pList* pplist, DataType d)
{
	pList cur = NULL;
	assert(pplist);
	cur = *pplist;
	//先找到最后一个节点
	while(cur->next)
	{
		cur = cur->next;
	}
	//将节点接在最后一个节点的后面
	cur->next = BuyNode(d);
}
//尾删法
void PopBack(pList *pplist)
{
	pList cur = NULL;
	pList node = NULL;
	assert(pplist);
	cur = *pplist;
	//只有头节点时
	if(cur->next == NULL)
	{
		printf("链表为空");
		return ;
	}
	//链表有不只一个节点
	while(cur->next->next != NULL)
	{
		cur = cur->next;
	}
	node = cur->next;//记录最后一个节点
	free(node);
	node = NULL;
	cur->next = NULL;
}
//头插法
void PushFront(pList* pplist, DataType d)
{
	//1.保存首节点后面的地址
	//2.将新节点接在首节点的后面
	//3.将首节点后的原地址接在新地址后面
	pList cur = NULL;
	assert(pplist);
	cur = (*pplist)->next;
	(*pplist)->next = BuyNode(d);
	(*pplist)->next->next = cur;
}
//头删法
void PopFront(pList *pplist)
{
	pList cur = NULL;
	assert(pplist);
	//记住首节点后面的节点
	cur = (*pplist)->next;
        //如果链表中只有首节点的话,直接返回
        if(cur == NULL)
        {
            return ;
        }
        //如果链表中有多个个节点的话
	//将首节点后的第二个节点接在首节点的后面
	(*pplist)->next = cur->next;
	//将删除的节点释放
	free(cur);
	cur = NULL;
}
//打印
void PrintfList(pList plist)
{
	pList cur = plist->next;
	if(plist->next == NULL)
	{
		printf("链表为空\n");
	}
	else
	{
		while(cur)
		{
			printf("%d->", cur->data);
			cur = cur->next;
		}
		printf("NULL\n");
	}
}
//在单链表中找指定数据的位置
pList Find(pList plist, DataType d)
{
	pList cur = plist->next;
	while(cur != NULL)
	{
		if(cur->data == d)
			return cur;
		cur = cur->next;
	}
	return NULL;
}
//获取单链表的最后一个节点
pList SListBack(pList plist)
{
	pList cur = (plist)->next;
	if(cur != NULL)
	{
		while(cur->next != NULL)
		{
			cur = cur->next;
		}
	}
	return cur;
}

//在指定位置之前插入一个数据
void Insert(pList *pplist,pList pos, DataType d)
{
	pList cur = NULL;
	pList ret = NULL;
	pNode* Node = NULL;
	assert(pplist);
	cur = *pplist;
	ret = (*pplist)->next;
	//假设为空链表
	if(cur->next == NULL && pos == NULL)
		return ;
	//假设不是空链表且地址有效
        //找到要插入节点之前的这个节点
	while(ret != pos )
	{
		if(ret == NULL)
			return ;
		cur = ret;
		ret = ret->next;
	}
	//将新节点接在指定位置之前
	Node = BuyNode(d);
	cur->next = Node;
	Node->next = ret;
}

主函数.c

#include"List.h"

void Text()
{
	pList plist = NULL;
	pList cur = NULL;
	lnitLinkList(&plist);
	PushBack(&plist, 1);
	PushBack(&plist, 2);
	PushBack(&plist, 3);
	PopBack(&plist);
	PushFront(&plist, 6);
	PushFront(&plist, 5);
	PushFront(&plist, 4);
	PopFront(&plist);
	PrintfList(plist);	
	cur = SListBack(plist);
	printf("最后一个节点为:%d", cur->data);
    Insert(&plist,Find(plist,1), 4);
	DestroyLinkList(plist);
}

int main()
{
	Text();
	return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值