当先锋百科网

首页 1 2 3 4 5 6 7

# -*- coding: utf-8 -*-

# @Time : 2020/10/25 10:27 PM

class Node:

def __init__(self, input_data):

self.data = input_data

self.next = None

self.pre = None

class my_list:

def __init__(self, node=None):

self.head = node

def is_empty(self):

return self.head ==None

def length(self):

index = 0

now = self.head

while now != None:

now = now.next

index += 1

return index

def add(self, data):

node = Node(data)

if self.is_empty():

self.head = node

else:

node.next = self.head

self.head.pre = node

self.head = node

def append(self, data):

node = Node(data)

if self.is_empty():

self.head = node

else:

now = self.head

while now.next != None:

now = now.next

now.next = node

node.pre = now

def pop(self):

if self.is_empty():

return None

else:

now = self.head

while now.next != None:

now = now.next

now.pre.next = None

def index(self, index, data):

if index <= 0:

self.add(data)

elif index >= self.length() - 1:

self.append(data)

else:

node = Node(data)

cur = 0

now = self.head

while now != None:

if index - 1 == cur:

break

now = now.next

node.next = now.next

now.next = node

node.pre = now

def remove(self, data):

now = self.head

while now != None:

if now.data == data:

if now == self.head:

self.head = now.next

else:

now.pre.next = now.next

break

else:

now = now.next

if __name__ == '__main__':

l = my_list()

l.add(0)

l.append(1)

l.append(2)

l.append(3)

l.pop()

l.remove(6)

print(l.length())

print(l.is_empty())