【初学数据结构】双链表

 文章目录


提示:以下是本篇文章正文内容,下面案例可供参考

一、双链表概念

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。。

二、双链表接口实现

1.自定义单链表:struct LNode

代码如下(示例):

typedef int ElemType;
typedef struct DNode{
    ElemType data;
    struct DNode*next;
    struct DNode*prior;
}DLinkNode;

2. 初始化单链表:InitList

代码如下(示例):

void InitList(DLinkNode *&L)
{
    L=(DLinkNode *)malloc(sizeof(DLinkNode));
    L->next=NULL;
}

3.头插法创建单链表: CreateListF

void CreatListF(DLinkNode *&L,ElemType a[],int n)
{
    DLinkNode *s;
    L=(DLinkNode *)malloc(sizeof(DLinkNode)); 
    L->next=NULL;
    for(int i=0;i<n;i++)
    {
        s=(DLinkNode *)malloc(sizeof(DLinkNode)); 
        s->data=a[i];
        s->next=L->next;
        if(L->next!=NULL)
        L->next->prior=s;
        L->next=s;
        s->prior=L;
    }
}

 

 4.尾插法创建单链表: CreateListR

 void CreatListR(DLinkNode *&L,ElemType a[],int n)
{
    DLinkNode *s,*r;
    L=(DLinkNode *)malloc(sizeof(DLinkNode)); 
    L->next=NULL;
    r=L;
    for(int i=0;i<n;i++)
    {
        s=(DLinkNode *)malloc(sizeof(DLinkNode)); 
        s->data=a[i];
        r->next=s;
        s->prior=r;
        r=s;
    }
    r->next=NULL;
}

 5.判断单链表是否为空: ListEmpty

 bool ListEmpty(DLinkNode *L)
{
    return (L->next==NULL);
}

 6.求单链表长度:ListLength

int ListLenght(DLinkNode *L)
{
    int n=0;
    DLinkNode *p=L;
    while(p->next!=NULL)
    {
        n++;
        p=p->next;
    }
    return n;
}

 

7.插入数据:ListInsert 

bool ListInsert(DLinkNode *&L,int i,ElemType e)
{
    int j=0;
    DLinkNode *p=L,*s;
    while(j<i-1&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
    return false;
    else
    {
        s=(DLinkNode *)malloc(sizeof(DLinkNode));
        s->data=e;
        s->next=p->next;
        if(p->next!=NULL)
        p->next->prior=s;
        s->prior=p;
        p->next=s;
        return true;
    }
}
 

8.输出单链表:DispList

void DispList(DLinkNode *L)
{
    DLinkNode *p;
    p=L->next;
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}

 9.删除数据元素:DeleteList

 

bool DeleteList(DLinkNode *&L,int i,ElemType &e)
{
    DLinkNode *p=L,*q;
    int j=0;
    if(i<=0)
    return false;
    while(j<i-1&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
    return false;
    else
    {
        q=p->next;
        if(q==NULL)
        return false;
        e=q->data;
        p->next=q->next;
        if(q->next!=NULL)
        q->next->prior=p;
        free(q);
        return true;
    }
}

10.取指定位置数据元素:GetElem 

bool GetElem(DLinkNode *L,int i,ElemType &e)
{
    int j=0;
    DLinkNode *p=L;
    if(i<=0)
    return false;
    while(j<i&&p!=NULL)
    {
        j++;
        p=p->next;
    }
    if(p==NULL)
    return false;
    else
    {
        e=p->data;
        return true;
    }
}

11.销毁单链表:DestroyList 

 void DestroyList(DLinkNode *&L)
{
    DLinkNode *pre=L,*p=L->next;
    while(p!=NULL)
    {
        free(pre);
        pre=p;
        p=pre->next;
    }
    free(pre);
}

12.双链表完整代码 

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct DNode{
	ElemType data;
	struct DNode*next;
	struct DNode*prior;
}DLinkNode;

void CreatListF(DLinkNode *&L,ElemType a[],int n)
{
	DLinkNode *s;
	L=(DLinkNode *)malloc(sizeof(DLinkNode)); 
	L->next=NULL;
	for(int i=0;i<n;i++)
	{
		s=(DLinkNode *)malloc(sizeof(DLinkNode)); 
		s->data=a[i];
		s->next=L->next;
		if(L->next!=NULL)
		L->next->prior=s;
		L->next=s;
		s->prior=L;
	}
}

void CreatListR(DLinkNode *&L,ElemType a[],int n)
{
	DLinkNode *s,*r;
	L=(DLinkNode *)malloc(sizeof(DLinkNode)); 
	L->next=NULL;
	r=L;
	for(int i=0;i<n;i++)
	{
		s=(DLinkNode *)malloc(sizeof(DLinkNode)); 
		s->data=a[i];
		r->next=s;
		s->prior=r;
		r=s;
	}
	r->next=NULL;
}

void DispList(DLinkNode *L)
{
	DLinkNode *p;
	p=L->next;
	while(p!=NULL)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
}

bool ListEmpty(DLinkNode *L)
{
	return (L->next==NULL);
}
int ListLenght(DLinkNode *L)
{
	int n=0;
	DLinkNode *p=L;
	while(p->next!=NULL)
	{
		n++;
		p=p->next;
	}
	return n;
}
void InitList(DLinkNode *&L)
{
	L=(DLinkNode *)malloc(sizeof(DLinkNode));
	L->next=NULL;
}
bool ListInsert(DLinkNode *&L,int i,ElemType e)
{
	int j=0;
	DLinkNode *p=L,*s;
	while(j<i-1&&p!=NULL)
	{
		j++;
		p=p->next;
	}
	if(p==NULL)
	return false;
	else
	{
		s=(DLinkNode *)malloc(sizeof(DLinkNode));
		s->data=e;
		s->next=p->next;
		if(p->next!=NULL)
		p->next->prior=s;
		s->prior=p;
		p->next=s;
		return true;
	}
}

bool DeleteList(DLinkNode *&L,int i,ElemType &e)
{
	DLinkNode *p=L,*q;
	int j=0;
	if(i<=0)
	return false;
	while(j<i-1&&p!=NULL)
	{
		j++;
		p=p->next;
	}
	if(p==NULL)
	return false;
	else
	{
		q=p->next;
		if(q==NULL)
		return false;
		e=q->data;
		p->next=q->next;
		if(q->next!=NULL)
		q->next->prior=p;
		free(q);
		return true;
	}
}

bool GetElem(DLinkNode *L,int i,ElemType &e)
{
	int j=0;
	DLinkNode *p=L;
	if(i<=0)
	return false;
	while(j<i&&p!=NULL)
	{
		j++;
		p=p->next;
	}
	if(p==NULL)
	return false;
	else
	{
		e=p->data;
		return true;
	}
}


void DestroyList(DLinkNode *&L)
{
	DLinkNode *pre=L,*p=L->next;
	while(p!=NULL)
	{
		free(pre);
		pre=p;
		p=pre->next;
	}
	free(pre);
}
int main()
{
	DLinkNode *L,*L1,*L2;
	ElemType e;
	int a[]={1,4,7,2,3} ;
	CreatListF(L1,a,5);
	DispList(L1);
	CreatListR(L2,a,5);
	DispList(L2);
	
	printf("***************\n");
	InitList(L);
	printf("%d\n",ListEmpty(L));
	printf("%d\n",ListLenght(L));
	printf("***************\n");
	ListInsert(L,1,4);
	ListInsert(L,2,5);
	ListInsert(L,1,7);
	DispList(L);
	
	printf("***************\n");
	DeleteList(L,2,e);
	DispList(L);
	printf("%d\n",e);
	
	printf("***************\n");
	GetElem(L1,2,e);
	printf("%d\n",e);
	
	printf("***************\n");
	DestroyList(L);
	DestroyList(L1);
	DestroyList(L2);
	return 0;
} 

 


总结

以上就是数据结构双链表的内容,本文仅仅简单介绍了单链表接口的实现,如果以上有问题,请大神指教,和同学们一起学习进步。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值