单链表基本操作的实现,初始化,头插,尾插,判空,获取个数,查找,删除,获取前置和后置位,清空,销毁

目录

一.单链表的设计

二.单链表的实现

三.单链表的总结


一.单链表的设计

1.单链表的结构定义:

typedef struct Node{

   int data;//数据域
   struct Node* next;//后继指针

}Node,*List;

2.单链表的设计示意图:

3.注意,单链表的最后一个节点的next域为NULL;

4.为什么要有一个头节点?(简单方便,不用传二级指针);

二.单链表的实现

//初始化
void InitList(List plist)
{
	assert(plist != NULL);
	if (plist == NULL)
		return;
	//plist->data;//头节点的数据域不使用
	plist->next = NULL;
}

//考试重点
//头插
bool Insert_head(List plist, int val)
{
	assert(plist != NULL);
	if (plist == NULL)
		return false;
	//动态申请一个节点
	Node* p = (Node *)malloc(sizeof(Node));
	assert(p != NULL);

	//将数据val放入到新节点
	p->data = val;

	//插入新节点
	p->next = plist->next;
	plist->next = p;

	return true;
}

//考试重点
//尾插
bool Insert_tail(List plist, int val)
{
	assert(plist != NULL);
	if (plist == NULL)
		return false;
	//申请节点
	Node* p = (Node*)malloc(sizeof(Node));
	assert(p != NULL);
	//将数据val放入到新节点
	p->data = val;
	//找尾巴
	Node* q;
	for(q=plist;q->next!=NULL;q=q->next)
   {
		;
	}
	//插入新节点
	p->next = q->next;//p->next=NULL;
	q->next = p;
	return true;
}

//插入数据,在plist链表的pos位置插入val;
bool Insert(List plist, int pos, int val)
{
	assert(plist != NULL);
	if (plist == NULL)
		return false;

	if (pos<0 || pos>GetLength(plist))
	{
		return false;
	}
	Node* p = (Node*)malloc(sizeof(Node));
	assert(p != NULL);
	p->data = val;
	//找到位置
	Node* q;
	int i;
	for (q = plist, i = 0; i < pos; i++, q = q->next)
	{
		;
	}
	// 插入
	p->next = q->next;
	q->next = p;

	return true;
}

//判空
bool IsEmpty(List plist)
{
	assert(plist != NULL);
	if (plist == NULL)
		return false;

	return plist->next == NULL;
}

//获取数据节点的个数
int GetLength(List plist)
{
	assert(plist != NULL);
	if (plist == NULL)
		return 0;
	int count = 0;
	for (Node* p = plist->next; p!= NULL; p = p->next)
	{
		count++;
	}
	return count;

}

//在plist中查找第一个key值,找到返回节点地址,没有找到返回NULL;
Node* Search(List plist, int key)
{
	assert(plist != NULL);
	if (plist == NULL)
		return NULL;
	
	for (Node* p = plist->next; p != NULL; p = p -> next)
	{
		if (p->data == key)
		{
			return p;
		}
	}
	return NULL;
}

//删除pos位置的值
bool DelPos(List plist, int pos)
{
	assert(plist != NULL);
	if (plist == NULL)
		return false;
	if (pos < 0 || pos >= GetLength(plist))
	{
		return false;
	}
	Node* p;
	int i;
	for (p = plist, i = 0; i < pos; i++, p = p->next)
	{
		;
	}
	//删除p后面的节点
	Node* q = p->next;
	p->next = q->next;
	free(q);

	return true;
}


//考试重点
//删除第一个val的值
bool DelVal(List plist, int val)
{
	Node* p = GetPrio(plist, val);
	if (p == NULL)
		return false;
	Node* q = p->next;
	//删除q
	p->next = q->next;//p->next=p->next->next;
	//释放q
	free(q);

	return true;
}

//返回key的前驱地址,如果不存在返回NULL;
Node* GetPrio(List plist, int key)
{
	for (Node* p = plist; p->next != NULL; p = p->next)
	{
		if (p->next->data == key)
			return p;
	}
	return NULL;
}

//返回key的后继地址,如果不存在返回NULL;
Node* GetNext(List plist, int key)
{
	assert(plist != NULL);
	if (plist == NULL)
		return NULL;

	Node* p = Search(plist, key);
	if (p == NULL)
		return NULL;

	return p->next;
}

//输出
void Show(List plist)
{
	//注意,头节点不能访问data
	for (Node* p = plist->next; p != NULL; p = p->next)
	{
		printf("%d ", p->data);
	}
	printf("\n");
}

//清空数据
void Clear(List plist)
{
	Destroy(plist);
}

void Destroy(List plist)
{
	//总是删除第一个数据节点
	Node* p;
	while (plist->next != NULL)
	{
		p = plist->next;
		plist->next = p->next;
		free(p);

		//error
		//plist->next = plist->next->next;
		//free(plist->next);
	}
}

三.单链表的总结

1.单链表的特点:
头插,头删 时间复杂度是O(1)
尾插,尾删 时间复杂度是O(n)

2.P初始化成什么?

如果我们要修改表的结构(或者说依赖于前驱,比如插入,删除):遍历:

for(Node *p=plist;p->next!=NULL;p=p->next)

如果我们不修改表的结构(或者说不依赖于前驱, 比如求长度,打印,查找) :遍历:

for (Node* p = plist->next; p != NULL; p = p->next)

3.注意考点:头插,尾插,按值删除;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值