双向链表基本操作

//结点类型
typedef int ElemType
{
	ElemType data;
	struct Node *prior;
	struct Node *next;
}DList;
//初始化
void InitDList(DList *head)
{
	if(head == NULL)   exit(0);

	head->next = head->prior = head;
}
//创建新结点
static DList *_ApplyNode(ElemType val , DList *prior , DList *next)
{
	DList *s = (DList*)malloc(sizeof(DList));

	if(s == NULL)   return  NULL;
	s->data = val;
	s->prior = prior;
	s->next = next;

	return s;
}
//按位置插
void InterDListPos(DList *head , ElemType val , int pos)
{
	if(head == NULL)  exit(0);

	while(pos < 0)   return;

	DList *p = head;
	
	while(pos && p != NULL)
	{
		p=p->next;
		pos--;
	}
	if(p = NULL)   return;
	DList *s = _ApplyNode(val , p ,p->next);
	if(s == NULL)   return;

	if(p->next != NULL)
		p->next->prior = s;
	p->next = s;
}
//Show
void ShowDList(DList *head)
{
	if(head == NULL)   exit(0);

	while(p->next != NULL)
	{
		printf("%d-->",p->next->data);
		p = p->next;
	}
	printf("NULL\n");
	while(p != head)
	{
		printf("%d-->",p->data);
		p = p->prior;
	}
}
//头插
void InterDListHead(DList * head , ElemType val)
{
	InterDListPos(head , val , 0);
}
//尾插
void InterDListTail(DList * head , ElemType val)
{
	if(head == NULL) exit(0);

	while(p->next != NULL)
	{
		p=p->next;
	}
	if(p->next == NULL)
	DList *s = _ApplyNode(val , p ,NULL);
}
//按位置删除
void DeleteDListPos(DList *head , int pos)
{
	if(head == NULL)   exit(0);

	while(pos <0)  return NULL;

	DList *p = head;
	while(pos == 0)
	{
	}
	while(pos && p->next != NULL)
	{
		p = p->next;
		pos--;
	}
	if(p->next == NULL)  return;

	DList *q = p->next;

	p->next = q->next;

	if(p->next != NULL)  q->next->prior = p;
	free(q);
}
//头删
void DeleteDListHead(DList *head )
{
	DeleteDListPos (head , 0);
}
//尾删
void DeleteDListTail(DList *head)
{
	if(head == NULL)  exit(0);

	DList *p = head;

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

	p->next->prior =NULL;

	free(p);
}
//销毁
void DestroyDList(DList *head)
{
	if(head == NULL)   exit(0);

	while(head->next != NULL)
	{
		DeleteDListHead(head);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值