复合型数据结构:(双向循环)链表_第1部分

"双向循环链表"是一个很方便的复合型数据结构,这种结构既能向前找结点,又能向后找结点!利用这个特性,就能够很便捷地实现 “栈” 和 “队列”

  • (创建链表时,采用头插法) 从向头,遍历链表 —— 队列
  • (创建链表时,采用头插法) 从往尾,遍历链表 ——
  • (创建链表时,采用尾插法) 从向头,遍历链表 ——
  • (创建链表时,采用尾插法) 从往尾,遍历链表 —— 队列
    注意:不能混着用。如果有时用头插,有时用尾插,那就没法很便捷地实现 “队列” 和 “栈” 了!

一举多得:双向循环链表会写了,单向链表、单向循环链表、双向链表也就会写啦!!!嘿嘿嘿

这种 “循环数据结构” 可以很方便地进行动态调整,如果要使用数组实现队列(不循环的),那么实现过程中,需要把数据进行不断地移位,以对齐到起始(或者最后)的位置!这就比较麻烦了!

“循环”的意义在于:“”可以是这个循环中的任意一个位置!从而避免了 “对齐” 这种大规模移动数据操作!

1 原子数据抽象

typedef struct _tag_Node{
   
    int num;
    struct _tag_Node *pNext;
    struct _tag_Node *pPrev;
}node;

2 创建链表

2.1 带头结点的

2.1.1 创建头结点

node *createListWithHead(void){
   
    // Pointer variable: linking the pointer to an entity
    node *phead = (node *)malloc(sizeof(node));
    if(phead == NULL){
    // Defensive programming
        return NULL;
    }
    // Initializing the variable; Differentiation.
    phead->pNext = phead;
    phead->pPrev = phead;
    return phead;
}

2.1.2 使用数组创建带头结点链表

node *createListWithHead_UsingArray(int *arr, int len){
   
    node* list = createListWithHead();
    if(list == NULL){
    // Defensive programming
        return NULL;
    }
    node *p = list; // Defining an auxiliary pointer.
    for(int i = 0; i < len; i++){
   
        node *newNode = (node *)malloc(sizeof(node));
        if(newNode == NULL){
    // Defensive programming
            return list;
        }
        newNode->num = arr[i]; // Loading data to the new node.
        // Inserting a new node at the tail.
        newNode->pPrev = p;
        newNode->pNext = list;
        p->pNext = newNode;
        list->pPrev = newNode;
        // Moving the auxiliary pointer.
        p = p->pNext;
    }
    return list;
}

2.2 不带头结点的

2.2.1 使用单个数据创建不带头结点的链表

node *createList(int value){
   
    node *phead = (node *)malloc(sizeof(node));
    if(phead == NULL){
   
        return NULL<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值