用c语言实现单链表

用c语言实现单链表

Node.h

#pragma once
typedef int DataType;
typedef struct Node
{
	DataType data;
	struct Node* next;
}Node;

Node* BuyNode(DataType x); //增容
void PrintList(Node* pHead); //打印
void PushBack(Node** ppHead, DataType x); //尾插
void PopBack(Node** ppHead); //尾删
void PushFront(Node** ppHead, DataType x); //头插
void PopFront(Node** ppHead); //头删
void Insert(Node** ppHead,Node* pos, DataType x); //插入元素
void Erase(Node** ppHead,Node* pos); //删除任意
Node* Find(Node* pHead, DataType x); //查找


Node* BuyNode(DataType x) //增容
{
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = x;
	node->next = NULL;
	return node;
}

void PrintList(Node* pHead) //打印
{
	Node* cur = pHead;
	while (cur)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

void PushBack(Node** ppHead, DataType x) //尾插
{
	if (*ppHead == NULL)
	{ 
		*ppHead = BuyNode(x);
	}
	else
	{
		Node* tail = *ppHead;
		while (tail->next)
		{
			tail = tail->next;
		}
		tail->next = BuyNode(x);
	}
}

void PopBack(Node** ppHead) //尾删
{
	if (*ppHead == NULL) //空链表
	{
		return;
	}
	else if ((*ppHead)->next == NULL) //只有一个节点
	{
		free(*ppHead);
		*ppHead = NULL;
	}
	else      //有多个节点
	{
		Node* cur = *ppHead;
		Node* prev = NULL;
		while (cur->next)
		{
			prev = cur;
			cur = cur->next;
		}
		prev->next = NULL;
		free(cur);
	}
}

void PushFront(Node** ppHead, DataType x) //头插
{
	/*if (*ppHead == NULL)
	{
		*ppHead = BuyNode(x);
	}
	else
	{
		Node* tmp = BuyNode(x);
		tmp->next = *ppHead;
		*ppHead = tmp;
	}*/
	Node* tmp = BuyNode(x);
	tmp->next = *ppHead;
	*ppHead = tmp;
}

void PopFront(Node** ppHead) //头删
{
	if (*ppHead == NULL) //空链表
	{
		return;
	}
	else if ((*ppHead)->next == NULL) //只有一个节点
	{
		free(*ppHead);
		*ppHead = NULL;
	}
	else  //有多个节点
	{
		Node* next = (*ppHead)->next;
		free(*ppHead);
		*ppHead = next;
	}
}

void Insert(Node** ppHead, Node* pos, DataType x) //插入元素
{
	assert(pos);
	if (pos == *ppHead)  //等于头插
	{
		PushFront(ppHead, x);
	}
	else
	{
		Node* tmp=BuyNode(x);
		Node* prev = *ppHead;
		while ((prev->next)!=pos)  //prev记录的是被插位置的前一位置元素
		{
			prev = prev->next;
		}
	
		prev->next = tmp;
		tmp->next = pos;
	}
}

void Erase(Node** ppHead, Node* pos) // 删除任意元素
{
	assert(pos);
	if (pos == *ppHead)
	{
		PopFront(ppHead);
	}
	else if (pos->next == NULL)
	{
		PopBack(ppHead);
	}
	else
	{
		Node* prev = *ppHead,*next = pos->next;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		prev->next = next;
		free(pos);
	}
}

Node* Find(Node* pHead, DataType x) //查找
{
	Node* cur = pHead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

void Testlist1()
{
	Node* list = NULL;
	PushBack(&list, 1);
	PushBack(&list, 2);
	PushBack(&list, 3);
	PushBack(&list, 4);
	PrintList(list);
	PopBack(&list);
	PrintList(list);
	PushFront(&list,4);
	PrintList(list);
	PopFront(&list);
	PrintList(list);
}

void Testlist2()
{
	Node* list = NULL;
	Node* pos = NULL;
	PushBack(&list, 1);
	PushBack(&list, 2);
	PushBack(&list, 3);
	PushBack(&list, 4);
	PrintList(list);
	pos = Find(list, 3);
	/*pos = list;
	pos = pos->next;*/
	Insert(&list, pos, 5);
	PrintList(list);
	Erase(&list, pos);
	PrintList(list);
}

Node.c

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "Node.h"

int main()
{
	Testlist2();
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值