数据结构03-双向链表

目录

定义

结构

初始化

打印表

插入新元素

删除元素

根据元素查找

测试函数

返回地址

测试结果

完整代码

定义

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

结构

typedef struct doublelinkedNode{
	char data;
	struct doublelinkedNode *previous;
	struct doublelinkedNode *next;
}DLNode,*DLNodePtr;

初始化

DLNodePtr iniLinkList (){
	DLNodePtr Header = (DLNodePtr)malloc(sizeof(DLNode));
	Header->data = 0;
	Header->next = NULL;
	Header->previous = NULL;
	return Header;
}

打印表

void printList(DLNodePtr Header){
	DLNodePtr p = Header->next;
	while(p != NULL){
		printf("%c",p->data);
		p = p->next;
	}
	printf("\n");
}

插入新元素

void insertElement(DLNodePtr Header,char pchar,int pos){
	DLNodePtr p,q;
	
	p = Header;
	for(int i = 0;i <pos ;i++){
		p = p->next;
		if(p == NULL){
			printf("The position %d is betond the scope of the list",pos);
			return;
		}
	}
	
	q = (DLNodePtr)malloc(sizeof(DLNode));
	q->data = pchar;
	

	q->next = p->next;
	q->previous = p;
	p->next = q;
	
	if(q->next != NULL){
		q->next->previous = q;
	} 
}

删除元素

void deleteElement(DLNodePtr Header,char pchar){
	DLNodePtr p,q;
	p = Header;
	
	while((p->next != NULL) && (p->next->data != pchar)){
		p = p->next;
	}
	
	if(p->next == NULL){
		printf("The char %c does not exist.\n",pchar);
		return;
	}
	
	q = p->next;
	p->next = q->next;
	if(q->next != NULL){
		q->next->previous = p;
	}
	
	free(q);
}

根据元素查找

DLNodePtr locateElement(DLNodePtr Header,char pchar){
	DLNodePtr p;
	p = Header;
	
	while((p->next != NULL) && (p->next->data != pchar)){
		p = p->next;
	}
	
	if(p->next == NULL){
		printf("The char %c does not exist.\n",pchar);
		return NULL;
	}
	
	return p->next;
}

测试函数

void insertdeletelocateTest(){
	DLNodePtr tempList = iniLinkList();
	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, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);

	insertElement(tempList, 'e', 1);
	insertElement(tempList, 'o', 4);
	insertElement(tempList, ' ', 5);
	insertElement(tempList, 'W', 6);
	insertElement(tempList, 'o', 7);
	insertElement(tempList, 'r', 8);
	insertElement(tempList, 'l', 9);
	insertElement(tempList, 'd', 10);
	printList(tempList);
	
	DLNodePtr newl = locateElement(tempList,'l');
	printList(newl);
}

返回地址

void basicAddressTest(){
	DLNode tempNode1, tempNode2;

	tempNode1.data = 4;
	tempNode1.next = NULL;
	tempNode1.previous = NULL;

	tempNode2.data = 6;
	tempNode2.next = NULL;
	tempNode2.previous = NULL;

	printf("The first node: %d, %d, %d, %d\r\n",
		&tempNode1, &tempNode1.data, &tempNode1.next, &tempNode1.previous);
	printf("The second node: %d, %d, %d, %d\r\n",
		&tempNode2, &tempNode2.data, &tempNode2.next, &tempNode2.previous);

}

测试结果


Hello!
The char a does not exist.
Hll!
Hello World!
lo World!
The first node: 6487504, 6487504, 6487520, 6487512
The second node: 6487472, 6487472, 6487488, 6487480

完整代码

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

typedef struct doublelinkedNode{
	char data;
	struct doublelinkedNode *previous;
	struct doublelinkedNode *next;
}DLNode,*DLNodePtr;

DLNodePtr iniLinkList (){
	DLNodePtr Header = (DLNodePtr)malloc(sizeof(DLNode));
	Header->data = 0;
	Header->next = NULL;
	Header->previous = NULL;
	return Header;
}

void printList(DLNodePtr Header){
	DLNodePtr p = Header->next;
	while(p != NULL){
		printf("%c",p->data);
		p = p->next;
	}
	printf("\n");
}

void insertElement(DLNodePtr Header,char pchar,int pos){
	DLNodePtr p,q;
	
	p = Header;
	for(int i = 0;i <pos ;i++){
		p = p->next;
		if(p == NULL){
			printf("The position %d is betond the scope of the list",pos);
			return;
		}
	}
	
	q = (DLNodePtr)malloc(sizeof(DLNode));
	q->data = pchar;
	

	q->next = p->next;
	q->previous = p;
	p->next = q;
	
	if(q->next != NULL){
		q->next->previous = q;
	} 
}

void deleteElement(DLNodePtr Header,char pchar){
	DLNodePtr p,q;
	p = Header;
	
	while((p->next != NULL) && (p->next->data != pchar)){
		p = p->next;
	}
	
	if(p->next == NULL){
		printf("The char %c does not exist.\n",pchar);
		return;
	}
	
	q = p->next;
	p->next = q->next;
	if(q->next != NULL){
		q->next->previous = p;
	}
	
	free(q);
}

DLNodePtr locateElement(DLNodePtr Header,char pchar){
	DLNodePtr p;
	p = Header;
	
	while((p->next != NULL) && (p->next->data != pchar)){
		p = p->next;
	}
	
	if(p->next == NULL){
		printf("The char %c does not exist.\n",pchar);
		return NULL;
	}
	
	return p->next;
}

void insertdeletelocateTest(){
	DLNodePtr tempList = iniLinkList();
	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, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);

	insertElement(tempList, 'e', 1);
	insertElement(tempList, 'o', 4);
	insertElement(tempList, ' ', 5);
	insertElement(tempList, 'W', 6);
	insertElement(tempList, 'o', 7);
	insertElement(tempList, 'r', 8);
	insertElement(tempList, 'l', 9);
	insertElement(tempList, 'd', 10);
	printList(tempList);
	
	DLNodePtr newl = locateElement(tempList,'l');
	printList(newl);
}

void basicAddressTest(){
	DLNode tempNode1, tempNode2;

	tempNode1.data = 4;
	tempNode1.next = NULL;
	tempNode1.previous = NULL;

	tempNode2.data = 6;
	tempNode2.next = NULL;
	tempNode2.previous = NULL;

	printf("The first node: %d, %d, %d, %d\r\n",
		&tempNode1, &tempNode1.data, &tempNode1.next, &tempNode1.previous);
	printf("The second node: %d, %d, %d, %d\r\n",
		&tempNode2, &tempNode2.data, &tempNode2.next, &tempNode2.previous);

}

int main(){
	insertdeletelocateTest();
	basicAddressTest();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值