《数据结构》-链表的增删改查以及翻转链表(利用辅助空间)


```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node
{
	int iData;
	struct Node* iNext;
 };
void Add(struct Node **Phead, struct Node **Pend,int iData);//添加链表
void AddHead(struct Node** Phead, struct Node** Pend, int iData);//添加头链表
void Delete(struct Node** Phead, struct Node** Pend, int iData);//删除链表
void FreeAll(struct Node** Phead, struct Node** pend);//释放全链表
void OutPut(struct Node* Phead);//输出链表
int GetListCount(struct Node* Phead);//链表长度
struct Node * LookIndex(struct Node* Phead, int Index);//根绝下标并返回其头结点
void SwapIndex(struct Node* Phead, int Index1,int Index2);//根据下标交换数据
void ReverseData(struct Node* Phead);//原地翻转链表
void ReverseSpace(struct Node ** Phead, struct Node ** pend);//原地翻转链表借助辅助空间
int main()
{
	struct Node* Phead = NULL;
	struct Node* Pend = NULL;
	AddHead(&Phead, &Pend, 4);
	Add(&Phead,&Pend,3);
	Add(&Phead,&Pend,2);
	Add(&Phead,&Pend,9);
	//ReverseData(Phead);
	//OutPut(Phead);	ReverseData(Phead);
	ReverseSpace(&Phead,&Pend);
	OutPut(Phead);
}
void Add(struct Node** Phead, struct Node** Pend, int iData)
{
	struct Node* p = (struct Node*)malloc(sizeof(struct Node));
	p->iData = iData; p->iNext = NULL;
	if (*Phead == NULL)
		*Phead = p;
	else
		(*Pend)->iNext = p;
	*Pend = p;

}
void AddHead(struct Node** Phead, struct Node** Pend, int iData)
{
	struct Node* p = (struct Node*)malloc(sizeof(struct Node));
	p->iData = iData; p->iNext = NULL;
	if (*Phead == NULL)
	{
		*Phead = p;
		*Pend = p;
	}
	else
	{
		p->iNext = *Phead;
		*Phead = p;
	}

}
void Delete(struct Node** Phead, struct Node** Pend, int iData)
{
	if ((*Phead)->iData == iData)
	{
		struct  Node* py = *Phead;
		*Phead = py->iNext;
		free(py);

	}
	else 
	{
	
		struct Node* p = *Phead;
		while (p->iNext != NULL)
		{
			if (p->iNext->iData == iData)
				break;
			p = p->iNext;
		}
		if (p->iNext != NULL)
		{
			struct Node* pt = p->iNext;
			p->iNext = pt->iNext;
			free(pt);
		}
		else
			printf("查无此节点");
	}

}
void FreeAll(struct Node** Phead, struct Node** pend)
{
	struct Node* p = *Phead;
	while (p != NULL)
	{
		struct Node* py = p;
		p = p->iNext;
		free(py);
	 }
}
void OutPut(struct Node* Phead)
{
	while (Phead !=NULL)
	{
		printf("%d ", Phead->iData);
		Phead = Phead->iNext;
	}
	putchar('\n');
}
int GetListCount(struct Node* Phead)
{
	int sum = 0;
	while (Phead != NULL)
	{
		sum++;
		Phead = Phead->iNext;
	 }
	return sum;
}
struct Node	*LookIndex(struct Node* Phead, int Index)
{
	//参数合法性检测
	if (Phead == NULL || Index < 0)
	{
		printf("链表参数有误或者下标小于0");
		return NULL;
	}
	int i = 0;
	while (Phead!=NULL)
	{
		if (i == Index)
		{
			return Phead;
		 }
		Phead = Phead->iNext;
		i++;
	}
	printf("下标过大未找到");
	return NULL;
}
void SwapIndex(struct Node* Phead, int Index1, int Index2)
{
	if (Phead == NULL)
		return;
	struct Node* pt1 = LookIndex(Phead, Index1);
	struct Node* pt2 = LookIndex(Phead, Index2);
	if (pt1 != NULL && pt2 != NULL)
	{
		struct Node* p = pt1->iData;
		pt1->iData = pt2->iData;
		pt2->iData = p;

  }
}
void ReverseData(struct Node* Phead)
{
	if (Phead == NULL || Phead->iNext == NULL)
		return;
	int j = GetListCount(Phead)-1;
	for (int i = 0; i < j; i++, j--)
	{
		SwapIndex(Phead, i, j);
	}
}
void ReverseSpace(struct Node** Phead, struct Node** pend)
{
	if (*Phead == NULL || (*Phead)->iNext == NULL)
		return;
	struct  Node* p = *Phead;
	struct Node* PNewhead = NULL;
	struct Node* PNewend = NULL;
	while (p!= NULL)
	{
		AddHead(&PNewhead, &PNewend,p->iData);
		p = p->iNext;
	   }
	//释放原链表
	FreeAll(Phead, pend);
	//重新指向;
	*Phead = PNewhead;
	*pend = PNewend;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值