码迷,mamicode.com
首页 > 其他好文 > 详细

单链表

时间:2015-07-01 01:04:28      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

"Slist.h"

#pragma once

#define __Slist_H__
#ifdef __Slist_H__

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

typedef int DataType;

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

void InitSlist(PLinkList* ppList);
void PrintList(PLinkList pList);

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

int GetLength(PLinkList pList);

void PopFront(PLinkList* ppList);
void PopBack(PLinkList* ppList);

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


#endif//用来结束__Slist_H__



"Slist.c"

#include "Slist.h"

//创建一个节点
PLinkList _CreateNode(DataType x)
{
	PLinkList tmp = (PLinkList*)malloc(sizeof(Node));
	tmp->data = x;
	tmp->next = NULL;
	return tmp;
}

void InitSlist(PLinkList* ppList)
{
	assert(ppList);
	*ppList = 0;
}

void PrintList(PLinkList pList)
{
	PLinkList begin =pList;
		
	while (begin != NULL)
	{
		printf("%d->", begin->data);
		begin = begin->next;
	}
	printf("NULL\n");
}

void PushBack(PLinkList* ppList, DataType x)
{
	assert(ppList);
	PLinkList Head= *ppList;
	// 1.空链表
	// 2.一个及以上节点
	if (*ppList == NULL)
	{
		*ppList = _CreateNode(x);
		
	}
	else
	{
		while (Head->next != NULL)
		{
			Head = Head->next;
		}
		PLinkList tmp = _CreateNode(x);
		Head->next = tmp;
	}
}

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;
	}
}

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

	while (begin != NULL)
	{
		count++;
		begin = begin->next;
	}
	printf("count is %d\n",count);
	return count;
}

void PopFront(PLinkList* ppList)
{
	assert(ppList);
	if (*ppList == NULL)
	{
		printf("List Is Empty\n");
		return;
	}
	PLinkList tmp = *ppList;
	*ppList=(*ppList)->next;
	free(tmp);
}

void PopBack(PLinkList* ppList)
{
	assert(ppList);
	PLinkList Last = *ppList;
	if (*ppList == NULL)
	{
		printf("List Is Empty\n");
		return;
	}
	else
	{
		// 1.一个节点情况
		// 2.一个及以上节点的情况。
		if (Last->next == NULL)
		{
			free(Last);
			*ppList = NULL;
		}
		else
		{
			PLinkList begin = *ppList;
			while (begin->next->next != NULL)
			{
				begin = begin->next;
			}
			free(begin->next);
			begin->next = NULL;
		}
	}
}

Node* Find(PLinkList pList, DataType x)
{
	PLinkList target = pList;
	while (target != NULL)
	{
		if (target->data == x)
		{
			return target;
		}
		target = target->next;
	}
	printf("no this value\n");
	return NULL;
}

void Insert(PLinkList* ppList, Node* n, DataType x)
{
	PLinkList tmp;
	if (*ppList == NULL)
	{
		*ppList == _CreateNode(x);
		return;
	}
	tmp = _CreateNode(x);
	tmp->next = n->next;
	n->next = tmp;
}

int Remove(PLinkList* ppList, Node* n)
{
	assert(ppList);
	PLinkList begin = *ppList;
	PLinkList prev = 0;
	if (begin->next ==NULL&&(*ppList) == n)//只有一个节点且此节点恰好是要删除的节点
	{
		free(*ppList);
		*ppList = NULL;
		return 0;
	}
	else if ((*ppList) == n)//至少两个节点且第一个就是要找的节点
	{
		PLinkList tmp = *ppList;
		*ppList = (*ppList)->next;
		free(tmp);
		return 0;
	}
	//多个节点
	while (begin != NULL && begin != n)
	{
		prev = begin;
		begin = begin->next;
	}
	if (begin != NULL)
	{
		prev->next = n->next;
		free(n);
		return 0;
	}
	return -1;
}

"main.c"
#include "Slist.h"

void Test1()
{
	PLinkList pList;
	InitSlist(&pList);
	PushBack(&pList, 1);
	PushBack(&pList, 2);
	PushBack(&pList, 3);
	PrintList(pList);
	GetLength(pList);
	PopBack(&pList);
	PrintList(pList);
}

void Test2()
{
	PLinkList pList;
	InitSlist(&pList);
	PushFront(&pList, 1);
	PushFront(&pList, 2);
	PushFront(&pList, 3);
	PrintList(pList);
	GetLength(pList);
	PopFront(&pList);
	PrintList(pList);
}

void Test3()
{
	PLinkList pList;
	InitSlist(&pList);
	PushFront(&pList, 1);
	PushFront(&pList, 2);
	PushFront(&pList, 3);
	PrintList(pList);
	PLinkList ret = Find(pList,2);
	if (ret != NULL)
	{
		Insert(&pList, ret, 8);//ret已经接收了想找的位置,所以在
								//传参时把想要插入的位置参数改为ret
	}
	PrintList(pList);
}

void Test4()
{
	PLinkList pList;
	InitSlist(&pList);
	PushFront(&pList, 1);
	PushFront(&pList, 2);
	PushFront(&pList, 3);
	PLinkList ret = Find(pList, 2);
	Remove(&pList, ret);
	PrintList(pList);
}

void Test5()
{
	PLinkList pList;
	InitSlist(&pList);
	PushBack(&pList, 1);
	PushBack(&pList, 2);
	PushBack(&pList, 2);
	PushBack(&pList, 3);
	EraseAll(&pList,2,1);
	PrintList(pList);
}

void main()
{
	//Test1();
	//printf("\n");
	//Test2();
	//printf("\n");
	//Test3();
	//printf("\n");
	//Test4();
	//printf("\n");
	Test5();
	getchar();
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

单链表

标签:

原文地址:http://blog.csdn.net/lolita0917/article/details/46701211

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!