单链表的初始化,删除,销毁,查找

单链表的初始化,头删,尾删,头插,尾插,销毁,查找。
Node.h
#ifndef __NODE_H__
#define __NODE_H__
typedef int DataType;
#define NULL 0
#include
#include
#include
typedef struct Node
{
	DataType data;
	struct Node*next;
}Node, *LinkList;
void InitList(LinkList *L);//单链表的初始化
LinkList BuyNode(DataType data);//产生新节点
void PrintList(LinkList L);//打印单链表的值域
LinkList Find(LinkList* L, DataType data);//找指定data值,返回其节点
void PushBack(LinkList* L, DataType data);//尾插
void PushFront(LinkList* L, DataType data);//头插
void PopBack(LinkList* L);//尾删
void PopFront(LinkList* L);//头删
void Destroy(LinkList* L);//单链表的销毁
size_t Size(LinkList L);//单链表长度
void Erase(LinkList* L,LinkList pos);//删除任意位置的节点
void Insert(LinkList pos, DataType data);//在任意节点后插入一个节点
#endif//__NODE_H__


Node.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"node.h"
void InitList(LinkList *L)
{
	*L = (Node*)malloc(sizeof(Node));
	if (*L == NULL){
		printf("申请内存空间失败");
	}
	(*L)->next = NULL;
	(*L)->data =NULL;
}
LinkList BuyNode(DataType data)
{
	LinkList NewNode = NULL;
	NewNode = (LinkList)malloc(sizeof(Node));
	if (NewNode == NULL){
		printf("为节点创建空间失败");
	}
	NewNode->data = data;
	NewNode->next = NULL;
    return NewNode;
}
void PrintList(LinkList L)
{
	LinkList Cur = L;
	if (L == NULL){
		printf("NULL");
	}
	while (Cur->next)
	{
		printf("%d--->", Cur->data);
		Cur = Cur->next;
	}
	printf("%d--->", Cur->data);
	printf("\n");
}
LinkList Find(LinkList* L, DataType data)
{
	assert(L);
	if (*L == NULL){
		return NULL;
	}
	LinkList Cur = *L;
	while (Cur->next!=NULL)
	{
		if (Cur->data== data){
			return Cur;
		}
		else
			Cur = Cur->next;
	}
	if (Cur->data == data){
		return Cur;
	}
	return Cur;
}
void PushBack(LinkList* L, DataType data)
{
	assert(L);
	LinkList Cur = *L;
	if (Cur== NULL){
		Cur = BuyNode(data);
	}
	while (Cur->next)
	{
	    Cur = Cur->next;
	}
	Cur->next= BuyNode(data);
}
void PushFront(LinkList* L, DataType data)
{
	assert(L);
	LinkList Cur = *L;
	if (Cur == NULL){
		*L = BuyNode(data);
	}
	*L = BuyNode(data);
	(*L)->next = Cur;
}
void PopBack(LinkList*L)
{
	LinkList Cur = *L;
	LinkList Pre = NULL;
	assert(L);
	if (Cur->next == NULL || Cur == NULL)
		return NULL;
	while (Cur->next){
		Pre = Cur;
		Cur = Cur->next;
	}
	free(Pre->next);
	Pre->next = NULL;
}
void PopFront(LinkList* L)
{
	LinkList Cur = *L;
	assert(L);
	if (Cur->next==NULL||Cur==NULL)
		return NULL;
	*L=(*L)->next;
	free(Cur);
}
size_t Size(LinkList L)
{
	LinkList Cur = L;
	int count = 0;
	while (Cur->next){
		count++;
		Cur = Cur->next;
	}
	return count + 1;
}

void Erase(LinkList* L, LinkList pos)
{
	LinkList Cur = *L;
	LinkList Pre = NULL;
	assert(L);
	if (*L == NULL || pos == NULL)
		return;
	if (*L == pos)
		return NULL;

	while (Cur->next){
		Pre = Cur;
		Cur = Cur->next;
		if (Cur == pos){
			Pre->next = pos->next;
			break;
		}
	}
}
void Insert(LinkList pos, DataType data)
{
	LinkList NewNode = BuyNode(data);
	if (pos == NULL)
		return;
	NewNode->next = pos->next;
	pos->next = NewNode;
}



void Destroy(LinkList* L)
{
	LinkList Cur = NULL;
	if (*L == NULL)
		return NULL;
	while (*L)
	{
		Cur = *L;
		*L = (*L)->next;
		free(Cur);
	}
}

test.c
 
  
#define _CRT_SECURE_NO_WARNINGS 1
#include"Node.h"
void test()
{
	LinkList M = NULL;
	InitList(&M);
	PushFront(&M, 2);
	PushBack(&M, 3);
	PushBack(&M, 4);
	PushBack(&M, 5);
	PushBack(&M, 6);
	PushBack(&M, 7);
	PopBack(&M);
	PopFront(&M);
	Erase(&M, Find(&M,3));
	Insert(Find(&M,0), 3);
	printf("%d\n",Size(M));
	PrintList(M);
	Destroy(&M);
}
int main()
{

	test();
	system("pause");
	return 0;
}


 
  



 
 

你可能感兴趣的:(单链表的初始化,删除,销毁,查找)