阅读笔记-数据结构(C语言)-严蔚敏老师-单链表

这里创建单链表都是指定长度进行创建的,之后会补上动态创建。

一、头插法创建单链表(p30)

  1. 由于严蔚敏老师书上的代码是C/C++混着来的,按照书上的代码敲出来的代码,用C语言编译器编译会报错。

  2. 比如CreateList_L(LinList &L, int n)这函数,C语言中是没有引用调用(即 &L) 这种用法的。

  3. 所以下面给出纯C语言的版本,只需要将 &L 换成指针即可。

  4. 在这里提出一个小建议,很多博客写的头插法(包括严蔚敏老师书上的),都用到了typedef struct LNode {} *LinkLIst,对于新手来说,这种写法不太友好,感觉不太能直观的感受到指针的用法。所以,下面的我直接把它写成了二级指针。

  5. 之后会补上为什么要这样转化。(即LnikLIst &L -> LNode** L)==。

  6. T(n) = O(n)
    在这里插入图片描述

  7. 运行结果
    在这里插入图片描述


#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;

typedef struct LNode
{
    ElemType data;
    struct LNode* next;
} LNode;


/* 
    # 头插法创建单链表
        - 头插法是将每个节点插入到头节点后面,即如输入1,2,3,4,5
            最后的链表应该是 head->5->4->3->2->1
        - L是单链表的首地址(头节点)
        - n是单链表的节点数目
*/
void List_HeadInsert(LNode** L, int n) {
    (*L) = (LNode*)malloc(sizeof(LNode));
    (*L)->next = NULL; // 头节点的创建
    (*L)->data = n;
    // printf("init: %x\n", &L);
    
    for(int i = 0; i < n; i ++) {
        LNode* p = (LNode*)malloc(sizeof(LNode));
        scanf("%d", &p->data);
        p->next = (*L)->next;
        (*L)->next = p;
    }
}

/*
    # 打印单链表
*/
void print(LNode** L) {
    // L = L - 40;
    // printf("%x\n", &L);
    LNode* p = (*L)->next;
    if( !p) {
        printf("L is NULL.\n");
    }
    int len = (*L)->data;
    printf("Contents of the list are:\n");
    for (int i = 0; i < len; i ++) {
        printf("%d%s", p->data, i == len - 1 ? "\n" : "->");
        p = p->next;
    }
}

int main() {
    LNode* L;
    List_HeadInsert(&L, 5);
    // printf("%x\n", &L);
    print(&L);
    return 0;
}


二、尾插法创建单链表

  1. 头插法创建的单链表和输入的节点逆序,尾插法则是顺序。
  2. 代码和运行结果。
  3. T(n) = O(n)
    在这里插入图片描述
void List_HeadInsert(LNode** L, int n) {
    (*L) = (LNode*)malloc(sizeof(LNode));
    (*L)->next = NULL; // 头节点的创建
    (*L)->data = n;
    // printf("init: %x\n", &L);
    
    for(int i = 0; i < n; i ++) {
        LNode* p = (LNode*)malloc(sizeof(LNode));
        scanf("%d", &p->data);
        p->next = (*L)->next;
        (*L)->next = p;
    }
}

三、按索引查找和按值查找

  1. 运行结果和代码。
  2. T(n) = O(n)
    在这里插入图片描述
/* 按序号查找节点值 */
LNode* GetElem(LNode** L, int dst) {
    if(dst < 0) return NULL;
    if(dst == 0) return *L;
    LNode* p = (*L)->next;
    for (int i = 0 ; i < dst; i ++) {
        if(p) p = p->next;
    }
    return p;
}

/* 按元素值查找结点 */
LNode* LocateElem(LNode** L, int e) {
    LNode* p = (*L)->next;
    while( p->data != e && p) {
        p = p->next;
    }
    return p;
}

四、插入到第几个节点之后

  1. 运行结果和代码。
  2. T(n) = O(n)
    在这里插入图片描述
/* 插入到第几个节点之后 */
void List_Insert(LNode** L, int dst) {
    if(dst < 0) return;

    LNode* s = (LNode*)malloc(sizeof(LNode));
    printf("var of current insert:\n");
    scanf("%d", &s->data);

    if(dst == 0) {
        s->next = (*L)->next;
        (*L)->next = s;
        (*L)->data += 1;
        return;
    }
    LNode* p = GetElem(L, dst-1);
    s->next = p->next;
    p->next = s;
    (*L)->data += 1;  // 表长加1
}

五、删除节点

  1. 运行结果和代码。
  2. T(n) = O(n)
    在这里插入图片描述
/* 删除第i个节点 */
ElemType List_Delete(LNode** L, int i) {
    if(i < 0) return -1;
    ElemType res;
    LNode *p, *q;
    if(i == 0) {
        q = (*L)->next;
        res = q->data;
        (*L)->next = (*L)->next->next;
        free(q);
        (*L)->data --;
        return res;
    }
    p = GetElem(L, i - 1);
    q = p->next;
    res = q->data;
    p->next = p->next->next;
    free(q);
    (*L)->data --;
    return res;
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向链表 (Doubly Linked List) 是一种常见的数据结构,它的每个结点都有两个指针,分别指向前一个结点和后一个结点。这种结构使得双向链表可以在 O(1) 时间内进行插入、删除和查找操作。 按位置查找双向链表的代码实现如下: ```c++ #include <iostream> using namespace std; struct Node { int val; Node* prev; Node* next; }; class DoublyLinkedList { private: Node* head; Node* tail; public: DoublyLinkedList() { head = tail = nullptr; } void insert(int pos, int val) { Node* newNode = new Node(); newNode->val = val; if (head == nullptr) { head = tail = newNode; return; } if (pos <= 0) { newNode->next = head; head->prev = newNode; head = newNode; return; } Node* cur = head; int i = 0; while (cur != nullptr && i < pos) { cur = cur->next; i++; } if (cur == nullptr) { newNode->prev = tail; tail->next = newNode; tail = newNode; } else { newNode->prev = cur->prev; newNode->next = cur; cur->prev->next = newNode; cur->prev = newNode; } } Node* find(int pos) { if (pos < 0) return nullptr; Node* cur = head; int i = 0; while (cur != nullptr && i < pos) { cur = cur->next; i++; } return cur; } }; int main() { DoublyLinkedList dll; dll.insert(0, 1); dll.insert(1, 2); dll.insert(2, 3); Node* node = dll.find(1); if (node != nullptr) { cout << node->val << endl; } else { cout << "Not found" << endl; } return 0; } ``` 上面的代码中,我们定义了一个 `DoublyLinkedList` 类,其中包含了插入和查找操作。在插入操作中,我们首先判断链表是否为空,如果为空,则直接将新结点作为链表头部;否则,我们遍历链表找到插入位置,然后将新结点插入到该位置。 在查找操作中,我们同样遍历链表找到指定位置的结点,并返回该结点的指针。注意,在双向链表中,我们可以从头部或尾部开始遍历,这里我们选择从头部开始遍历。 在主函数中,我们创建了一个双向链表,插入了三个结点,并查找了第二个结点,输出结果为 `2`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值