栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录
  • 一、基本操作
  • 1、定义节点
  • 2、双向链表的初始化
  • 3、双向链表的打印
  • 4、双向链表的插入
  • 5、双向链表的删除
  • 6、双向链表的元素定位
  • 7、链表清空
  • 8、测试
  • 二、完整代码

一、基本操作 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='';
	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("rn");
}
4、双向链表的插入

同单链表类似
如图所示

void insertElement(DLNodeptr pHeader,char pchar,int pos)
{
	DLNodeptr p,q,r;
	
	p=pHeader;
	int i;
	for(i=0;inext;
		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
#include

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

DLNodeptr initLinkList()
{
	DLNodeptr tempHeader=(DLNodeptr)malloc(sizeof(DLNode));
	tempHeader->data='';
	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("rn");
}

void insertElement(DLNodeptr pHeader,char pchar,int pos)
{
	DLNodeptr p,q,r;
	
	p=pHeader;
	int i;
	for(i=0;inext;
		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.
清空成功!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/857616.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号