单链表

@  今天写了整整6个小时,在不断的出错找错,修改中,终于将单链表初步成型了,真的很累,代码中没有多余的注释,现在的版本还是有很多地方可以改进,可是今天真的很累了! 后期的完善和优化好之后,会加上详细注释! 大家可以先看看思路


@  单链表初成型


#  自定义头文件"LinkList.h",包含一些函数功能的声明;


#define  _CRT_SECURE_NO_WARNINGS 1

#ifndef __LINKLIST_H__//防止头文件的重定义
#define __LINKLIST_H__


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

typedef int DataType;//将int重命名,方便修改,如果元素是char只需要将int改为char;

enum Select
{
	EXIT,
	INSERT,
	PRINT,
	REMOVE,
	REMOVEALL,
	SEARCH,
	SORT,
	PUSHF,
	PUSHB,
	POPF,
	POPB,
	DOSTORY,
	EASER,

	
};

typedef struct LinkNode
{
	DataType data;
	struct LinkNode* next;
}LinkNode,*pLinkNode;//结点结构体

typedef struct LinkList
{
	LinkNode* pHead;//头结点指针
}LinkList ,*pLinkList;//链表


void InitLinkList(pLinkList pList);//初始化列表
void PrintList(pLinkList pList);//打印
void DostoryList(pLinkList pList);//free链表

void PopBack(pLinkList pList);//尾删
void PopFront(pLinkList pList);//头删
void PushBack(pLinkList pList,DataType x);//尾插
void PushFront(pLinkList pList,DataType x);//头插
void Insert(pLinkList pList,pLinkNode pos,DataType x);//指定位置前或者后插入元素


pLinkNode Find(pLinkList pList,DataType x);//查找
void Search(pLinkList pList,DataType x);//查找指定元素;


void Remove(pLinkList pList,DataType x);//删除指定元素
void RemoveAll(pLinkList pList,DataType x);//删除所有出现的指定元素
void Erase(pLinkList pList,pLinkNode pos);//删除指定位置的元素
void BubbleSort(pLinkList pList);//冒泡排序链表元素
void Exit();

#endif //__LINKLIST_H__

# 函数的测试部分 main();

#include"LinkList.h"


void Print()
{
	printf("\n————————————————————\n");
	printf("***************************************\n");
	printf("**** 0.退出                      ******\n");
	printf("**** 1. 插入元素                 ******\n");
	printf("**** 2. 打印链表                 ******\n");
	printf("**** 3. 删除指定元素             ******\n");
	printf("**** 4. 删除所有出现的指定元素   ******\n");
	printf("**** 5. 查找                     ******\n");
	printf("**** 6. 排序                     ******\n");
	printf("**** 7. 头插                     ******\n");
	printf("**** 8. 尾插                     ******\n");
	printf("**** 9. 头删                     ******\n");
	printf("**** 10. 尾删                    ******\n");
	printf("**** 11. 释放链表                   ******\n");
	printf("**** 12. 删除指定位置的元素      ******\n");
	printf("***************************************\n");
	printf("\n————————————————————\n");
}

void test()
{
	LinkList List = {0};
	
	int select = 0;
	DataType x = 0;
	DataType pos = 0;
	InitLinkList(&List);

	while(1)
	{
		Print();
		printf("请输入选项:> ");
		scanf("%d",&select);

		switch(select)
		{
		case EXIT:
			Exit();
			break;
		case INSERT:
			printf("请输入要在哪一个位置前插入:> ");
			scanf("%d",&pos);
			printf("请输入要插入的元素:> ");
			scanf("%d",&x);
			Insert (&List,Find(&List,pos),x);
			break;
		case PRINT:
			PrintList(&List);
			break;
		case REMOVE:
			Remove(&List,5);
			break;
		case REMOVEALL:
			RemoveAll(&List,1);
			break;
		case SEARCH:
			Search(&List,3);
			break;
		case SORT:
			BubbleSort(&List);
			break;
		case PUSHF:
			printf("请输入要插入的元素:> ");
			scanf("%d",&x);
			PushFront (&List,x);
			break;
		case PUSHB:
			printf("请输入要插入的元素:> ");
			scanf("%d",&x);
			PushBack (&List,x);
			break;
		case POPF:
			PopFront (&List);
			break;
		case POPB:
			PopBack (&List);	
			break;
		case EASER:
			printf("请输入指定位置元素:>\n");
			scanf("%d",&pos);
			Erase(&List,Find(&List,pos));
			break;
		case DOSTORY:
			DostoryList (&List);
			break;
		default:
			break;
		}
	}
}

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

#  函数的实现部分   LinkList.c

#include"LinkList.h"

void InitLinkList(pLinkList pList)
{
	assert(pList);
	pList->pHead = NULL;

}//初始化列表

void PrintList(pLinkList pList)
{
	pLinkNode cur = NULL;
	assert(pList);

	cur = pList->pHead ;
	while( cur!=NULL )
	{
		printf("%d->",cur->data );
		cur = cur->next ;
	}

	printf("over\n");
}//打印

void DostoryList(pLinkList pList)
{
	pLinkNode cur = NULL;
	pLinkNode tmp = NULL; 
	assert(pList);

	cur = pList->pHead ;

	if(pList ->pHead == NULL)
		return;

	while(cur)
	{
		tmp = cur->next ;
		free(cur);
		cur = NULL;
		cur = tmp;
	}
	pList ->pHead = NULL;
}
//free链表

void PopBack(pLinkList pList)
{
	pLinkNode cur = NULL;
	pLinkNode pvr = NULL;
	assert(pList);
    cur = pList->pHead ;

	if(pList ->pHead ==NULL)
		return;
	else if(cur ->next == NULL)
	{
		pList->pHead = cur->next;
	         free(cur);
	         cur = NULL;
	}
	else
	{
		while(cur->next)
		{
			pvr = cur;
			cur = cur->next ;
		}
		free(cur);
		pvr->next = NULL;
	}
}	
//尾删

void PopFront(pLinkList pList)
{
	pLinkNode cur = NULL;
	assert(pList);
	cur = pList ->pHead ;

	if(cur == NULL)
		return;
	pList->pHead = cur->next;
	free(cur);
	cur = NULL;
	
}//头删

void PushBack(pLinkList pList,DataType x)
{
	pLinkNode cur = NULL;
	pLinkNode pvr = NULL;
	pLinkNode newNode = (pLinkNode)malloc(sizeof(LinkNode ));
	if(newNode == NULL)
	{
		printf("out of memory\n");
		exit(0);	
	}
	assert(pList);
	cur = pList ->pHead ;
	newNode ->data = x;
	newNode ->next = NULL;

	if(cur == NULL)
	{
		pList ->pHead  = newNode ;
		return;
	}
	while(cur)
	{
		pvr = cur;
		cur = cur->next ;
	}
	pvr->next  = newNode ;

}//尾插

void PushFront(pLinkList pList,DataType x)
{
	pLinkNode cur = NULL;
	pLinkNode newNode = (pLinkNode)malloc(sizeof(LinkNode ));
	if(newNode == NULL)
	{
		printf("out of memory\n");
		exit(0);	
	}
	assert(pList);
	cur = pList ->pHead ;
	newNode ->data = x;
	newNode ->next = cur;
	pList ->pHead  = newNode ;

}//头插

void Insert(pLinkList pList,pLinkNode pos,DataType x)
{//将指定元素插入到指定位置前, 插入指定位置后类似;
	pLinkNode cur = NULL;
	pLinkNode pvr = NULL;
	DataType tmp = x;
	pLinkNode newNode = (pLinkNode )malloc(sizeof (LinkNode ));
	if(newNode == NULL)
	{
		printf("out of memory!\n");
		return ;
	}
	newNode ->data = tmp;
	assert(pList);
	cur = pList ->pHead ;
	if(cur == NULL)
	{
		PushFront (pList,x);
		return ;
	}
	if(pos == NULL)
	{
		printf("default locate!\n");
		return;
	}

	newNode ->next = pos->next ;
	pos->next = newNode ;

	tmp = pos->data ;
	pos->data = newNode ->data ;
	newNode ->data = tmp;

}//指定位置前或者后插入元素


pLinkNode Find(pLinkList pList,DataType x)//只找出第一次出现的x
{
	pLinkNode cur = NULL;
	assert(pList);
	cur = pList ->pHead ;

	while(cur)
	{
		if(cur->data == x)
			break;
		cur = cur->next ;
	}
	return cur;
}//公用的查找函数

void Search(pLinkList pList,DataType x)
{
	pLinkNode cur = NULL;
	assert(pList);
	cur = Find(pList, x);

	if(cur == NULL)
		printf("the x wasn't searched!\n");
	else
		printf("the search of result:> %d\n",cur->data );
}//查找指定元素;

void Remove(pLinkList pList,DataType x)//只删除第一个匹配到的元素;
{
	pLinkNode cur = NULL;
	assert(pList);
	cur = pList ->pHead ;

	while(cur)
	{
		if(cur->data == x)
		{
			if(cur == pList ->pHead )
				PopFront ( pList);
			else if(cur->next == NULL )
				PopBack ( pList);
			else
			{
				pLinkNode tmp = NULL;
				cur->data = cur->next ->data ;
				tmp = cur->next ;
				cur->next = cur->next ->next ;
				free(tmp);
				tmp = NULL;
			}
			printf("\n Delete success!\n");
			return;
		}
		cur = cur->next ;
	}
	printf("\nThe list haven't exist x!\n");

}//删除指定元素

void RemoveAll(pLinkList pList,DataType x)
{
	pLinkNode cur = NULL;
	pLinkNode pon = NULL;
	int flag = 0;
	assert(pList);
	cur = pList ->pHead ;

	while(cur)
	{
		
		if(cur->data == x)
		{
			flag = 1;
			pon = cur->next ;

			if(cur == pList ->pHead )
			{
				PopFront ( pList);
				cur = pon;
			}

			else if(cur->next == NULL )
			{
					PopBack ( pList);
					cur = NULL;
					break;
			}

			else
			{
				pLinkNode tmp = NULL;
				cur->data = cur->next ->data ;
				tmp = cur->next ;
				cur->next = cur->next ->next ;
				free(tmp);
				tmp = NULL;
			}
		}
		else 
			cur = cur->next ;
           
	}
	if(flag == 1)
		printf("\n Delete success!\n");
	else
	printf("\nThe list haven't exist x!\n");

}//删除所有出现的指定元素

void Erase(pLinkList pList,pLinkNode pos)
{
	pLinkNode cur = NULL;
	pLinkNode pvr = NULL;
	assert(pList);
	cur = pList ->pHead ;

	if(pos == NULL)
	{
		printf("\nThe list haven't exist x!\n");
		return ;
	}
	else if(pos == cur)
	{
		PopFront (pList);
		return ;
	}
	while(cur != pos)
	{
		pvr = cur;
		cur = cur->next ;
	}
	pvr->next = pos->next ;
	free(pos);
	pos = NULL;
}//删除指定位置的元素

void BubbleSort(pLinkList pList)//冒泡排序单链表
{
	pLinkNode p = NULL;
	pLinkNode q = NULL;
	assert(pList);

	for(p = pList ->pHead ;p!=NULL; p = p->next )
	{
		for(q = p->next;q!=NULL; q = q->next )
		{
			if(p->data > q->data )
			{
				DataType tmp = q->data ;
				q->data  = p->data ;
				p->data = tmp;
			}
		}
	}
}//冒泡排序链表元素

void Exit()
{
	exit(0);
}

累了还是要好好休息一下!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值