羅德興老師的教學歷程檔案 - 111-2 DS & Algorithm - 串列 List
 

企業資訊與管理系
助理教授/日導
羅德興


歷程檔案 Portfolio

    串列 List

    List (CH05)

    一、Single Linked List [download]
    # This is list0.py by 0000000XXX (學號) YYYYYY(姓名) on 2020/05/15
    # This is 展示 linked list (連結串列)的實作
    # 程式的前兩行務必註明:學號、姓名、題號、及用途

    class ListNode:
    def __init__(self, data):
    # initialize this object

    # store data
    self.data = data

    # store the reference (next item)
    self.next = None
    return
     
    def has_value(self, value):
    # method to compare the value with the node data

    if self.data == value:
    return True
    else:
    return False



    class SingleLinkedList:
    def __init__(self):
    # initialize this object

    self.head = None
    self.tail = None
    return

    def add_list_item(self, item):
    # make sure item is a proper node

    if not isinstance(item, ListNode):
    item = ListNode(item)
     
    if self.head is None:
    self.head = item
    else:
    self.tail.next = item
     
    self.tail = item
    return
     
    def list_length(self):
    #returns the number of list items

    count = 0
    current_node = self.head

    while current_node is not None:
    count = count + 1

    # jump to the linked node
    current_node = current_node.next
    return count

    def output_list(self):
    # outputs the list (the value of the node, actually)
    current_node = self.head
    results = []
    while current_node is not None:
    results.append(current_node.data)
    # jump to the linked node
    current_node = current_node.next
    print(results)
    return

    def remove_value(self, value):
    #remove the first item in the list with this value

    previous_node = None
    current_node = self.head

    while current_node is not None:
    if current_node.data == value:
    previous_node.next = current_node.next
    return

    previous_node = current_node
    current_node = current_node.next
    return

    import datetime # 引入時間日期模組
    today = datetime.date.today()
    print ("Hello, 現在是", today)
    print ("我是 學號 0000000XXX YYYYYY(姓名) 在學習 data structure- linked list 實作 \n\n")

    node1 = ListNode(99)
    #print(type(node1)) # <class '__main__.ListNode'>

    list1 = SingleLinkedList()

    print("\n 目前的 list 000:")
    list1.output_list()

    list1.add_list_item(node1)
    print("\n 加入 node,目前的 list:")
    list1.output_list()

    list1.add_list_item('ABCD')
    print("\n 加入 node,目前的 list:")
    list1.output_list()

    list1.add_list_item(15)
    print("\n 加入 node,目前的 list:")
    list1.output_list()

    list1.add_list_item(17)
    print("\n 加入 node,目前的 list:")
    list1.output_list()

    list1.remove_value(15)
    print("\n 移除 node 值為 15 的,目前的 list:")
    list1.output_list()

    list1.remove_value('20')
    print("\n 目前的 list:")
    list1.output_list()

    '''
    作業1:就你的理解,在每行程式 加上 註解 #
    作業2:在執行部分,改變並加入別的數值與文字
    作業3:定義一個函式 移除 list 的特定節點 def remove_list_item_by_id(self, id):
    作業4:定義一個函式 搜尋 list 的內容 def unordered_search(self, value):
    作業5:定義一個函式 反轉 list 的內容 def reverse(self):
    '''

    # 作業5:定義一個函式 反轉 list 的內容 def reverse(self):
    # 可參考
    def reverse(self):
    # reverse the order of the list

    previous_node = None
    current_node = self.head
    next_node = current_node.next

    while next_node is not None:
    current_node.next = previous_node
    previous_node = current_node
    current_node = next_node
    next_node = next_node.next

    current_node.next = previous_node
    self.head = current_node
    return
     
    全部共 0則留言
    登入帳號密碼代表遵守學術網路規範


    文章分類 Labels


    最新文章 Top10

    中華科技大學數位化學習歷程 - 意見反應