首页 > 代码库 > 单链表.

单链表.

#pragma once

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

typedef int DataType;

typedef struct Node
{
	DataType data;
	struct Node* next;
}Node, *PLinkList;

//struct Node* 

// 初始化/销毁 /打印单链表
void InitList(PLinkList* list);
void PrintList(PLinkList list);
int GetLength(PLinkList list);

void PushBack(PLinkList* ppList, DataType x);
void PushFront(PLinkList* ppList, DataType x);


Node* Find(PLinkList pList, DataType x);
void Insert(PLinkList* ppList, Node * n, DataType x);
void Remove(PLinkList* ppList, Node * n);
void Erase(PLinkList* ppList, DataType x,DataType all); 
void DelMidNode(Node*n);

void InsertFrontNode(Node *n,DataType x);
void Reverse(PLinkList* ppList);
void Sort(PLinkList pList);



PLinkList _CreateNode(DataType x)
{
	PLinkList tmp = (PLinkList)malloc(sizeof(Node));
	tmp->data = http://www.mamicode.com/x;"%d->", begin->data);
		begin = begin->next;
	}

	printf("NULL\n");
}

int GetLength(PLinkList pList)
{
	int count = 0;
	PLinkList begin = pList;

	while (begin != NULL)
	{
		count++;
		begin = begin->next;
	}

	return count;
}

void PushBack(PLinkList* ppList, DataType x)
{
	assert(ppList);
	PLinkList tmp = *ppList;
	if (*ppList == NULL)
		*ppList = _CreateNode(x);
	else
	{
		while (tmp->next != NULL)
		{
			tmp = tmp->next;
		}
		tmp->next = _CreateNode(x);
	}
}

void PushFront(PLinkList* ppList, DataType x)
{
	assert(ppList);

	// 1.没有节点
	// 2.有多个节点
	if (*ppList == NULL)
	{
		*ppList = _CreateNode(x);
	}
	else
	{
		PLinkList tmp = _CreateNode(x);
		tmp->next = *ppList;
		*ppList = tmp;
	}
}

void PopFront(PLinkList* ppList)
{
	PLinkList tmp;
	assert(ppList);

	if (*ppList == NULL)
	{
		printf("List Is Empty\n");
		return;
	}

	tmp = *ppList;
	*ppList = (*ppList)->next;
	free(tmp);
}

void PopBack(PLinkList* ppList)
{
	assert(ppList);

	if (*ppList == NULL)
	{
		printf("List Is Empty!\n");
		return;
	}
	else
	{
		PLinkList prev, end;
		prev = NULL;
		end = *ppList;
		while (end->next != NULL)
		{
			prev = end;
			end = end->next;
		}
		free(end);
		if (prev == NULL)
		{
			*ppList = NULL;
		}
		else
		{
			prev->next = NULL;
		}
	}
}


Node* Find(PLinkList pList, DataType x)
{
	assert(pList);
	PLinkList begin=pList;
	while (begin != NULL)
	{
		if (begin->data =http://www.mamicode.com/=x)"n Is NULL!\n");
	}
	else if (n->next == NULL)
	{
		printf("n Is Tail!\n");
	}
	else
	{
		Node* del = n->next;
		n->data = http://www.mamicode.com/del->data;"n Is NULL!\n");
	}
	else if (n->next==NULL)
	{
		n->next= _CreateNode(x);
		PLinkList *tmp = n->data;
		n ->data= http://www.mamicode.com/n->next->data;"List Is NULL!\n");
		return;
	}
	else if (pList->next == NULL)
	{
		printf("There Is Only One Node In This List!\n");
		return;
	}
	else
	{
		PLinkList tmp=0,end=pList;
		PLinkList begin = pList;
		for (; end ->next!= NULL; )
		{
			for (begin=pList; begin->next != NULL; begin=begin->next)
			{
				if ((begin->data) < (begin->next->data))
				{
					tmp = begin->data;
					begin->data = http://www.mamicode.com/begin->next->data;"Slist.h"

void Test1()
{
	PLinkList pList;
	InitList(&pList);

	PushBack(&pList, 1);
	PushBack(&pList, 2);
	PushBack(&pList, 3);
	PushBack(&pList, 4);
	PrintList(pList);

	GetLength(pList);
	PrintList(pList);
	printf("Length: %d\n", GetLength(pList));
}

void Test2()
{
	PLinkList pList;
	InitList(&pList);

	PushFront(&pList, 1);
	PushFront(&pList, 2);
	PushFront(&pList, 3);
	PushFront(&pList, 4);

	PopFront(&pList);
	PopFront(&pList);
	PopFront(&pList);
	PopFront(&pList);
	PopFront(&pList);


	GetLength(pList);
	PrintList(pList);
	printf("Length: %d\n", GetLength(pList));
}

void Test3()
{
	PLinkList pList;
	InitList(&pList);

	PushFront(&pList, 1);
	PushFront(&pList, 2);
	PushFront(&pList, 3);
	PushFront(&pList, 4);
	PrintList(pList);

	PopBack(&pList);
	PopBack(&pList);
	PopBack(&pList);
	PopBack(&pList);
	
	PrintList(pList);

	GetLength(pList);
	PrintList(pList);
	printf("Length: %d\n", GetLength(pList));
}

void Test4()
{
	PLinkList pList;
	InitList(&pList);


	PushFront(&pList, 1);
	PushFront(&pList, 2);
	PushFront(&pList, 3);
	PushFront(&pList, 4);

	PrintList(pList);

	Node* ret = Find(pList, 1);
	if (ret != NULL)
	{
		Insert(&pList, ret, 0);
	}
	PrintList(pList);

	Node* ret1 = Find(pList, 0);
	Remove(&pList, ret1);
	PrintList(pList);
}

Test5()
{
	PLinkList pList;
	InitList(&pList);

	PushFront(&pList, 1);
	PushFront(&pList, 2);
	PushFront(&pList, 3);
	PushFront(&pList, 4);

	PrintList(pList);

	Node* ret = Find(pList, 1);
	DelMidNode(ret);
	PrintList(pList);
	ret = Find(pList, 4);
	DelMidNode(ret);
	PrintList(pList);
	ret = Find(pList, 2);
	DelMidNode(ret);
	PrintList(pList);

	Node* ret1 = Find(pList, 1);
	InsertFrontNode(ret1, 2);
	PrintList(pList);

	Node* ret2 = Find(pList, 3);
	InsertFrontNode(ret2, 4);
	PrintList(pList);


}

Test6()
{
	PLinkList pList;
	InitList(&pList);

	PushFront(&pList, 1);
	PushFront(&pList, 1);
	PushFront(&pList, 1);
	PushFront(&pList, 2);
	PushFront(&pList, 3);
	PushFront(&pList, 4);
	PushFront(&pList, 5);
	PushFront(&pList, 6);
	PushFront(&pList, 7);
	PrintList(pList);

	Erase(pList, 1, 0);
	PrintList(pList);

	Erase(pList, 1, 1);
	PrintList(pList);

	Reverse(&pList);
	PrintList(pList);

	Sort(pList);
	PrintList(pList);
}

int main()
{
	//Test1();
	//Test2();
	//Test3();
	//Test4();
	Test5();
	//Test6();
	return 0;
}

单链表.