0
点赞
收藏
分享

微信扫一扫

线性表-带头结点的单链表

梦幻之云 2022-01-26 阅读 80

链表的特点是一种物理存储结构上非连续、非顺序的存储结构。

为了表示每个数据节点与其后继节点之间的逻辑关系,每个节点被分为数据域和指针域。

数据域用来存储当前数据元素的信息,指针域用来存储下一个节点的地址。

下面是带头结点的单链表的结构:

结构体设计:

typedef int ELEM_TYPE;
	
typedef struct Node
{
	ELEM_TYPE data; //数据域       (1.头结点: 不保存任何数据   2.有效数据节点:保存有效值)
	struct Node* next; 	//指针域       (1.头结点: 保存下一个元素的地址   2.有效数据节点: 存放下一个有效数据节点的地址)

} Node, * PNode;

单链表具体函数功能实现方法

1.头插

首先,申请一个指针pnewnode,

然后让pnewnode的next域指向有效数据节点的开始地址,

最后,在修改头节点的next域,将其指向pnewnode的地址。

注:不能先断开plist的next域,会导致无法找到后继节点的地址。

bool Insert_head(struct Node *plist, ELEM_TYPE val)
{
	//1.判断参数合法性
	assert(plist != NULL);
	if(NULL == plist)
	{
		return false;
	}

	//2.1申请新节点 (插入一个节点, malloc申请一个节点)
	struct Node*pnewnode = (struct Node*)malloc(1 * sizeof(struct Node));
	assert(pnewnode != NULL);
	if(pnewnode == NULL)
	{
		return false;
	}
	//2.2将val值赋值给新节点
	pnewnode->data = val;
	//pnewnode->next = NULL; //?

	//3.找到合适的插入位置  (因为是头插函数, 所以直接可以得到合适的插入位置)

	//4.插入
	pnewnode->next = plist->next;//因为plist的next域指向首元素地址
	plist->next = pnewnode;

	return true;
}

2.尾插

首先,申请一个指针pnewnode

其次,用一个指针p来指向尾结点并将其next域指向pnewnode的地址

最后将pnewnode的next域指向NULL

    struct Node *p = plist;
	for(p; p->next!=NULL; p=p->next);
	//此时p指向尾结点

	//插入
	pnewnode->next = p->next; //pnewnode->next = NULL;
	p->next = pnewnode;

3.按位置插入

首先申请新节点pnewnode, 然后用临时指针p指向待插入位置,然后修改p->next和pnewnode->next域的指向。

bool Insert_pos(struct Node* plist, int pos, ELEM_TYPE val)
{
	assert(plist != NULL);
	assert(pos >= 0 && pos <= Get_length(plist));
	if (plist == NULL)
	{
		return false;
	}
	if (pos<0 || pos>Get_length(plist))
		return false;
	if (pos == 0)
	{
		Insert_head(plist, val);
		return true;
	}
	if (pos == Get_length(plist))
	{
		Insert_tail(plist, val);
		return true;
	}
	//购买新节点
	struct Node* pnewnode = BuyNode(val);
	//找到插入位置
	struct Node* p = plist;
	for (int i = 0; i < pos; i++)
	{
		p = p->next;
	}
	//插入值
	pnewnode->next = p;
	p->next = pnewnode;

}

4.删除头结点

先用一个临时指针p指向待删除节点,然后再跨越指向

:首先要判断是否为空链表

bool Del_head(struct Node* plist)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return false;
	}
//判空
	if (IsEmpty(plist))
		return false;
	//找到删除位置
	struct Node* p = plist->next;
	plist->next = p->next;
	free(p);
}

 

 

5.尾节点删除 

首先让临时指针p 指向待删除节点

其次用另一个临时指针q指向待删除节点的前一个节点

最后修改q->next为空,释放尾结点

//尾删除 
bool Del_tail(struct Node* plist)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return false;
	}
	//判空
	if (IsEmpty(plist))
	{
		return false;
	}
	struct Node* p = plist;
	for (; p->next!= NULL; p = p->next);
	struct Node* q = plist;
	for (; q->next != p; q = q->next);

	q->next = NULL;
	free(p);

}

 6.按位置删除

先用一个临时指针p指向待删除节点,在用另外一个指针q指向p的前一个节点

然后跨越指向,最后释放p,完成删除操作。

bool Del_pos(struct Node* plist, int pos)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return false;
	}
	//判空
	if (IsEmpty(plist))
	{
		return false;
	}
	//用一个临时指针p指向待删除节点
	struct Node* p = plist;
	for (int i = 0; i < pos; i++)
	{
		p = p->next;
	}
	//用一个指针q指向待删除节点的前一个节点
	struct Node* q = plist;
	for (; q->next != p; q = q->next);
	
	q->next = p->next;
	free(p);

}

 

 单链表所有代码如下:

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "list.h"




//带头结点的单链表有哪些操作函数:
//初始化函数(对于头结点进行赋初值)
void Init_list(struct Node *plist)
{
	assert(plist != NULL);
	if(NULL == plist)
	{
		return;
	}
	//plist->data;  头结点的数据域不需要赋值
	plist->next = NULL;
}

//购买一个新节点
struct Node *BuyNode(ELEM_TYPE val)
{
	struct Node*pnewnode = (struct Node*)malloc(1 * sizeof(struct Node));
	assert(pnewnode != NULL);
	if(pnewnode == NULL)
	{
		return NULL;
	}
	
	pnewnode->data = val;
	pnewnode->next = NULL; 

	return pnewnode;
}

//头插
bool Insert_head(struct Node *plist, ELEM_TYPE val)
{
	//1.判断参数合法性
	assert(plist != NULL);
	if(NULL == plist)
	{
		return false;
	}

	//2.1申请新节点 (插入一个节点, malloc申请一个节点)
	struct Node*pnewnode = (struct Node*)malloc(1 * sizeof(struct Node));
	assert(pnewnode != NULL);
	if(pnewnode == NULL)
	{
		return false;
	}
	//2.2将val值赋值给新节点
	pnewnode->data = val;
	//pnewnode->next = NULL; //?

	//3.找到合适的插入位置  (因为是头插函数, 所以直接可以得到合适的插入位置)

	//4.插入
	pnewnode->next = plist->next;//因为plist的next域指向首元素地址
	plist->next = pnewnode;

	return true;
}

//尾插
bool Insert_tail(struct Node *plist, ELEM_TYPE val)
{
	//assert

	//1.购买新节点
	struct Node * pnewnode = BuyNode(val);
	assert(pnewnode != NULL);
	if(NULL == pnewnode)
	{
		return false;
	}

	//2.找到合适的插入位置
	struct Node *p = plist;//因为指针p在for循环外面,还要使用,所以定义在for外面
	for(p; p->next!=NULL; p=p->next);
	//此时p指向尾结点

	//3.插入
	pnewnode->next = p->next; //pnewnode->next = NULL;
	p->next = pnewnode;

	return true;
}

//按位置插入(pos=0 相当于头插  pos==length 相当于尾插)
bool Insert_pos(struct Node *plist, int pos, ELEM_TYPE val)
{
	assert(plist != NULL);
	assert(pos >=0 && pos<=Get_length(plist));

	//1.购买新节点
	struct Node * pnewnode = BuyNode(val);
	assert(pnewnode != NULL);
	if(NULL == pnewnode)
	{
		return false;
	}

	//2.找到合适的插入位置   
	struct Node *p = plist; 
	for(int i=0; i<pos; i++)
	{
		p=p->next;
	}

	//3.插入
	pnewnode->next = p->next;
	p->next = pnewnode;

	return true;
}

//头删
bool Del_head(struct Node *plist)
{
	//assert
	if(IsEmpty(plist))//删除需要判空
		return false;


	struct Node *p = plist->next;
	plist->next = p->next;//plist->next = plist->next->next;

	free(p);
	return true;
}

//尾删
bool Del_tail(struct Node *plist)
{
	//assert
	if(IsEmpty(plist))   //删除需要判空
		return false;

	struct Node *p = plist;
	for(p; p->next!=NULL; p=p->next); // 此时p指向尾结点

	struct Node *q = plist;
	for(q; q->next!=p; q=q->next); // 此时q停在尾结点的前面

	q->next = p->next;//q->next = NULL;
	free(p);
	return true;
}

//按位置删(pos==0 相当于头删   pos==length-1 相当于尾删(pos==length非法))
bool Del_pos(struct Node *plist, int pos)
{
	assert(plist != NULL);
	assert(pos >=0 && pos<Get_length(plist));//length这个位置 可以插不能删
	if(IsEmpty(plist))   //删除需要判空
		return false;

	/*struct Node *p = plist;
	for(int i=0; i<=pos; i++)
	{
		p=p->next;
	}*/

	struct Node *q = plist;
	for(int i=0; i<pos; i++)
	{
		q=q->next;
	}
	struct Node *p = q->next;

	q->next = p->next;
	free(p);
	return true;
}

//按值删
bool Del_val(struct Node *plist, ELEM_TYPE val)
{
	//assert
	struct Node *p = Search(plist, val);
	if(p == NULL)
	{
		return false;
	}

	struct Node *q = plist;
	for(q; q->next!=p; q=q->next);
	//q在待删除节点p的前面

	q->next = p->next;
	free(p);
	return true;
}

//获取值位置  (如果val值存在, 则返回其地址  不然返回NULL)
struct Node* Search(struct Node *plist, ELEM_TYPE val)
{
	//assert
	for(struct Node *p = plist->next; p!=NULL; p=p->next)
	{
		if(p->data == val)
		{
			return p;
		}
	}
	
	return NULL;
}

//判空
bool IsEmpty(struct Node *plist)
{
	//assert

	return plist->next == NULL;
}

//判满  单链表不存在满这个概念

//获取单链表有效数据节点个数
int Get_length(struct Node *plist)
{
	//assert
	int count = 0;
	for(struct Node *p=plist->next; p!=NULL; p=p->next)
	{
		count++;
	}

	return count;
}

//清空  相当于直接调用销毁
void Clear(struct Node *plist)
{
	Destroy(plist);
}

//销毁1(malloc申请来的空间 全部释放掉)
void Destroy(struct Node *plist)
{
	//assert
	//一直循环判断(判断单链表里还有没有节点,如果有,则头删一次)
	/*while(!IsEmpty(plist))
	{
		Del_head(plist);
	}
	plist->next = NULL;*/

	while(plist->next != NULL)
	{
		struct Node *p = plist->next;
		plist->next = p->next;
		free(p);
	}
	plist->next = NULL;
}

//销毁2
void Destroy2(struct Node *plist)
{
	//assert

	struct Node *p = plist->next;
	struct Node *q = NULL;//q先不要赋值为p->next  因为p有可能指向NULL 

	plist->next = NULL;//接下来 就和我头结点没有任何关系了

	while(p != NULL)
	{
		q = p->next;
		free(p);
		p = q;
	}

}

//打印
void Show(struct Node *plist)
{
	for(struct Node *p=plist->next; p!=NULL; p=p->next)
	{
		printf("%d ", p->data);
	}
	printf("\n");
}
举报

相关推荐

0 条评论