羅德興老師的教學歷程檔案 - 100-1 計算機概論 - List
 

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


歷程檔案 Portfolio

    List

    # This is linked list .py by 0000000XXX (學號) YYYYYY(姓名) on 2020/05/15
    # This is 展示 Single 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 unordered_search(self, value):
    # search the linked list for the node that has this value

    # define current_node
    current_node = self.head

    # define position
    node_id = 1

    # define list of results
    results = []

    while current_node is not None:
    if current_node.data == value:
    results.append(node_id)
     
    node_id = node_id + 1
    current_node = current_node.next

    return results

    def remove_list_item_by_id(self, id):
    # remove the list item with the item id

    current_id = 1
    current_node = self.head
    previous_node = None

    while current_node is not None:
    if current_id == id:
    if previous_node is not None:
    previous_node.next = current_node.next
    else:
    self.head = current_node.next
    previous_node = current_node
    current_node = current_node.next
    current_id = current_id + 1
    return

    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
     
    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:")
    list1.output_list()

    list1.add_list_item(node1)
    print("\n 加入 node,目前的 list:")
    list1.output_list()
    list1.add_list_item('20')
    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_list_item_by_id(3)
    print("\n 移除 第 3 個 node,目前的 list:")
    list1.output_list()

    list1.reverse()
    print("\n 反轉 list,目前的 list:")
    list1.output_list()

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

    """
    list1 = SingleLinkedList()
    list1.add_list_item(node1)
    print(list1)
    list1.add_list_item('12')
    print(list1)
    list1.add_list_item(15)
    list1.add_list_item(17)
    print(list1)
    list1.remove_list_item_by_id(3)
    list1.reverse()
    list1.remove_value(15)
    list1.output_list()
    """
     
    全部共 0則留言
    登入帳號密碼代表遵守學術網路規範


    文章分類 Labels


    最新文章 Top10

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