首页 > 编程语言 > 详细

C语言的链表-第二部分

时间:2020-04-03 23:53:35      阅读:126      评论:0      收藏:0      [点我收藏+]

无空头

#include"stdio.h"
#include"stdlib.h"
#include"windows.h"


/*链表的基本使用,不用改,直接用欢迎交流 QQ2473183730*/

struct Node
{
	int a;
	struct Node* pNEXT;
};

//链表头尾指针
struct Node* g_pHead = NULL;
struct Node* g_pEnd = NULL;

struct Node* Create_Node(int N)
{
	struct Node* pTemp = (struct Node*)malloc(sizeof(struct  Node));
	pTemp->a = N;
	pTemp->pNEXT = NULL;
	return pTemp;
}


 //////////添加数据函数  尾添加的方式
void AddNodeToList_End(int N)
{
	//创建一个节点
	struct Node* pTemp =Create_Node(N);

	//链接
	if (g_pHead == NULL)
	{
		g_pHead = pTemp;
		g_pEnd = pTemp;//此时首尾指针相同
	}
	else
	{
		g_pEnd->pNEXT = pTemp;//新的节点指向上一个节点的Next
		g_pEnd = pTemp;;//尾巴一直指向最后一个节点
	}
}


//////////添加数据函数  头添加的方式
void AddNodeToList_Head(int N)
{
	//创建一个节点
	struct Node* pTemp=Create_Node(N);


	//链接
	if (g_pHead == NULL)
			{
				g_pHead = pTemp;
				g_pEnd = pTemp;				//此时首尾指针相同
			}
	else
	{
		pTemp->pNEXT =g_pHead ;				//新的节点指向原来节点的Next
		g_pHead = pTemp;;					//头部一直指向最后一个节点
	}
}



//链表的遍历,全部输出
void List_SeeAll(struct Node* TouPlace)
{
	struct Node* CANSHU = TouPlace;
	if (CANSHU == NULL)
	{
		printf("当前链表为空");
		exit(0);
	}
	else
	{
		while (CANSHU!=NULL)
		{
			printf("%d\n", CANSHU->a);
			CANSHU = CANSHU->pNEXT;
		}
	}
}


//查找数据
struct Node* List_Search(struct Node* TouPlace,int se)
{
	struct Node* CANSHU = TouPlace;
	if (CANSHU == NULL)
	{
		exit(0);
		printf("当前链表为空\n");
	}
	else
	{
		while (CANSHU != NULL)
		{
			if (CANSHU->a == se)
			{
				return CANSHU;				//返回节点       
			}
			CANSHU = CANSHU->pNEXT;
		}
	}
	return NULL;							//没找到,返回NULL
}



//清空链表
void List_Free_All(struct Node* TouPlace)// TouPlace应为g_pHead
{
	struct Node* CANSHU = TouPlace;
	while (CANSHU != NULL)
	{
		struct Node* pt = CANSHU;			//构建中间参数
		CANSHU = CANSHU->pNEXT;				//先指向下一个,再释放
		free(pt);							//释放内存
	}
	//清空链表头尾指针
	g_pHead = NULL;
	g_pEnd = NULL;
}

//指定特定(位置)内容的后一位插入节点
void Add_List_Behind_Special(struct Node* TouPlace,int index, int N)
{
	if (TouPlace == NULL)
	{
		printf("此链表为空\n");
		return;
	}
	else 
	{
		struct Node* pt = List_Search(TouPlace, index);
		if (pt == NULL)
		{
			printf("没有指定节点\n");
			return;
		}
		else
		{
			struct Node* pTemp =Create_Node(N);

			if (pt == g_pEnd)
			{
				g_pEnd->pNEXT = pTemp;
				g_pEnd = pt;
			}
			else
			{
				//先连
				pTemp->pNEXT = pt->pNEXT;
				//后断 
				pt->pNEXT = pTemp;
			}
		}
	}
}


//指定特定(位置)内容的前一位插入节点
void Add_List_Front_Special(struct Node* TouPlace,int index, int N)
{
	if (TouPlace == NULL)
	{
		printf("此链表为空\n");
		return;
	}
	else
	{
		struct Node* pt = List_Search(TouPlace, (index-1));
		if (pt == NULL)
		{
			printf("没有指定节点\n");
			return;
		}
		else
		{
			struct Node* pTemp = (struct Node*)malloc(sizeof(struct  Node));
			pTemp->a = N;
			pTemp->pNEXT = NULL;

			//先连
			pTemp->pNEXT = pt->pNEXT;
			//后断 
			pt->pNEXT = pTemp;
		}
	}
}


//头删除 删除第一个节点
void Delete_List_Head(struct Node* TouPlace) // TouPlace应为g_pHead
{
	if (NULL == TouPlace)
	{
		printf("当前的链表为空\n");
		return;
	}
	else
	{
		g_pHead = TouPlace->pNEXT;
		free(TouPlace);
	}
}




// 尾删除
void Delete_List_End(struct Node* TouPlace, struct Node* WeiPlace)//TouPlace应为g_pHead,WiePlace 是g_pEnd
{
	if (NULL == TouPlace)
	{
		printf("当前的链表为空\n");
		return;
	}
	else
	{

		if (TouPlace == WeiPlace)//只有一个
		{
			free(g_pHead);
			g_pHead = NULL;
			g_pEnd = NULL;
		}
		else
		{
			//找到倒数第二个
			struct Node* pTemp = g_pHead;
			while (pTemp->pNEXT != g_pEnd)
			{
				pTemp = pTemp->pNEXT;
			}

			//删除 
			free(g_pEnd);
			g_pEnd = pTemp; 
			g_pEnd->pNEXT = NULL;
		}
	}
}

//删除任意节点
void Delete_List_Special(struct Node* TouPlace, struct Node* weiPlace, int index)//TouPlace应为g_pHead,WiePlace 是g_pEnd
{
	if (NULL == TouPlace)
	{
		printf("当前的链表为空\n");
		return;
	}
	else
	{
		struct Node* pTemp = List_Search(g_pHead, index);
		if (pTemp==NULL)
		{
			printf("查无此节点");
			return;
		}
		else
		{
			//只有一个节点
			if (TouPlace == weiPlace)
			{
				free(g_pHead);
				g_pHead = NULL;
				g_pEnd = NULL;

			}
			//有两个节点
			else if (TouPlace->pNEXT == weiPlace)
			{
				if (pTemp == g_pHead)
				{
					Delete_List_Head(g_pHead);
				}
				else
				{
					Delete_List_End(g_pHead, g_pEnd);
				}
			}

			//有多个节点
			else
			{
				if (pTemp == g_pHead)
				{
					Delete_List_Head(g_pHead);
				}
				else if(pTemp == g_pEnd)
				{
					Delete_List_Head(g_pHead);
					 
				}
				else
				{
				//找到被删除节点的前一个
					struct Node* pT = g_pHead;
					while (pT->pNEXT != pTemp)
					{
						pT = pT->pNEXT;
					}
					//链接
					pT->pNEXT = pTemp->pNEXT;
					//删除
					free(pTemp);
				}
			}
		}
	}
}

//特定位置的替换
void Delete_Remap_Special(struct Node* TouPlace, int se, int Aim)
{
	struct Node* UI = List_Search(TouPlace, se);
	if (UI == NULL)
	{
		printf("查找失败");
	}
	else
	{ 
	UI->a = Aim;
    }
}

int main(void)
{
	AddNodeToList_Head(7);
	AddNodeToList_Head(6);
	AddNodeToList_Head(5);
	AddNodeToList_Head(4);
	AddNodeToList_Head(3);
	AddNodeToList_Head(2);
	AddNodeToList_Head(1);
	

	Delete_List_Special(g_pHead,g_pEnd,5);
	List_SeeAll(g_pHead);
	
	system("pause");
	return 0;
}

  

C语言的链表-第二部分

原文:https://www.cnblogs.com/Loving-Q/p/12629649.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!