0
点赞
收藏
分享

微信扫一扫

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

蓝莲听雨 2022-03-20 阅读 41
c++
#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 date;
	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->date != 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->date = 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->date = p->date;
	p->date = 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->date;
	q->date = p->date;
	p->date = temp;
	return true;
}
bool Delete_IndexNode(Linklist& L, int i, ElemType& e)
{
	LNode* p = GetElem(L,i-1);
	if (p == NULL||p->next== NULL) return false;
	LNode* q = p->next;
	p ->next= q->next;
	e = q->date;
	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->date != e)
		{
			s = s->next;
		}
		if (NULL == s)
		{
			printf("no == e");
			return false;
		}
		q = s->next;
		s->next = NULL;
		free(q);
		q = NULL;
		return true;
	}
	p->date = q->date;
	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);
	if (NULL == p)
		return false;
	LNode* q = (LNode*)malloc(sizeof(LNode));
	if (NULL == q) 
		return false;
	q->date = e;
	q->next = p->next;
	p->next = q;
	return true;
}
bool Listlength(Linklist L, int& len)
{
	len = 0;
	LNode* p = L->next;
	if (NULL == p)
	{
		return false;
	}
	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->date);
		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->date = 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->date = 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->date = 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 条评论