首页 > 编程语言 >数据结构与算法学习笔记———链表(Linked List)

数据结构与算法学习笔记———链表(Linked List)

时间:2022-09-05 12:14:20浏览次数:95  
标签:node head cur self List next 链表 Linked

链表( Linked List)

# 该篇笔记转自【Python】python链表_小周ipython的博客-CSDN博客_python 链表

简介

链表(Linked List):是一种线性表数据结构。他是用一组任意的存储单元(可以是连续的,也可以是不连续的),来存储一组具有相同类型的数

链表是实现线性表的链式存储结构的基础。

 

单链表每个数据元素占用若干存储单元的组合称为一个链节点,还要存放一个指出这个数据元素在逻辑关系上的直接后继元素所在链节点的地址,该地址称为后继指针。

 

 

双链表(doubly Linked List):链表的一种,也叫做双链表。 它的每个链节点中有两个指针,分别指向直接后继和直接前驱。

 

 

 

 

循环链表(Circualr Linked List):链表的一种。它的最后一个链节点指向头节点,形成一个环。

 

 

 

 

 

链表的python实现:

 

首先定义一个链节点类,然后再定义链表类。 LinkedList类中只有一个链节点变量head用来表示链表的头节点。

 

 

class ListNode:
    def __init__(self, val=0 ,next=None):
        self.val = val
        self.next = next

class LinkedList:
    def __init__(self):
        self.head = None

    #add an item to the head of the linked list:
    def insertfront(self,val):
        node = ListNode(val) # first create a node
        node.next = self.head # then let the created node point the head of the original linked list
        self.head = node # At last, the created node be the head of the modified linked list

    # add an item to the end o fhte linked list:
    def inserttail(self,val):
        node = ListNode(val)
        cur = self.head
        while cur.next != None:
            cur = cur.next
        cur.next  = node

    def insertINside(self,val,index):
        node = ListNode(val)
        count = 0
        cur =  self.head
        while cur != None and count < index-1:
            cur = cur.next # firstly, connect the tail of node
            count += 1 # Then, connect the head fo the node

        if cur: return 'error'

        node.next = cur.next
        cur.next = node


    # creat a linked List based on the data(data=[item1,item2,item3....])
    def create(self,data):
        self.head = ListNode(0)
        cur = self.head
        for i in range(len(data)):
            node = ListNode(data[i])
            cur.next = node
            cur = cur.next


    # return the lenth of the linked List
    def length(self):
        count = 0
        cur = self.head
        while cur:
            count += 1
            cur = cur.next
        return count

    def removeFront(self):
        if self.head:
            self.head = self.head.next
    
    def removeinside(self,index):
        count = 0
        cur = self.head
        
        while cur.next and count < index - 1
            count += 1
            cur = cur.next
        
        if not cur:
            return 'Error'
        
        del_node = cur.next
        cur.next = del_node.next
        

 

标签:node,head,cur,self,List,next,链表,Linked
From: https://www.cnblogs.com/wujiadeqiao/p/16657626.html

相关文章

  • OpenHarmony中的HDF单链表及其迭代器
    概念为了性能考虑,嵌入式系统一般使用C语言进行开发,由于C语言标准库没有封装链表,所以嵌入式系统一般自己设计和实现链表这种数据结构。单链表是链表中的一种,本文描述Open......
  • 对List进行分页工具类
    对List<?>进行分页工具类packagecom.soft.mpms.zfream.util;importjava.util.List;/***@ClassName对List集合进行份分页*@description:TODO*@author:hhs......
  • Peerlist 如何改变现代专业网络
    Peerlist如何改变现代专业网络什么是同行列表?起初,这听起来可能有点像千禧一代,但Peerlist正在为专业网络带来一场革命。您原本打算发送简历以申请公司职位的日子已经......
  • 算法--链表
       方法一:构造链表如果此类型的题出现在笔试中,如果内存要求不高,可以采用如下方法:可以先用一个vector将单链表的指针都存起来,然后再构造链表。此方法简单易懂,代码好......
  • leetcode 83. Remove Duplicates from Sorted List 删除排序链表中的重复元素(简单)
    一、题目大意给定一个已排序的链表的头head,删除所有重复的元素,使每个元素只出现一次。返回已排序的链表。示例1:输入:head=[1,1,2]输出:[1,2]示例2:输入:h......
  • 设计链表
    设计链表设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val和next。val是当前节点的值,next是指向下一个节点的指针/引用。如果要使......
  • error loading sources list: ('The read operation timed out',)解决办法!!
    一、灵魂四问1.为什么叫rosdepc?rosdepc,c指的是China中国,主要用于和rosdep区分。2.rosdepc和rosdep功能一致吗?rosdep官方最新版源码直接修改的,小鱼只动了名称和源地址......
  • Java中Iterator和ListIterator用法整理
    1 迭代器简介(Iterator)迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构。迭代器通常被称为“轻量级”对象,因为创建......
  • cmake add_library编译链接静态库cmakelists
      本篇文章我们来编写CMakeLists.txt使用cmake的add_library的构建静态库,并使用target_link_libraries链接指定的静态库。cmake的linuxwindows和linux环境的准备可以......
  • [数据结构10分钟入门] 面向初学者从零实现(基于C语言)-- 单链表
    ​一、链表是什么    链表是一种通过指针串联在一起的线性结构,在内存中是分散存储的(数组在内存中连续分布),链表由一系列节点组成,每个节点都由数据域和指针域组成。主......