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

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

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<windows.h>
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;
}```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值