Двойной связанный список в проверке Python - PullRequest
0 голосов
/ 03 июля 2019

Я только начал изучать структуры данных и алгоритмы, и это моя первая попытка в связанном списке.Может ли кто-нибудь предоставить какой-либо способ проверить правильность созданного мной связанного списка?Или, если приведенный ниже код верен или нет?

Я попытался написать функцию для вычисления количества узлов, чтобы доказать функциональность этого связанного списка.

Edit1: я хочу знатьявляется ли эта реализация правильной или нет для связанного списка

Edit2: я попытался улучшить интерфейс и удалить свободный узел.Есть ли что-нибудь еще, что я мог бы сделать, чтобы улучшить это больше?

class Node:
    def __init__(self,d):
        self.data=d
        self.next=None
        self.prev=None

class LinkedList:

    def __init__(self,head=None):
        self.head=head
        self.size=0

    def add(self,data):
        new_node= Node(data)
        new_node.next=self.head 
        if self.head is not None:
            self.head.prev=new_node
        self.head=new_node
        self.size=self.size+1


    def count(self):
        return self.size

    def count_nodes(self,head):
    # assuming that head != None
        count = 1
        current = head
        while current.next is not None:
            current = current.next
            count += 1
        return count      
    def find(self,head,data):
    # assuming that head != None
        current = head
        while current.next is not None:
            if current.data==data:
                print("found")
            current = current.next


    def printnodes(self,head):
    # assuming that head != None
        current = head
        while current.next is not None:
            print(current.data)
            current = current.next
    def delete(self,head,data):
        current = head
        while current.next is not None:
            if current.data==data:
                current.next.prev=current.prev
                current.prev.next=current.next
            current = current.next
    def insertion(self,head,data,search):
        new_node=Node(data)
        current = head
        while current.next is not None:
            if current.data==search:
                new_node.next=current
                new_node.prev=current.prev
                current.prev.next=new_node
                current.prev=new_node
            current = current.next




mylist=LinkedList()
mylist.add(1)
mylist.add(2)
mylist.add(3)
mylist.add(4)
mylist.add(5)
mylist.add(6)
mylist.add(7)
mylist.add(8)
mylist.add(9)
mylist.add(10)
mylist.add(11)
mylist.add(12)
mylist.printnodes(mylist.head)
mylist.delete(mylist.head,5)
mylist.printnodes(mylist.head)
mylist.insertion(mylist.head,21,8)
mylist.printnodes(mylist.head) 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...