数据结构-双向链表(C语言)

一、基本操作

1、定义节点

双向链表是链表的一种,与单链表不同的shi,它的每个数据结点中都有两个指针,分别指向直接前驱和直接后继。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。下图为双向链表的结构图。
在这里插入图片描述

typedef struct DoubleLinkNode
{
	char data;
	struct DoubleLinkNode *previous;
	struct DoubleLinkNode *next;
}DLNode,*DLNodeptr;

2、双向链表的初始化

DLNodeptr initLinkList()
{
	DLNodeptr tempHeader=(DLNodeptr)malloc(sizeof(DLNode));
	tempHeader->data='\0';
	tempHeader->previous=NULL;
	tempHeader->next=NULL;
	return tempHeader; 
}

3、双向链表的打印

与单链表基本一致

void printList(DLNodeptr pHeader)
{
	DLNodeptr p=pHeader->next;
	while(p)
	{
		printf("%c",p->data);
		p=p->next;
	}
	printf("\r\n");
}

4、双向链表的插入

同单链表类似
如图所示
在这里插入图片描述

void insertElement(DLNodeptr pHeader,char pchar,int pos)
{
	DLNodeptr p,q,r;
	
	p=pHeader;
	int i;
	for(i=0;i<pos;i++)
	{
		p=p->next;
		if(p==NULL)
		{
			printf("该位置不在链表范围内!");
			return; 
		}
	}
	q=(DLNodeptr)malloc(sizeof(DLNode));
	q->data=pchar;
		
	r=p->next;
	q->next=p->next;
	q->previous=p;
	p->next=q;
	if(r!=NULL)
	{
		r->previous=q;
	} 
}

5、双向链表的删除

在这里插入图片描述

void deleteElement(DLNodeptr pHeader,char Achar)
{
	DLNodeptr p,q,r;
	p=pHeader;
	
	while((p->next) && (p->next->data!=Achar))
	{
		p=p->next;
	}
	
	if(p->next==NULL)
	{
		printf("该数据不存在于链表中!\n"); 
		return;
	}
	q=p->next;
	r=q->next;
	p->next=r;
	if(r)
	{
		r->previous=p;
	}
	free(q);
}

6、双向链表的元素定位

void locateElement(DLNodeptr pHeader,char Achar)
{
	DLNodeptr p,q;
	p=pHeader;
	
	int i=0;
	while((p->next) && (p->next->data!=Achar))
	{
		p=p->next;
		i++;
	}
	if(p->next ==NULL)
	{
		printf("该元素(%c)不存在于链表中!\n",Achar); 
		return;
	}
	printf("The element(%c)’s location is %d.\n",Achar,i);
}

7、链表清空

void destroy_list(DLNodeptr tempList)
{
	DLNodeptr p,q;
	p=tempList;
	if(p)
	{
		q=p;
		p=p->next;
		free(q);
	}
	printf("清空成功!\n");
}

8、测试

 void insert_delete_locate_destroy_list_Text()
{
	DLNodeptr tempList=initLinkList();
	printList(tempList);
	
	insertElement(tempList, 'H', 0);
	insertElement(tempList, 'e', 1);
	insertElement(tempList, 'l', 2);
	insertElement(tempList, 'l', 3);
	insertElement(tempList, 'o', 4);
	insertElement(tempList, '!', 5);
	printList(tempList);

	deleteElement(tempList, 'H');
	deleteElement(tempList, 'a');
	deleteElement(tempList, '!');
	printList(tempList);
	
	insertElement(tempList, 'o', 1);
	printList(tempList);
	
	locateElement(tempList,'p');
	locateElement(tempList,'o');
	
	destroy_list(tempList);
	
}

二、完整代码

#include<stdio.h>
#include<malloc.h>

typedef struct DoubleLinkNode
{
	char data;
	struct DoubleLinkNode *previous;
	struct DoubleLinkNode *next;
}DLNode,*DLNodeptr;

DLNodeptr initLinkList()
{
	DLNodeptr tempHeader=(DLNodeptr)malloc(sizeof(DLNode));
	tempHeader->data='\0';
	tempHeader->previous=NULL;
	tempHeader->next=NULL;
	return tempHeader; 
}

void printList(DLNodeptr pHeader)
{
	DLNodeptr p=pHeader->next;
	while(p)
	{
		printf("%c",p->data);
		p=p->next;
	}
	printf("\r\n");
}

void insertElement(DLNodeptr pHeader,char pchar,int pos)
{
	DLNodeptr p,q,r;
	
	p=pHeader;
	int i;
	for(i=0;i<pos;i++)
	{
		p=p->next;
		if(p==NULL)
		{
			printf("该位置不在链表范围内!");
			return; 
		}
	}
	q=(DLNodeptr)malloc(sizeof(DLNode));
	q->data=pchar;
		
	r=p->next;
	q->next=p->next;
	q->previous=p;
	p->next=q;
	if(r!=NULL)
	{
		r->previous=q;
	} 
}

void deleteElement(DLNodeptr pHeader,char Achar)
{
	DLNodeptr p,q,r;
	p=pHeader;
	
	while((p->next) && (p->next->data!=Achar))
	{
		p=p->next;
	}
	
	if(p->next==NULL)
	{
		printf("该数据不存在于链表中!\n"); 
		return;
	}
	q=p->next;
	r=q->next;
	p->next=r;
	if(r)
	{
		r->previous=p;
	}
	free(q);
} 

void locateElement(DLNodeptr pHeader,char Achar)
{
	DLNodeptr p,q;
	p=pHeader;
	
	int i=0;
	while((p->next) && (p->next->data!=Achar))
	{
		p=p->next;
		i++;
	}
	if(p->next ==NULL)
	{
		printf("该元素(%c)不存在于链表中!\n",Achar); 
		return;
	}
	printf("The element(%c)’s location is %d.\n",Achar,i);
}

void insert_delete_locate_destroy_list_Text()
{
	DLNodeptr tempList=initLinkList();
	printList(tempList);
	
	insertElement(tempList, 'H', 0);
	insertElement(tempList, 'e', 1);
	insertElement(tempList, 'l', 2);
	insertElement(tempList, 'l', 3);
	insertElement(tempList, 'o', 4);
	insertElement(tempList, '!', 5);
	printList(tempList);

	deleteElement(tempList, 'H');
	deleteElement(tempList, 'a');
	deleteElement(tempList, '!');
	printList(tempList);
	
	insertElement(tempList, 'o', 1);
	printList(tempList);
	
	locateElement(tempList,'p');
	locateElement(tempList,'o');
	
	destroy_list(tempList);
	
}

void destroy_list(DLNodeptr tempList)
{
	DLNodeptr p,q;
	p=tempList;
	if(p)
	{
		q=p;
		p=p->next;
		free(q);
	}
	printf("清空成功!\n");
}

void main()
{
	insert_delete_locate_destroy_list_Text();
}

运行结果:

Hello!
该数据不存在于链表中!
ello
eollo
该元素(p)不存在于链表中!
The element(o)’s location is 1.
清空成功!
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值