47-有头结点的单链表

typedef int ElemType;

typedef struct Node
{
	union {       //  共同体
		ElemType  data;
		int       length;
	};
	struct Node *next;
}HeadList;

有头结点的单链表

#include "headlist.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

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

void InitHeadList(HeadList * head)//初始化 
{
	if (head == NULL)  exit(0);
	head->next = NULL;
	head->length = 0;
	
}

int GetLength(HeadList *head)//求出长度 
{
	if (head == NULL) exit(0);
	int length = 0;
	HeadList *p = head->next;
	while (p != NULL)
	{
		length++;
		p = p->next;
	}
	return length;
	
}

void ShowList(HeadList *head)//输出链表值 
{
	if (head == NULL) exit(0);
	HeadList *p = head->next;
	while (p != NULL)
	{
		printf("%d  ", p->data);
		p = p->next;
	}
	printf("\n");
}

bool  InsertOFPos(HeadList *head, ElemType val, int pos)//按位置插入 
{
	if (head == NULL) exit(0);
	if (pos < 0) return false;  // pos和链表长度做对比
	HeadList *p = head;
	while (pos && p != NULL)
	{
		pos--;
		p = p->next;
	}
	if (p == NULL)  return false;
	HeadList *newNode = ApplyNode(val, p->next);
	if (newNode == NULL) return false;
	p->next = newNode;
	return true;
	
}

bool InsertOFHead(HeadList *head, ElemType val)//头插 
{
	if (head == NULL) return false;
	// 方法一
	return InsertOFPos(head, val, 0);
	/* 方法二
	HeadList *newNode = ApplyNode(val, head->next);
	if (newNode == NULL) return false;
	head->next = newNode;
	return true;
	*/
}

bool InsertOFTail(HeadList *head, ElemType val)//尾插 
{
	if (head == NULL) return false;
	//return InsertOFPos(head, val, GetLength(head));
	HeadList *p = head;
	while (p->next != NULL) p = p->next;
	p->next = ApplyNode(val, NULL);
	return true;
	
}

bool DeleteOFPos(HeadList *head, int pos)//按位置删除 
{
	if (head == NULL) exit(0);
	if (pos < 0) return false;
	HeadList *p = head;
	while (pos && p->next != NULL)
	{
		p = p->next;
		pos--;
	}
	if (p->next == NULL) return false;  // pos越界的情况
	HeadList *q = p->next;  // q就是要删除的节点
	p->next = q->next;
	free(q);
	return true;
	
}
bool DeleteOFHead(HeadList *head)//头删除 
{
	if (head == NULL) exit(0);
	return DeleteOFPos(head, 0);
	
}
bool DeleteOFTail(HeadList *head)//尾删 
{
	if (head == NULL) exit(0);
	if (head->next == NULL) return false;
	HeadList *p = head;
	HeadList *q = p->next;
	while (q->next != NULL)
	{
		p = q;
		q = q->next;
	}
	p->next = NULL;
	free(q);
	return true;
	
}

// 将所有重复的值都删除
bool DeleteOFValue(HeadList *head, ElemType val)
{
	if (head == NULL) exit(0);
	if (head->next == NULL)  return false;
	HeadList *p = head;
	HeadList *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 DestroyHeadList(HeadList *head)//销毁 
{
	if (head == NULL) exit(0);
	while (head->next != NULL)
	{
		DeleteOFHead(head);
	}
}
void ClearHeadList(HeadList *head)//清除 
{
	if (head == NULL) exit(0);
	DestroyHeadList(head);
	
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林林林ZEYU

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

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

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

打赏作者

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

抵扣说明:

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

余额充值