双链表的实现

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>

typedef int elemtype;

typedef struct DNode
{
	union
	{
		int data;
		int length;
	};
	struct DNode* next;
	struct DNode* prior;
}DNode, * PDNode;



//初始化
void Init_list(PDNode plist);

//头插
bool Insert_head(PDNode plist, int val);

//尾插
bool Insert_tail(PDNode plist, int val);

bool Del_head(PDNode plist);
bool Del_tail(PDNode plist);
//按位置插入
bool Insert_pos(PDNode plist, int pos, int val);

//按位置删除

bool Del_pos(PDNode  plist, int pos);

//按值删除 遇到值为val的第一个有效节点,返回其地址
PDNode Del_val(PDNode plist, int val);

//寻找值为val的节点的前驱
PDNode prior(PDNode plist, int val);

//寻找值为val节点的后继
PDNode Get_next(PDNode plist, int val);

//判空
bool IsEmpty(PDNode plist);

//获取其有效长度
int Get_length(PDNode plist);

//清空
void Clear(PDNode plist);

//销毁
void Destory(PDNode plist);
void Destory2(PDNode plist);

//打印
void Show(PDNode plist);

//初始化
void Init_list(PDNode plist)
{
	assert(plist != NULL);
	plist->next = NULL;
	plist->prior = NULL;
}

//头插
bool Insert_head(PDNode plist, int val)
{
	assert(plist != NULL);
	DNode* pnewnode = (DNode*)malloc(sizeof(DNode));
	assert(pnewnode != NULL);
	pnewnode->data = val;
	pnewnode->next = plist->next;
	pnewnode->prior = plist;

	//3.0插入 (双向链表的头插比较特殊,因为有可能只需要更改3条线)
	//3.1先处理自身两条线 
	//3.2再处理特殊的那条线(下一个的节点的前驱线)
	//3.3再处理头结点的next线
	if (plist->next != NULL)//有可能是个空链表,导致不需要更改下一个节点的前驱指针
	{
		plist->next->prior = pnewnode;
	}
	plist->next = pnewnode;
	return true;
}


//按位置插入
bool Insert_pos2(PDNode  plist, int pos, int val)
{
	assert(plist != NULL);
	if (pos<0 || pos>Get_length(plist))return false;
	DNode* s = (DNode*)malloc(sizeof(DNode));
	DNode* p = plist;
	assert(s != NULL);
	int i = 0;
	s->data = val;
	while (i < pos)
	{
		p = p->next;
		i++;
	}
	s->next = p->next;//这两步位置不能调换,为了保证后边的节点不丢失
	s->prior = p;
	p->next->prior = s;
	p->next = s;
	return true;
}

//尾插
bool Insert_tail(PDNode plist, int val)
{
	assert(plist != NULL);
	DNode* pnewnode = (DNode*)malloc(sizeof(DNode));
	assert(pnewnode != NULL);
	pnewnode->data = val;
	DNode* p = plist;
	while (p->next != NULL)
	{
		p = p->next;
	}
	pnewnode->next = p->next;
	pnewnode->prior = p;
	p->next = pnewnode;
	return true;
}
bool Del_head(PDNode plist)
{
	Del_pos(plist, 0);
	return true;
}
bool Del_tail(PDNode plist)
{
	Del_pos(plist, Get_length(plist)-1);
	return true;
}

//按位置删除
bool Del_pos(PDNode plist, int pos)
{
	assert(plist != NULL);
	DNode* p = plist;
	int i = 0;
	while (i < pos)
	{
		p = p->next;
		i++;
	}
	DNode* q = p->next;//q指针必须定义在此处,不可在初始位置定义
    p->next = q->next;
	if (q->next != NULL)
	{
		q->next->prior = p;
	}
	free(q);
	q = NULL;
	return true;
}

//寻找值为val的节点的前驱
PDNode Get_prior(PDNode plist, int val)
{
	assert(plist != NULL);
	DNode* p = plist->next;
	int i = 0;
	while (p != NULL)
	{
		if (p->data == val)
		{
			return p->prior;
			break;
		}
		else
			p = p->next;
	}
	return NULL;
}

//寻找值为val节点的后继
PDNode Get_next(PDNode plist, int val)
{
	assert(plist != NULL);
	DNode* p = plist->next;
	int i = 0;
	while (p != NULL)
	{
		if (p->data == val)
		{
			return p->next;
			break;
		}
		else
			p = p->next;
	}
	return NULL;
}

//按值删除 遇到值为val的第一个有效节点,返回其地址
 PDNode Del_val(PDNode plist, int val)
{
	assert(plist != NULL);
	DNode* p = Get_prior(plist, val);
	assert(p != NULL);
	DNode* q = p->next;
	p->next = q->next;
	if (q->next != NULL)
	{
		q->next->prior = p;
	}
	free(q);
	q = NULL;
	return NULL;
}


//判空
 bool IsEmpty(PDNode plist)
 {
	 return plist->next = NULL;
}

//获取其有效长度
int Get_length(PDNode plist)
{
	 int count = 0;
	 DNode* p = plist->next;
	 while (p != NULL)
	 {
		 p = p->next;
		 count++;
	 }
	 return count;
}

//清空
void Clear(PDNode plist)
{
	DNode* p = plist->next;
	while (p != NULL)
	{
		Del_pos(plist, 0);
		p = p->next;
	}
}

//销毁
void Destory(PDNode plist)
{
	while (plist->next != NULL)//每次判断第一有效节点是否存在
	{
		DNode* p = plist->next;
		plist->next = p->next;
		free(p);
	}
	plist->next = NULL;//此时没有有效节点,那么将plist->next置NULL;
}

//打印
void Show(PDNode plist)
{
	assert(plist != NULL);
	DNode* p = plist->next;
	while (p != NULL)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}

int main()
{
	DNode ps;
	Init_list(&ps);
	for (int i = 0; i < 10; i++)
	{
		Insert_tail(&ps, i + 1);
	}
	Show(&ps);
	Insert_pos2(&ps, 2, 100);
	Show(&ps);
	printf("%d\n", Get_length(&ps));
	Del_pos(&ps, 3);
	Show(&ps);
	Del_val(&ps,8);
	Show(&ps);
	printf("%d\n", Get_length(&ps));
	for (int i = 0; i < 4; i++)
	{
		Del_head(&ps);
	}
	Show(&ps);
	printf("%d\n", Get_length(&ps));
	Del_tail(&ps);
	Show(&ps);
	//Clear(&ps);
	//Show(&ps);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值