对双链表的 *** 作(c语言实现)

对双链表的 *** 作(c语言实现),第1张

跟单链表相比差距很大的在于头插和尾插法

#include
#include
#include
#include
typedef struct LNode
{
	int data;
	struct LNode *next;	
	struct LNode *prior;
}LNode;	
void CreatDoubleLinkList_head(LNode *&L,int a[],int n)///头插
{
	LNode *s;
	L=(LNode*)malloc(sizeof(LNode));
	L->next=NULL;
	L->prior=NULL;
	for(int i=0;i<n;i++){
		s=(LNode*)malloc(sizeof(LNode));
		s->data=a[i];
		s->next=L->next;
		if(L->next!=NULL){
			L->next->prior=s;
		}
		s->prior=L;
		L->next=s;
	}
}
void CreatDoubleLinkList_end(LNode *&L,int a[],int n)///尾插
{
	L=(LNode*)malloc(sizeof(LNode));
	LNode *s,*r=L;
	L->next=NULL;
	L->prior=NULL;
	for(int i=0;i<n;i++){
		s=(LNode*)malloc(sizeof(LNode));
		s->data=a[i];
		r->next=s;
		s->prior=r;
		r=r->next;
	}
	r->next=NULL;
}
void PrintDounleLinkList(LNode *L)///输出链表
{
	L=L->next;
	while(L!=NULL){
		printf("%d ",L->data);
		L=L->next;
	}
	printf("\n");
}
void DeleteDoubleLinkList(LNode *L,int n)///删除节点
{
	while (L->data!=n)
	{
		L=L->next;
	}
	L=L->prior;
	LNode *s=L->next;
	L->next=L->next->next;
	L->next->prior=L;
	free(s);
}

int main(){
	LNode *L;
	int n,a[100];
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	scanf("%d",&a[i]);
	//CreatDoubleLinkList_head(L,a,n);
	CreatDoubleLinkList_end(L,a,n);
	PrintDounleLinkList(L);
	DeleteDoubleLinkList(L,3);
	PrintDounleLinkList(L);
	system("pause");
	return 0;
}```

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/674904.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)

保存