48-没有头结点的单链表

typedef int ElemType;

typedef struct Node
{
	ElemType data;
	struct Node *next;
}PointList;

没有头结点的单链表

#include "pointlist.h"
#include <string.h>
#include <stdlib.h>


static PointList* ApplyNode(ElemType val, PointList *nt)//申请新的结点 
{
	PointList *s = (PointList *)malloc(sizeof(PointList));
	if (s == NULL) return NULL;
	s->data = val;
	s->next = nt;
	return s;
	
}


void  InitPointList(PointList **pp)//初始化 
{
	if (pp == NULL)  exit(0);
	*pp = NULL;
	
}

bool InsertHead(PointList **pp, ElemType val)//头插入 
{
	if (pp == NULL) exit(0);
	*pp = ApplyNode(val, *pp);
	if (*pp == NULL) return false;
	return true;
	
}


bool  InsertPointList(PointList **pp, ElemType val, int pos)//按位置插入 
{
	if (pp == NULL) exit(0);
	if (pos < 0) return false;
	if (*pp == NULL || pos == 0)  return InsertHead(pp, val);
	PointList *p = *pp;
	while (pos > 1 && p != NULL)
	{
		pos--;
		p = p->next;
	}
	if (p == NULL) return false;
	PointList *newNode = ApplyNode(val, p->next);
	if (newNode == NULL) return false;
	p->next = newNode;
	return true;
	
}

bool InsertTail(PointList **pp, ElemType val)//尾插入 
{
	if (pp == NULL) exit(0);
	if (*pp == NULL) return InsertHead(pp, val);
	PointList *p = *pp;
	while (p->next != NULL) p = p->next;
	p->next = ApplyNode(val, NULL);
	return false;
	
}

bool DeleteHead(PointList **pp)//头删除 
{
	if (pp == NULL) exit(0);
	if (*pp == NULL) return false;
	PointList *q = *pp;
	*pp = q->next;
	free(q);
	return true;
	
}

bool  DeletePonitList(PointList **pp, int pos)//按位置删除 
{
	if (pp == NULL) exit(0);
	if (*pp == NULL || pos < 0) return false;
	if (pos == 0)
	{
		return DeleteHead(pp);
	}
	PointList *p = *pp;
	while (pos > 1 && p->next != NULL)
	{
		pos--;
		p = p->next;
	}
	if (p->next == NULL)  return false;  // pos越界
	PointList *q = p->next;
	p->next = q->next;
	free(q);
	return true;
	
}

bool DeleteTail(PointList **pp)//尾删除 
{
	if (pp == NULL) exit(0);
	if (*pp == NULL) return false;
	if ((*pp)->next == NULL) return DeleteHead(pp);
	PointList *p = *pp;
	PointList *q = p->next;
	while (q->next != NULL)
	{
		p = q;
		q = q->next;
	}
	p->next = NULL;
	free(q);
	return true;
	
}

bool DeleteValue(PointList **pp, ElemType val)//按值删除 
{
	if (pp == NULL) exit(0);
	if (*pp == NULL) return false;  // 进来就是空链表
	while (*pp != NULL && (*pp)->data == val)
	{
		DeleteHead(pp);
	}
	if (*pp == NULL) return true;  //  将链表删除成空链
	PointList *p = *pp;
	PointList *q = p->next;
	while (q != NULL)
	{
		if (q->data == val)
		{
			p->next = q->next;
			free(q);
			q = p->next;
		}
		else
		{
			p = q;
			q = q->next;
		}
	}
	return true;
	
}

void DestroyPointList(PointList **pp)//销毁 
{
	if (pp == NULL) exit(0);
	while (*pp != NULL)
	{
		DeleteHead(pp);
	}
}

//将不带头结点的链表转化为带头结点的链表,新思想 
bool  InsertPointList2(PointList **pp, ElemType val, int pos)
{
	if (pp == NULL) exit(0);
	if (pos < 0) return false;
	PointList vmNode;
	vmNode.next = *pp;
	PointList *p = &vmNode;
	while (pos && p != NULL)
	{
		pos--;
		p = p->next;
	}
	if (p == NULL) return false;
	PointList *newNode = ApplyNode(val, p->next);
	if (newNode == NULL) return false;
	p->next = newNode;
	*pp = vmNode.next;
	return true;
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林林林ZEYU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值