单链表的查找和建立

本文详细介绍了单链表的查找方法,包括按位查找和按值查找,以及如何计算链表的长度。在查找过程中,按位查找平均时间复杂度为O(n),按值查找同样为O(n)。对于单链表的建立,对比了尾插法和头插法,尾插法在未使用尾指针时时间复杂度为O(n^2),而头插法适用于链表逆置等操作,但必须先进行初始化。
摘要由CSDN通过智能技术生成

单链表的查找

按位查找

GetElem(L,i):按位查找,获取表L中第i个位置的元素的值

LNode * GetElem(LinkLIst L,int i){
   
    if(i<0)
        return NULL;
    LNode *p;//指针p指向当前扫描到的结点
    int j=0;//当前p指向的是第几个结点
    p=L;//L指向头结点,头结点是第0个结点(不存数据)
    while(p!==NULL&&j<i){
   //循环找到第i个结点
        p=p->next;
        j++;
    }
    return 0;
}
  1. i=0,此时j=0,直接跳过while循环,返回头结点
  2. i大于表的长度,直接跳过while循环,返回NULL
  3. 平均时间复杂度:O(n)
按值查找

LocateElem(L,e):按值查找,在表中查找具有给定关键字值的元素

//按值查找。找到数据域==e的结点
LNode *LocatrElem(LInkList L,ElemType e){
   
	LNode *p=L->next
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个用C++编写的单链表的例子,包括建立查找、插入和删除操作的实现: ```cpp #include <iostream> // 定义单链表节点结构 struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // 建立单链表 ListNode* createLinkedList(std::initializer_list<int> values) { ListNode* head = NULL; ListNode* tail = NULL; for (int val : values) { ListNode* newNode = new ListNode(val); if (head == NULL) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } return head; } // 查找链表中是否存在某个值 bool searchLinkedList(ListNode* head, int target) { ListNode* curr = head; while (curr != NULL) { if (curr->val == target) { return true; } curr = curr->next; } return false; } // 在链表的指定位置插入节点 void insertNode(ListNode*& head, int pos, int val) { ListNode* newNode = new ListNode(val); if (pos == 0) { newNode->next = head; head = newNode; } else { ListNode* curr = head; int count = 0; while (curr != NULL && count < pos - 1) { curr = curr->next; count++; } if (curr != NULL) { newNode->next = curr->next; curr->next = newNode; } } } // 删除链表中指定位置的节点 void deleteNode(ListNode*& head, int pos) { if (pos == 0) { ListNode* temp = head; head = head->next; delete temp; } else { ListNode* curr = head; int count = 0; while (curr != NULL && count < pos - 1) { curr = curr->next; count++; } if (curr != NULL && curr->next != NULL) { ListNode* temp = curr->next; curr->next = curr->next->next; delete temp; } } } // 打印链表 void printLinkedList(ListNode* head) { ListNode* curr = head; while (curr != NULL) { std::cout << curr->val << " "; curr = curr->next; } std::cout << std::endl; } int main() { // 创建链表 1 -> 2 -> 3 -> 4 -> 5 ListNode* head = createLinkedList({1, 2, 3, 4, 5}); printLinkedList(head); // 查找链表中是否存在值为 3 的节点 bool found = searchLinkedList(head, 3); std::cout << "Found 3 in the linked list: " << (found ? "true" : "false") << std::endl; // 在位置 2 插入值为 6 的节点 insertNode(head, 2, 6); printLinkedList(head); // 删除位置 4 的节点 deleteNode(head, 4); printLinkedList(head); return 0; } ``` 这段代码实现了单链表建立查找、插入和删除操作。你可以根据需要进行修改和扩展。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不说话的白帽子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值