数据结构之线性表(三)

//双向链表的存储结构
typedef struct Node{
	DataType data;
	struct Node *prior;
	struct NOde *next;
}DListNode,*DLinkList;

//插入,在第i个位置插入元素值为e的结点,时间复杂度O(n) 
int InsertDList(DLinkList head,int i,DataType e){
	DListNode *p,*s;
	int j;
	p=head->next;
	j=0;
	while(p!=head&&j<i){
		p=p->next;
		j++;
	}
	if(j!=i){
		printf("插入位置不正确!");
		return 0;
	}
	s=(DListNode*)malloc(sizeof(DListNode));
	if(!s)
		return -1;
	s->data=s;
	s->prior=p->prior;
	p->prior->next=s;
	s->next=p;
	p->prior=s;
	return 1;
} 

//删除第i个结点,时间复杂度O(n) 
int DeleteDList(DLinkList head,int i,DataType *e){
	DListNode *p;
	int j;
	p=head->next;
	j=0;
		while(p!=head&&j<i){
		p=p->next;
		j++;
	}
	if(j!=i){
		printf("删除位置不正确!");
		return 0;
	}
	p->prior->next=p->next;
	p->next->prior=p->prior;
	free(p);
	return 1;
} 
发布了53 篇原创文章 · 获赞 117 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_40431584/article/details/88638122