首页 > 其他分享 >链表数据结构实现

链表数据结构实现

时间:2022-11-01 17:00:46浏览次数:48  
标签:Node count temp 实现 self next 链表 __ 数据结构


class Node(object):
"""docstring for Node"""
def __init__(self, val):
super(Node, self).__init__()
self.val = val
self.next = None



class LinkedList(object):
"""LinkedList"""
def __init__(self):
super( LinkedList, self).__init__()
self.head = Node(0)
self.length = 0


def get(self,index):
if index>=self.length or index<0:
reutrn -1

count = 0
temp_Node = self.head

while count<index:
count+=1
temp_Node = temp_Node.next

retun temp_Node.next.val

def __len__(self):
'''
return the length of LinkedList
'''
return self.length


def AddAtidnex(self,index,val):

if index>self.length or index<0:
reutn -1

count = 0
temp_Node = self.head


while count<index:
count+=1
temp_Node= temp_Node.next

next_node = temp_Node.next
node = Node(val)
temp_Node.next = node
node.next = next_node
self.length+=1


def deleteAtindex(self,idnex):

if index<0 or index>=self.length:
return -1

count=0
temp_Node = self.head

while count<index:
count+=1
temp_Node = temp_Node.next

next_node = temp_Node.next.next

temp_Node.next = next_node

self.length-=1






a


标签:Node,count,temp,实现,self,next,链表,__,数据结构
From: https://blog.51cto.com/u_13859040/5814508

相关文章

  • C#实现html和url的编码与解码
    html编码的作用很容易理解,例如储存超文本编辑器的内容到数据库,一般是需要先超文本的html代码编码后进行存储,需要用到的时候就解码返回给前端。url的编码解码一般是url中存......
  • 用栈实现队列
    classMyQueue{public:stack<int>stIn;stack<int>stOut;MyQueue(){}voidpush(intx){stIn.push(x);}//从......
  • 使用vue-handsontable实现web execl编辑
    npminstall--savevue-handsontable-official<template><divclass="wrap"><HotTable:root="test":settings="hotSettings"></HotTable></div></template><scri......
  • ViewPager2实现Tab标签页面
    是在此篇博文Viewpager迁移至ViewPager2实现Tab标签页面_Code-Porter的博客-CSDN博客的基础上对一些细节进行了补充,请支持原作者。使用的编译软件是AndroidStudio2019......
  • uniapp实现国际化多语言切换
    前言项目有海外用户所以需要配置多语言满足客户需求解决方法在uni-app里有内置i18n多语言的配置,并且uni-app里的组件可是可以支持跟随设置语言进行变换的,i18n的主要功能是可......
  • 三 docker安装rabbitMQ之springboot集成stomp,实现前端主动刷新
    一 场景分析对于一些需要数据同步的场景中,例如后台数据有变化,需要程序主动刷新前端界面的数据显示,这样能提供更好的用户数据交互,能第一时间了解到资源信息的变化,而不是......
  • 常用Api返回数据结构封装
    ///<summary>///返回封装///</summary>///<paramname="statusCode"></param>///<returns></returns>public......
  • CSharp实现关机、重启、注销、休眠类
    关机、重启、注销、休眠类usingSystem;usingSystem.Diagnostics;usingSystem.Runtime.InteropServices;usingSystem.Text;namespacePowerManager{///<s......
  • JS/TS数据结构---栈(单调栈)和队列
    一、栈栈(stack)是一种操作受限的线性表数据结构,基于后进先出(LIFO)策略的集合类型,例如函数中的临时变量符合后进先出的特性,因此用栈保存最合适。  在入栈和出栈过程中所需......
  • LMS算法MatLab实现
      LMS自适应滤波器是使滤波器的输出信号与期望响应之间的误差的均方值为最小,因此称为最小均方(LMS)自适应滤波器。其原理及推导见http://download.csdn.net/source/432206......