单链表_02_带头结点(自用)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
//3 4 5 6 7 9 10 -9999
typedef struct LNode {
	ElemType data;
	struct LNode* next;
}LNode,*Linklist;
bool initList(Linklist& L)
{
	L = (LNode *)malloc(sizeof(LNode));
	if (NULL == L)
	{
		return false;
	}
	L->next = NULL;
	return true;
}
bool Empty(Linklist L)
{
	return (NULL == L->next);
}
LNode* GetElem(Linklist L, int i)
{
	if (i < 0)
	{
		return NULL;
	}
	int j = 0;
	LNode* p = L;
	while (p && j < i)
	{
		p = p->next;
		j++;
	}
	return p;
}
LNode* locateElem(Linklist L, ElemType e)
{
	LNode* p = L->next;
	while (p && p->data != e)
	{
		p = p->next;
	}
	return p;
}
bool InsertNextNode(LNode* p,ElemType e)//当前节点后插
{
	if (NULL == p) return false;
	LNode* q = (LNode*)malloc(sizeof(LNode));
	if (NULL == q)
	{
		return false;
	}
	q->data = e;
	q->next = p->next;
	p->next = q;
	return true;
}
bool InsertPriorNode(LNode* p, ElemType e)
{
	if (NULL == p)	return false;
	LNode* q = (LNode*)malloc(sizeof(LNode));
	if (NULL == q) return false;
	q->next = p->next;
	p->next = q;
	q->data = p->data;
	p->data = e;
	return true;
}
//当传入的为一个节点插入前一个节点时
bool InsertPriorNOde_2(LNode* p, LNode* q)
{
	if (NULL == p || NULL == q)
		return false;
	q->next = p->next;
	p->next = q;
	ElemType temp = q->data;
	q->data = p->data;
	p->data = temp;
	return true;
}
bool Delete_IndexNode(Linklist L, int i, ElemType& e)
{
	LNode* p = GetElem(L,i-1);
	if (p == NULL) return false;
	LNode* q = p->next;
	if (q == NULL) return false;
	p ->next= q->next;
	e = q->data;
	free(q);
	q = NULL;
	return true;
}
bool Delete_ElemNode(Linklist L, ElemType e)//删除指定节点同理
{
	LNode* p=locateElem(L, e);
	if (NULL == p)
		return false;
	LNode* q = p->next;
	if (q == NULL)//当要删除元素为最后一个元素时
	{
		LNode* s = L->next;
		if (NULL == s) 
		{
			return false;
		}
		while (s->next->data != e)
		{
			s = s->next;
		}
		if (NULL == s)
		{
			printf("no == e\n");
			return false;
		}
		q = s->next;
		s->next = NULL;
		free(q);
		q = NULL;
		return true;
	}
	p->data = q->data;
	p->next = q->next;
	free(q);
	q = NULL;
	return true;
}
//按位序插入
bool List_IndexInsert(Linklist L, int i, ElemType e)//此处不用加&,删除的非头节点,插入删除不会改变头节点改变头部节点才需要引用
{
	LNode* p = GetElem(L, i - 1);
	InsertNextNode(p, e);
	return true;
}
bool Listlength(Linklist L, int& len)
{
	len = 0;
	LNode* p = L->next;
	if (NULL == p)
	{
		return true;
	}
	while (p)
	{
		len++;
		p = p->next;
	}
	return true;
}
void printlist(Linklist L)
{
	L = L->next;
	if (L == NULL)
	{
		printf("空链表\n");
		return;
	}
	while (L)
	{
		printf("%4d", L->data);
		L = L->next;
	}
	printf("\n");
}
bool List_HeadInsert(Linklist& L)
{
	int number;
	printf("INPUT number: \n");
	scanf("%d", &number);
	while (number!=-9999)
	{
		LNode* s = (LNode*)malloc(sizeof(LNode));
		if (NULL == s) return false;
		s->data = number;
		s->next = L->next;
		L->next = s;
		scanf("%d", &number);
	}
	return true;
}
bool List_TailInsert(Linklist& L)
{
	int number;
	LNode* p = L;
	printf("INPUT number: \n");
	scanf("%d", &number);
	while (number != -9999)
	{
		LNode* s = (LNode*)malloc(sizeof(LNode));
		if (NULL == s)	return false;
		s->data = number;
		p->next = s;
		p = s;
		scanf("%d", &number);
	}
	p->next = NULL;
	return true;
}
int main()
{
	Linklist L; int len=0;
	//if-else 省略
	initList(L);
	if (Empty(L))
	{
		printf("Empty List\n");
	}
	List_TailInsert(L);
	printlist(L);
	Listlength(L, len);
	printf("List_length=%d\n",len);
	List_IndexInsert(L, 3, 60);
	printlist(L);
	LNode* p=GetElem(L,4);
	InsertNextNode(p, 40);
	printlist(L);
	Listlength(L, len);
	p = locateElem(L, 9);
	InsertPriorNode(p, 70);
	printlist(L);
	LNode* s = (LNode*)malloc(sizeof(LNode));
	s->data = 50;
	InsertPriorNOde_2(p, s);
	printlist(L);
	ElemType e = -1;
	Delete_IndexNode(L, 1, e);
	printlist(L);
	Delete_ElemNode(L, 10);
	printlist(L);
	Listlength(L, len);
	printf("List_length=%d\n", len);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值