循环双向链表的·插入、删除

#include"stdio.h"
#include"stdlib.h"
typedef int Status;
typedef int ElemType;
typedef struct MyStruct
{
	ElemType data;
	MyStruct *prior;
	MyStruct *next;

}*dlist,node;
/*
	//获取指定节点地址
	//位置不当是返回NULL
*/
dlist GetPointer(dlist L,int pos){
	dlist p=L->next;
	int i=0;
	while((p!=L)&&i<pos-1){
		p=p->next;
		i++;
	}
	if (p==L||pos<1) return NULL;
	return p;
}
//插入
void Insert(dlist L,int pos,ElemType e){
	dlist p=GetPointer(L,pos-1);
	if (!p)
	{
		dlist pn=(dlist)malloc(sizeof(node));
		pn->next=p->next;
		pn->prior=p->next->prior;
		p->next->prior=pn;
		p->next=pn;
		pn->data=e;
	}
}
//删除
void Dele(dlist L,int pos,ElemType &e){
	dlist p=GetPointer(L,pos-1),pf;
	if(!p||p==L||pos<1) exit(0);
	pf=p->next;
	e=p->next->data;
	p->next->next->prior=p;
	p->next=p->next->next;
	free(pf);
}

猜你喜欢

转载自blog.csdn.net/qq_42777577/article/details/84582379