第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 OOP 實現(xiàn)鏈表?

如何使用 OOP 實現(xiàn)鏈表?

九州編程 2021-09-24 15:45:34
我正在嘗試使用 OOP 和私有變量來實現(xiàn)一個鏈表。但是,'str' object has no attribute 'get_data'當(dāng)我調(diào)用類的display方法時,我得到了LinkedList。另外,我覺得add方法也不對。當(dāng)我打印self.__head和self.__tail輸入時add(),代碼永遠不會進入 else 部分,它輸出:Sugar SugarMilk MilkTea TeaBiscuit Biscuit下面是我的代碼:class LinkedList:    def __init__(self):        self.__head=None        self.__tail=None    def get_head(self):        return self.__head    def get_tail(self):        return self.__tail    def add(self,data): # Skeptical about it        if self.__tail is None:            self.__head=Node(data).get_data()            self.__tail = self.__head            print(self.__head,self.__tail)        else:            b=Node(data)            self.__tail= b.get_data()            self.__head = self.__tail            b.set_next(self.__tail)            self.__tail = b.get_next()            print(self.__head,self.__tail)    def display(self): # Gives the error        temp = self.__head        msg = []        c = Node(temp)        while (temp is not None):            print(temp.get_data())            msg.append(str(temp.get_data()))            temp = temp.get_next()        msg = ''.join(msg)        print(msg)class Node:    def __init__(self,data):        self.__data=data        self.__next=None    def get_data(self):        return self.__data    def set_data(self,data):        self.__data=data    def get_next(self):        return self.__next    def set_next(self,next_node):        self.__next=next_nodelist1=LinkedList()list1.add("Sugar")#print(list1.get_head())#print("Element added successfully")list1.add("Milk")list1.add("Tea")list1.add("Biscuits")list1.display()
查看完整描述

6 回答

?
守著一只汪

TA貢獻1872條經(jīng)驗 獲得超4個贊

這似乎很可疑:

self.__head = Node(data).get_data()

考慮到您甚至不再引用節(jié)點...然后嘗試調(diào)用 Node 對象的方法。即便如此,您的實現(xiàn)仍然是錯誤的。

我確定還有其他問題,但你可以谷歌這個或?qū)嶋H做你自己的項目/家庭作業(yè)。


查看完整回答
反對 回復(fù) 2021-09-24
?
瀟湘沐

TA貢獻1816條經(jīng)驗 獲得超6個贊

所以,我已經(jīng)為我的問題找到了答案。謝謝大家的幫助。這個我還不是很清楚:


我知道與 Java 等語言相比,python 默認不帶有私有變量,但我相信 python 是關(guān)于尊重約定的,而“__”是告訴另一個開發(fā)人員這個特定實體是私有的約定。


但是,在我的情況下,我將無法直接訪問 Node 類表單 LinkedList 類的數(shù)據(jù)和下一個屬性,反之亦然,因為在使用私有變量時,名稱會解析為_Classnmae__attribute_name。所以更好的解決方案是使用 getter 和 setter,因為它們是通用的。


def add(self,data):

    #Remove pass and write the logic to add an element

    new_node = Node(data)

    if self.__head is None:

        self.__head = self.__tail = new_node

    else:

        self.__tail.set_next(new_node)

        self.__tail = new_node


def display(self):

    #Remove pass and write the logic to display the elements

    temp = self.__head       

    msg = []

    c = Node(temp)

    while (temp is not None):

        msg.append(str(temp.get_data()))

        temp = temp.get_next()

    msg = ' '.join(msg)

    print(msg)

算法:


添加(數(shù)據(jù))


用數(shù)據(jù)創(chuàng)建一個新節(jié)點


如果鏈表為空(頭節(jié)點不引用任何其他節(jié)點),則使頭節(jié)點和尾節(jié)點引用新節(jié)點


除此以外,


一種。使尾節(jié)點的鏈接指向新節(jié)點


灣 將新節(jié)點稱為尾節(jié)點


查看完整回答
反對 回復(fù) 2021-09-24
?
長風(fēng)秋雁

TA貢獻1757條經(jīng)驗 獲得超7個贊

這是一種通過使用我知道的幾個單鏈表實現(xiàn)“技巧”來更簡潔地做到這一點的方法。

鏈表總是由至少一個自動創(chuàng)建并存儲在實例屬性中的哨兵節(jié)點組成self._tail。擁有它有幾個好處。

  1. 知道在哪里tail可以快速輕松地添加一些東西。

  2. 該列表永遠不會為空,因此無需檢查特殊情況。這樣做的一個很好的副作用意味著迭代列表的元素只需要跟隨self._next直到它是哨兵節(jié)點——一個單一的條件表達式。

另一個“技巧”是在當(dāng)前哨兵節(jié)點之前添加一個新的最后一個元素——這在單鏈表中聽起來很慢,因為它似乎需要修改Node要添加的元素。為了達到這樣做的效果,但避免實際這樣做,它所做的是將現(xiàn)有的哨兵節(jié)點轉(zhuǎn)換為新的哨兵節(jié)點Node,并將其_next屬性設(shè)為它創(chuàng)建的新哨兵節(jié)點,以替換之前被重用的哨兵節(jié)點。

這如何有助于理解以下代碼中發(fā)生的事情:

class LinkedList:

    def __init__(self):

        self._tail = Node()

        self._head = self._tail


    def add(self, data):

        """ Add an item to the end of the linked list. """

        new_tail = Node()

        self._tail.set_data(data)  # Convert existing tail into a data node.

        self._tail.set_next(new_tail)

        self._tail = new_tail


        print('adding:', data)


    def display(self):

        """ Traverse linked list and print data associated with each Node. """

        print('\nLinked list contents:')

        curr = self._head

        while curr is not self._tail:

            print('  ' + curr.get_data())

            curr = curr.get_next()



class Node:

    def __init__(self, data=None):

        self._data = data

        self._next = None


    def get_data(self):

        return self._data


    def set_data(self, data):

        self._data = data


    def get_next(self):

        return self._next


    def set_next(self, next_node):

        self._next = next_node



if __name__ == '__main__':

    list1 = LinkedList()


    list1.add("Sugar")

    list1.add("Milk")

    list1.add("Tea")

    list1.add("Biscuits")


    list1.display()

輸出:


adding: Sugar

adding: Milk

adding: Tea

adding: Biscuits


Linked list contents:

  Sugar

  Milk

  Tea

  Biscuits


查看完整回答
反對 回復(fù) 2021-09-24
  • 6 回答
  • 0 關(guān)注
  • 418 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號