数据结构—双向链表

一、老师的代码

一、源代码

二、测试结果

二、自己的代码

一、储存结构

二、初始化

三、打印

四、按位查找

五、求前驱

六、按位插入

七、按位删除元素

八、删除固定元素

九、统计元素个数

十、摧毁链表

十一、插入删除测试

十二、地址打印

十三、main函数

十四、测试结果

十五、总结

一、老师的代码

一、源代码

#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 paraHeader)
 {
 	DLNodePtr p = paraHeader->next;
 	while(p!=NULL)
 	{
 		printf("%c ",p->data);
 		p = p->next;
	 }
	 printf("\r\n");
  } 
  
//元素的按位插入 
void insertElement(DLNodePtr paraHeader,char paraChar,int paraPosition)
{
	DLNodePtr p,q,r;
	int i;
	p=paraHeader;
	//先找到位置 
	for(i=0;i<paraPosition;i++)
	{
		p=p->next;
		if(p==NULL)
		{
			printf("The position %d is beyond the scope of the list",paraPosition);
			return ;
		}
	}
	//为插入的元素分配内存 
	q = (DLNodePtr)malloc(sizeof(DLNode));
	q->data = paraChar;
	q->next = NULL;
	q->previous = NULL;
	//开始插入
	r = p->next;
	p->next = q;
	q->previous = p;
	q->next = r;
	if(r!=NULL)
	{
		r->previous = q;
	}
 } 
 
void deleteElement(DLNodePtr paraHeader,char paraChar)
{
	DLNodePtr p,q,r;
	p = paraHeader;
	while((p->next!=NULL)&&(p->next->data!=paraChar))
	{
		p=p->next;
	}
	if(p->next==NULL)
	{
		printf("The char %c does not exist.\r\n",paraChar);
		return ;
	}
	q=p->next;
	r=q->next;
	p->next=r;
	if(r!=NULL)
	{
		r->previous=p;
	}
	free(q);
}

void insertDeleteTest(){
	// Step 1. Initialize an empty list.
	DLNodePtr tempList = initLinkList();
	printList(tempList);

	// Step 2. Add some characters.
	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);

	// Step 3. Delete some characters (the first occurrence).
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);

	// Step 4. Insert to a given position.
	insertElement(tempList, 'o', 1);
	printList(tempList);
}// Of appendInsertDeleteTest

/**
 * Address test: beyond the book.
 */
void basicAddressTest(){
	DLNode tempNode1, tempNode2;

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

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

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

	tempNode1.next = &tempNode2;
}// Of basicAddressTest

/**
 * The entrance.
 */
void main(){
	insertDeleteTest();
	basicAddressTest();
}// Of main

二、测试结果

二、自己的代码 

一、储存结构

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

//双向链表的储存结构
typedef struct doubleListNode
{
	char data;
	struct doubleListNode *previous;
	struct doubleListNode *next;
}DLNode,*DLNodePtr;

二、初始化

//初始化
DLNodePtr initDoubleList()
{
	DLNodePtr headNode = (DLNodePtr)malloc(sizeof(DLNode));
	headNode->data = '\0';
	headNode->previous = NULL;
	headNode->next = NULL;
	return headNode;
} 

三、打印

//打印链表 
void printList(DLNodePtr list)
{
	DLNodePtr p = list->next;
	while(p!=NULL)	
	{
		printf("%c",p->data);
		p=p->next;
	}
	printf("\r\n");
}

四、按位查找

//按位查找
DLNodePtr searchByLocation(DLNodePtr list,int newPosition)
{
	DLNodePtr p;
	p = list;
	int i;
	//找到位置 
	for(i=0;i<newPosition;i++)
	{
		p = p->next;
		if(p==NULL)
		{
			printf("The position %d is beyoud the scope of the list. \n",newPosition);
			return ;
		}
	}
	printf("The element is %c.\n",p->data);
	return ;
} 

五、求前驱

//求前驱 
DLNodePtr getPriorElement(DLNodePtr list,int newPosition)
{
	DLNodePtr p,q,r;
	int i;
	p = list;
	//先找到那个位置上的元素 
	for(i=0;i<newPosition;i++)
	{
		p =  p->next;
		if(p==NULL)
		{
			printf("The position %d is beyoud the scope of the list. \n",newPosition);
			return ;
		}
	}
	printf("The element'prior is %c.\n",p->previous->data);
	return ;
}

六、按位插入

//按位插入
void insertElement(DLNodePtr list,char newChar,int newPosition)
{
	DLNodePtr p,q,r;
	
	//给元素分配位置 
	q = (DLNodePtr)malloc(sizeof(DLNode));
	q->data = newChar;
	q->next = NULL;
	q->previous = NULL;
	//找到要插入的位置 
	p = list;
	int i;
	for(i=0;i<newPosition;i++)
	{
		p = p->next;
		if(p==NULL)
		{
			printf("The position %d is beyond the scope of the list",newPosition);
			return ;
		}
	}
	//插入 
	r = p->next;
	p->next = q;
	q->previous = p;
	q->next = r;
	if(r!=NULL)
	{
		r->previous = q;
	}
}

七、按位删除元素

//按位删除元素
void deleteElementByLocation(DLNodePtr list,int newPosition)
{
	DLNodePtr p,q,r;
	int i;
	p = list;
	//先找到要删除的元素的位置
	for(i=0;i<newPosition;i++)
	{
		p=p->next;
		if(p==NULL)
		{
			printf("The position %d is beyond the list.\r\n",newPosition);
			return ;
		}
	 } 
	 q = p->previous ;
	 r = p->next ;
	 q->next = r;
	 if(r!=NULL)
	 {
	 	r->previous =q;
	 }
	 free(p);
} 

八、删除固定元素

//删除固定元素 
void deleteElement(DLNodePtr list,char newChar)
{
	DLNodePtr p,q,r;
	p = list;
	//找到要删除的元素 
	while((p->next!=NULL)&&(p->next->data!=newChar)) 
	{
		p = p->next;
	}
	if(p->next==NULL)
	{
		printf("The char %c does not exist.\r\n",newChar);
		return ;
	}
	//删除 
	q = p->next;
	r = q->next;
	p->next = r;
	if(r!=NULL)
	{
		r->previous = p;
	}
	free(q);	
}

九、统计元素个数

//统计元素个数
int statisticsNumber(DLNodePtr list)
{
	int cnt=0;
	DLNodePtr p = list;
	while(p->next!=NULL)
	{
		cnt++;
		p = p->next;
	}
	printf("The number of the list is %d.\n",cnt);
	return ;
 }

十、摧毁链表

//摧毁链表
void destroyList(DLNodePtr list)
{
	DLNodePtr p = list;
	while(p!=NULL)
	{
		list= list->next;
		free(p); 
		p = list; 
	}
} 

十一、插入删除测试

//插入删除测试 
void insertDeleteTest(){
	DLNodePtr tempList = initDoubleList();
	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);
	statisticsNumber(tempList);
	searchByLocation(tempList,2);
	getPriorElement(tempList,2);
	deleteElementByLocation(tempList,2);
	statisticsNumber(tempList);
	printList(tempList);

	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);

	insertElement(tempList, 'o', 1);
	printList(tempList);
}

十二、地址打印

//地址打印 
void basicAddressTest(){
	DLNode tempNode1, tempNode2;

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

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

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

	tempNode1.next = &tempNode2;
}

十三、main函数

//main函数 
void main(){
	insertDeleteTest();
	basicAddressTest();
}

十四、测试结果

十五、总结

总结就是比前几天更加熟悉链表的操作了 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenshengnannn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值