【数据结构】C语言实现单链表

33 篇文章 9 订阅
26 篇文章 0 订阅

用C语言实现基础单链表

结构体

首先,我们定义一个结构体Node

typedef int DataType;

typedef struct Node
{
	int data;
	struct Node* next;
}Node,*PNode;

Node这个结构体中,包含一个数据元素,和一个指向下一个节点的指针

初始化链表

void InitList(PNode* pHead)
{
	*pHead = NULL;
}

申请一个新的节点

PNode BuyNewNode(DataType data)
{
	PNode New = NULL;
	New = (PNode)malloc(sizeof(Node));
	New->data = data;
	New->next = NULL;
	return New;
}

打印链表

void PrintList(PNode pHead)
{	
	PNode Cur = pHead;
	while (Cur)
	{
		printf("%d ", Cur->data); 
		Cur = Cur->next;
	}
	printf("\n");
}

尾插一个节点

void PushBack(PNode* pHead,DataType data)
{
	assert(pHead!=NULL);
	PNode Cur = NULL;
	PNode NewNode = BuyNewNode(data);
	Cur = *pHead;
	if (*pHead == NULL)
	{
		*pHead = NewNode;
	}
	else
	{	
		while (Cur->next)
		{
			Cur = Cur->next;
		}
		Cur->next = NewNode;
	}
}

前插一个节点

void PushFront(PNode* pHead, DataType data)
{	
	assert(pHead != NULL);
	PNode Cur = *pHead;
	PNode NewNode = BuyNewNode(data);
	if (*pHead == NULL)
	{
		*pHead = NewNode;
	}
	else           
	{
		NewNode->next = *pHead;
		*pHead = NewNode;
	}
}

从尾部删除一个节点

void PopBack(PNode* pHead)
{	
	assert(pHead!=NULL);
	PNode Cur = *pHead;
	PNode DelNode = NULL;
	if (*pHead == NULL)
	{
		return;
	}
	if (Cur->next == NULL)
	{	
		free(Cur);
		*pHead = NULL;
	}
	while (Cur->next->next != NULL)
	{
		Cur = Cur->next;
	}
	
	//Cur->next = NULL;

	DelNode = Cur->next;
	free(DelNode);
	//free(*(Cur->next));
	
	DelNode = NULL;
	Cur->next = NULL;
}

从前面删除一个节点

void PopFront(PNode* pHead)
{
	assert(pHead!=NULL);
	if ((*pHead) == NULL)//无节点
	{
		return;
	}
	else//有多个节点
	{	
		PNode Del = (*pHead)->next;
		
		//(*pHead) = (*pHead)->next;
		
		free((*pHead)->next);
		Del = NULL;
	}
}

查找一个节点

PNode Find(PNode pHead, DataType data)
{
	assert(pHead != NULL);
	PNode Cur = pHead;
	while (Cur != NULL)
	{	
		if (Cur->data == data)
		{
			return Cur;
		}
		Cur = Cur->next;
	}
	return NULL;
}

根据一个节点来插入一个节点

void Insert(PNode pos, DataType data)
{	
	assert(pos != NULL);
	PNode NewNode = BuyNewNode(data);
	if (pos->next == NULL)
	{
		pos->next = NewNode;
	}
	else
	{
		PNode Cur = pos->next;
		pos->next = NewNode;
		NewNode->next = Cur;
	}
}
//  Insert(Find(&pHead, 4),5);

销毁单链表

void Destroy(PNode* pHead)
{
	PNode Node = (*pHead);
	PNode NodeNext = Node->next;
	while (NodeNext != NULL)
	{
		free(Node);
		Node = NodeNext;
		NodeNext = NodeNext->next;
	}
	free(Node);
	*pHead = NULL;
	Node = NULL;
}

返回尾节点

PNode Back(PNode pHead)
{
	//assert(pHead != NULL);
	PNode Cur = pHead;
	if (Cur == NULL)
	{
		return Cur;
	}
	while (Cur->next != NULL)
	{
		Cur = Cur->next;
	}
	return Cur;
}

完整代码

头文件 SeqList.h

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

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

typedef int DataType;

typedef struct Node
{
	int data;
	struct Node* next;
}Node,*PNode;

void InitList(PNode* pHead);
PNode BuyNewNode(DataType data);
void PrintList(PNode pHead);
void PushBack(PNode* pHead, DataType data);
void PushFront(PNode* pHead, DataType data);
void PopBack(PNode* pHead);
void PopFront(PNode* pHead);
PNode Find(PNode pHead, DataType data);
void Insert(PNode pos, DataType data);
void Destroy(PNode* pHead);
PNode Back(PNode pHead);

#endif __SEQLIST_H__

函数实现 SeqList.c

#include "SeqList.h"

void InitList(PNode* pHead)
{
	*pHead = NULL;
}

PNode BuyNewNode(DataType data)
{
	PNode New = NULL;
	New = (PNode)malloc(sizeof(Node));
	New->data = data;
	New->next = NULL;
	return New;
}

void PrintList(PNode pHead)
{	
	PNode Cur = pHead;
	while (Cur)
	{
		printf("%d ", Cur->data); 
		Cur = Cur->next;
	}
	printf("\n");
}

void PushBack(PNode* pHead,DataType data)
{
	assert(pHead!=NULL);
	PNode Cur = NULL;
	PNode NewNode = BuyNewNode(data);
	Cur = *pHead;
	if (*pHead == NULL)
	{
		*pHead = NewNode;
	}
	else
	{	
		while (Cur->next)
		{
			Cur = Cur->next;
		}
		Cur->next = NewNode;
	}
}

void PushFront(PNode* pHead, DataType data)
{	
	assert(pHead != NULL);
	PNode Cur = *pHead;
	PNode NewNode = BuyNewNode(data);
	if (*pHead == NULL)
	{
		*pHead = NewNode;
	}
	else           
	{
		NewNode->next = *pHead;
		*pHead = NewNode;
	}
}

void PopBack(PNode* pHead)
{	
	assert(pHead!=NULL);
	PNode Cur = *pHead;
	PNode DelNode = NULL;
	if (*pHead == NULL)
	{
		return;
	}
	if (Cur->next == NULL)
	{	
		free(Cur);
		*pHead = NULL;
	}
	while (Cur->next->next != NULL)
	{
		Cur = Cur->next;
	}
	
	//Cur->next = NULL;

	DelNode = Cur->next;
	free(DelNode);
	//free(*(Cur->next));
	
	DelNode = NULL;
	Cur->next = NULL;
}

void PopFront(PNode* pHead)
{
	assert(pHead!=NULL);
	if ((*pHead) == NULL)//无节点
	{
		return;
	}
	else//有多个节点
	{	
		PNode Del = (*pHead)->next;
		
		//(*pHead) = (*pHead)->next;
		
		free((*pHead)->next);
		Del = NULL;
	}
}

PNode Find(PNode pHead, DataType data)
{
	assert(pHead != NULL);
	PNode Cur = pHead;
	while (Cur != NULL)
	{	
		if (Cur->data == data)
		{
			return Cur;
		}
		Cur = Cur->next;
	}
	return NULL;
}

void Insert(PNode pos, DataType data)
{	
	assert(pos != NULL);
	PNode NewNode = BuyNewNode(data);
	if (pos->next == NULL)
	{
		pos->next = NewNode;
	}
	else
	{
		PNode Cur = pos->next;
		pos->next = NewNode;
		NewNode->next = Cur;
	}
}

//  Insert(Find(&pHead, 4),5);

void Destroy(PNode* pHead)
{
	PNode Node = (*pHead);
	PNode NodeNext = Node->next;
	while (NodeNext != NULL)
	{
		free(Node);
		Node = NodeNext;
		NodeNext = NodeNext->next;
	}
	free(Node);
	*pHead = NULL;
	Node = NULL;
}


PNode Back(PNode pHead)
{
	//assert(pHead != NULL);
	PNode Cur = pHead;
	if (Cur == NULL)
	{
		return Cur;
	}
	while (Cur->next != NULL)
	{
		Cur = Cur->next;
	}
	return Cur;
}



 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值