数据结构-C语言代码 day 3-双向链表

1.双向链表的定义
上一节学习了单链表。今天学习双链表。
单向链表特点:
  1.我们可以轻松的到达下一个节点, 但是回到前一个节点是很难的.
  2.只能从头遍历到尾或者从尾遍历到头(一般从头到尾)
双向链表特点
  1.每次在插入或删除某个节点时, 需要处理四个节点的引用, 而不是两个. 实现起来要困难一些
  2.相对于单向链表, 必然占用内存空间更大一些.
  3.既可以从头遍历到尾, 又可以从尾遍历到头
双向链表的定义:
  双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。 所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。

 双向链表中各节点包含以下 3 部分信息:
  指针域:用于指向当前节点的直接前驱节点;
  数据域:用于存储数据元素。
  指针域:用于指向当前节点的直接后继节点;

指针域

(prior)

数据域

(data)

指针域

(next)

一、老师代码临摹

双链表结构

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

创建表头

DLNodePtr initLinkList(){
    DLNodePtr tempHeader = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
    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;
 
    //找到插入位置,并判断是否超出链表范围
    p = paraHeader;
    for (int 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(struct DoubleLinkedNode));
    q->data = paraChar;
 
    //插入链表
    r = p->next;
    q->next = p->next;
    q->previous = p;
    p->next = q;
    //判断是否在链表尾部插入元素结点
    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 = q;
    }
 
    //释放内存
    free(q);
}

查找元素

int locateElemt(DLNodePtr paraHeader, char paraChar){
    int paraPosition = -1;
    DLNodePtr p;
    p = paraHeader;
    
    //在链表中查找
    while(p != NULL && p->data != paraChar){
        p = p->next;
        paraPosition++;
    }
    
    //判断是否找到
    if(p == NULL){
        paraPosition = -1;
    }
    
    return paraPosition;
}

测试

void insertDeleteTest(){
    //创建链表
    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, 'e');
    deleteElement(tempList, 'a');
    deleteElement(tempList, 'o');
    printList(tempList);
 
    //插入元素
    insertElement(tempList, 'o', 1);
    printList(tempList);
}

运行结果


Hello!
The char 'a' does not exist.
Hll!
Holl!
The first node: 6487504, 6487504, 6487520
The second node: 6487472, 6487472, 6487488

二、部分代码改动及更多测试

1、locateElement()函数

int locateElement(DLNodePtr paraHeader, char paraValue){
	DLNodePtr p=paraHeader;
	
	for(int i=0; ;i++){
		if(p->data==paraValue){
			return i;
		}
		p=p->next;
	}
	return -1;
}

2、insertElement()函数更多测试

void insertDeleteTest(){
	//创建链表
	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, '\t', 5);
	insertElement(tempList, 'S', 6);
	insertElement(tempList, 'w', 7);
	insertElement(tempList, 'p', 8);
	insertElement(tempList, 'u', 9);
	insertElement(tempList, '!', 10);
	printList(tempList);

	// 删除链表
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	deleteElement(tempList, '!');
	printList(tempList);

	// 插入链表
	insertElement(tempList, 'p', 3);
	printList(tempList);
}

运行结果


Hello   Swpu!
The char 'a' does not exist.
Hll     Swpu
Hllp    Swpu
The first node: 6487504, 6487504, 6487520
The second node: 6487472, 6487472, 6487488

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值