1. 程式人生 > >單鏈表

單鏈表

ron 結點 style ins def bre emp return end

LinkList.h

#pragma once

#include<stdio.h>
#include<assert.h>
#include<malloc.h>
typedef int DataType;
typedef struct Node
{
	DataType data;
	struct Node* next;
}Node, *plist;
plist _CreateNode(DataType x);
void InitLinklist(plist* pplist);
void PrintLinklist(plist list);
void PushBack(plist* pplist, DataType x);
int GetLength(plist list);
void PushFront(plist* pplist, DataType x);
void PopFront(plist* pplist);
void PopBack(plist* pplist);
Node* Find(plist list, DataType x);
void Insert(plist* pplist, Node * n, DataType x);
void Remove(plist* pplist, Node * n);
int Erase(plist* pplist, DataType x);
int EraseAll(plist* pplist, DataType x, DataType all);
void DeleteNode(Node* n);
void InsertFront(Node* n, DataType x);
void PrintEndToBegin(plist list);

LinkList.c

#include"LinkList.h"
plist _CreateNode(DataType x)
{
	plist tmp = (plist)malloc(sizeof(Node));
	tmp->data = x;
	tmp->next = NULL;
	return tmp;
}
void InitLinklist(plist* pplist)
{
	assert(pplist);
	*pplist = NULL;
}
void PrintLinklist(plist list)
{
	plist begin = list; 
	while (begin != NULL)
	{
		printf("%d->", begin->data);
		begin = begin->next;
	}
	printf("NULL\n");
}
void PushBack(plist* pplist, DataType x)
{
	assert(pplist);
	if (*pplist == NULL)
	{
		(*pplist) = _CreateNode(x);
	}
	else
	{
		plist tmp = *pplist;
		plist endNode = _CreateNode(x);
		while (tmp->next != NULL)
		{
			tmp = tmp->next;
		}
		tmp->next = endNode;
		tmp = endNode;
	}
}
void PushFront(plist* pplist, DataType x)
{
	assert(pplist);
	if (*pplist == NULL)
	{
		(*pplist) = _CreateNode(x);
	}
	else
	{
		plist s = _CreateNode(x);
		s->next = *pplist;
		*pplist = s;
	}
}
int GetLength(plist list)
{
	int len = 0;
	while (list->next != NULL)
	{
		len++;
		list = list->next;
	}
	return len+1;
}
void PopFront(plist* pplist)
{
	assert(pplist);
	if ((*pplist) == NULL)
	{
		printf("The Link Is Empty\n");
	}
	else
	{
		*pplist = (*pplist)->next;
	}
}
void PopBack(plist* pplist)
{
	assert(pplist);
	if ((*pplist) == NULL)
	{
		printf("The Link Is Empty\n");
	}
	if ((*pplist)->next == NULL)
	{
		free(*pplist);
		*pplist = NULL;
	}
	else
	{
		plist prev = 0, end;
		end = *pplist;
		while (end->next != NULL)
		{
			prev = end;
			end = end->next;
		}
		free(end);
		prev->next=NULL;
	}
}
//返回值為NULL時,找不到此元素或者鏈表為空。反之。返回值為指向x的指針
Node* Find(plist list, DataType x)
{
	plist begin = list;
	while (begin != NULL)
	{
		if (begin->data == x)
		{
			return begin;
		}
		else
		{
			begin = begin->next;
		}
	}
	return NULL;
}
void Insert(plist* pplist, Node * n, DataType x)
{
	assert(pplist);
	//空表時插入值
	if (*pplist == NULL)
	{
		*pplist = _CreateNode(x);
	}
	n = Find(*pplist, 6);
	if (n == NULL)
	{
		printf("此鏈表中沒有此元素\n");
	}
	else
	{
		plist anyNode = _CreateNode(x);
		//一個節點時插入值
		if (n->next == NULL)
		{
			n->next = anyNode;
		}
		//多個節點時插入值
		else
		{
			anyNode->next = n->next;
			n->next = anyNode;
		}
	}
}
int Erase(plist* pplist, DataType x)
{
	assert(pplist);
	Node* n;
	if (*pplist == NULL)
	{
		printf("The Link Is Empty\n");
	}
	n = Find(*pplist, x);
	if (n == NULL)
	{
		printf("此鏈表中沒有此元素\n");
	}
	else
	{
		plist prev=0,tmp=0;
		tmp = *pplist;
		//刪除頭結點及包含處理僅僅有一個節點時的情況
		if ((*pplist)->data == x)
		{
			*pplist = (*pplist)->next;
			free(tmp);
			return 0;
		}
		//處理有多個節點的情況
		while (tmp != NULL && tmp->data != x)
		{
			prev = tmp;
			tmp = tmp->next;
		}
		prev->next = tmp->next;
		free(tmp);
	}
	return 0;
}
void Remove(plist* pplist, Node * n)
{
	assert(pplist);
	if (*pplist == NULL)
	{
		printf("The Link Is Empty\n");
	}
	n = Find(*pplist, 9);
	if (n == NULL)
	{
		printf("此鏈表中沒有此元素\n");
	}
	else
	{
		plist pre = 0, tmp;
		tmp = *pplist;
		if (*pplist == n)
		{
			*pplist = (*pplist)->next;
			free(tmp);
			return;
		}
		while (tmp != NULL && tmp != n)
		{
			pre = tmp;
			tmp = tmp->next;
		}
		pre->next = tmp->next;
		free(tmp);
	}
}
int EraseAll(plist* pplist, DataType x,DataType all)
{
	assert(pplist);
	Node* n;
	if (*pplist == NULL)
	{
		printf("The Link Is Empty\n");
	}
	n = Find(*pplist, 5);
	if (n == NULL)
	{
		printf("此鏈表中沒有此元素\n");
	}
	else
	{
		plist prev = 0, tmp = 0;
		tmp = *pplist;
		//刪除一個x時的情況
		if (all == 0)
		{
			if ((*pplist)->data == x)
			{
				*pplist = (*pplist)->next;
				free(tmp);
				return 0;
			}
			//處理有多個節點的情況
			while (tmp != NULL && tmp->data != x)
			{
				prev = tmp;
				tmp = tmp->next;
			}
			prev->next = tmp->next;
			free(tmp);
		}
		//刪除多個x的情況
		else
		{
			plist ptmp;
			//刪除且為頭結點的情況
			if ((*pplist)->data == x)
			{
				*pplist = (*pplist)->next;
				free(tmp);
			}
			ptmp = *pplist;
			while (ptmp != NULL)
			{
				while (ptmp != NULL && ptmp->data != x)
				{
					prev = ptmp;
					ptmp = ptmp->next;
				}
				if (ptmp == NULL)
				{
					break;
				}
				prev->next = ptmp->next;
				free(ptmp);
				ptmp = NULL;
				ptmp = *pplist;
			}
		}
	}
	return 0;
}
void DeleteNode(Node* n)
{
	if (n == NULL)
	{
		printf("Node Is NULL\n");
	}
	if (n->next == NULL)
	{
		PopBack(&n);
	}
	else
	{
		plist del = n->next;
		n->data = del->data;
		n->next = del->next;
		free(del);
	}
}
void InsertFront(Node* n, DataType x)
{
	Node* tmp = n;
	DataType ptmp = 0;
	if (tmp == NULL)
	{
		printf("Node Is NULL\n");
	}
	else
	{
		Node* begin = _CreateNode(x);
		begin->next = tmp->next;
		tmp->next = begin;
		ptmp = tmp->data;
		tmp->data = begin->data;
		begin->data =ptmp;
	}
}
void PrintEndToBegin(plist list)//遞歸
{
	if (list == NULL)
	{
		return;
	}
	else
	{
		PrintEndToBegin(list->next);
		printf("%d->", list->data);
	}
}

void Test()
{
	plist s;
	Node* pFind ;
	int ret = 0;
	InitLinklist(&s);
	PushBack(&s, 1);
	PushBack(&s, 2);
	PushBack(&s, 3);
	PrintLinklist(s);
	PushFront(&s, 1);
	PushFront(&s, 4);
	PushFront(&s, 5);
	PushFront(&s, 5);
	PushFront(&s, 6);
	PushFront(&s, 5);
	PushFront(&s, 1);
	PrintLinklist(s);
	ret = GetLength(s);
	printf("鏈表的長度為:%d\n", ret);
	PopFront(&s);
	PrintLinklist(s);
	PopBack(&s);
	PrintLinklist(s);
	PopFront(&s);
	pFind = Find(s, 6);
	Insert(&s, pFind, 9);
	PrintLinklist(s);
	pFind = Find(s, 9);
	Remove(&s, pFind);
	PrintLinklist(s);
	Erase(&s, 4);
	PrintLinklist(s);
	pFind = Find(s, 5);
	DeleteNode(pFind);
	PrintLinklist(s);
	pFind = Find(s, 3);
	InsertFront(pFind, 109);
	PrintLinklist(s);
	EraseAll(&s, 5,1);
	PrintLinklist(s);
	PrintEndToBegin(s);
}

void main()
{
	Test();
}


單鏈表