前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c++的链表-链表入门(C++)

c++的链表-链表入门(C++)

作者头像
囍楽云
发布2022-12-26 14:47:04
7840
发布2022-12-26 14:47:04
举报
文章被收录于专栏:囍楽云博客

从上的链表基础知识学习,进行总结如下:

  1.单链表介绍

  单链表与数组不同,数组中只存储元素的值,而单链表中除了数据的值外还包括了指向下一个节点的引用字段通常以next来表示。如下图表示,通过这个引用,单链表将所有节点按照顺序组织起来。

  通常单链表如下定义:

   // Definition for singly-linked list.

代码语言:javascript
复制
    struct SinglyListNode {
        int val;
        SinglyListNode *next;
        SinglyListNode(int x) : val(x), next(NULL) {}

  与数组区别,我们无法随机访问链表中的元素,但如果我们想要获得第i个元素就需要从头指针开始遍历。平均时间复杂度为O(N)。

  2.链表添加

  链表添加又分为在中间添加、在头部添加以及在尾部添加,首先是头部添加:

  头结点是整个链表的代表因此在头部进行添加节点时最重要的是添加后更新head:

  初始化一个cur;将该结点连接到head上;将cur指定为head.

  中间位置添加:

  首先初始化cur

  将cur->next连接到pred的下一个节点即pred->next

  最后将断掉的pred->next 再连接到cur上。

  这样与数组进行对比我们只需要O(1)的时间复杂度就可以将元素插入进链表。

  3.删除操作

  如果我们想要删除一个节点cur,那我们需要两步:

  找到cur的上一个节点以及下一个节点;

  连接上一个节点pred到cur下一个节点.

  因为cur节点的下一个节点就是cur->nextc++的链表,但是上一个节点需要遍历才可以找到c++的链表,因此删除节点的时间复杂度为O(N)。

  如果要删除第一个节点我们只需要把下一个节点赋予head即可。

  4.设计链表

   class MyLinkedList {

代码语言:javascript
复制
    public:
        struct node{
            int val;
            node *next;
            node(int x): val(x),next(NULL){};
        };
        
        /** Initialize your data structure here. */
        MyLinkedList() {
            head = new node(0);
            size = 0;
        }
        
        /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
        int get(int index) {
            if(index(size-1)) return -1;
            node *cur = head->next;    //辅助指针指向第一个节点
            while(index--) cur = cur->next;
            return cur->val;
        }
        
        /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
        void addAtHead(int val) {
            node *cur = new node(val);
            cur->next = head->next;
            head->next = cur;
            size++;
        }
        
        /** Append a node of value val to the last element of the linked list. */
        void addAtTail(int val) {
            node *cur = head;
            while(cur->next)
            {
                cur = cur->next;
            }
            node *newnode = new node(val);
            newnode->next = cur->next;    //把结尾最后一个节点信息保存下来
            cur->next = newnode;          //将新节点连接在cur之后
            size++;
        }
        
        /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
        void addAtIndex(int index, int val) {
            if(index==0) addAtHead(val);
            else if(index == size) addAtTail(val);
            else if(index > size) return;
            else{
                node *pred = head;     //是在indexth之前插入 之后可以head->next
                node *ans = new node(val);
                //node *cur = new node(0);
                while(index>0)
                {
                    pred = pred->next;
                    index--;
                }
                ans->next = pred->next;
                pred->next = ans;
                size++;
            }
        }
        
        /** Delete the index-th node in the linked list, if the index is valid. */
        void deleteAtIndex(int index) {
            if(index < 0 || index >=size)
            return;
            node *cur = head;
            while(index>0)
            {
                cur = cur->next;
                index--;
            }
            cur->next = cur->next->next;
            size--;
        }
    private:
        int size;
        node *head;
    };
    /**
     * Your MyLinkedList object will be instantiated and called as such:
     * MyLinkedList* obj = new MyLinkedList();
     * int param_1 = obj->get(index);
     * obj->addAtHead(val);
     * obj->addAtTail(val);
     * obj->addAtIndex(index,val);
     * obj->deleteAtIndex(index);

本文共 482 个字数,平均阅读时长 ≈ 2分钟

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档