zoukankan      html  css  js  c++  java
  • 2、链表(python实现)

    # -*- coding:utf-8 -*-
    #@Time : 2020/5/2 0:01
    #@Author: Aluosen
    #@File : Linklist.py
    #节点Node
    class Node:
    def __init__(self,initdata):
    self.data = initdata
    self.next = None
    def getData(self):
    return self.data
    def getNext(self):
    return self.next
    def setData(self,newdata):
    self.data = newdata
    def setNext(self,newnext):
    self.next = newnext

    #无序表
    class UnorderedList:
    def __init__(self):
    self.head = None
    def add(self,item):
    temp = Node(item)
    temp.setNext(self.head)
    self.head = temp
    def size(self):
    current = self.head
    count = 0
    while current != None:
    count = count + 1
    current = current.getNext()
    return count
    def search(self,item):
    current = self.head
    found = False
    while current != None and not found:
    if current.getData() == item:
    found = True
    else:
    current = current.getNext()
    return found
    def remove(self):
    current = self.head
    previous = None
    found = False
    while not found:
    if current.getData() == item:
    found = True
    else:
    previous,current = current,current.getNext()
    if previous == None:
    self.head = current.getNext()
    else:
    previous.getNext(current.getNext())

    #有序表
    class OrderList:
    def __init__(self):
    self.head = None
    def search(self,item):
    current = self.head
    found = False
    stop = False
    while current != None and not found and not stop:
    if current.getData() == item:
    found = True
    else:
    current = current.getNext()
    return found
    def add(self,item):
    current = self.head
    previous = None
    stop = False
    while current != None and not stop:
    if current.getData() > item:
    stop = True
    else:
    previous = current
    current = current.getNext()
    temp = Node(item)
    if previous == None:
    temp.setNext(self.head)
    self.head = temp
    else:
    temp.setNext(current)
    previous.setNext(temp)



  • 相关阅读:
    数据结构 trie 树 入门练手 POJ 3630 Phone List
    回溯法求 1~n 的排列
    POJ 1988 Cube Stacking (并查集,节点到根节点的距离题目)
    Ubuntu下的截图软件
    git使用方法
    在Linux下安装ADT
    在Linux下安装Android SDK
    在Linux下安装Java JDK
    研发项目风险因素差异性
    关于C#的partial修饰符
  • 原文地址:https://www.cnblogs.com/Aluosen/p/12817365.html
Copyright © 2011-2022 走看看