The Unordered List Abstract Data Type

The structure of an unordered list, as described above, is a collection of items where each item holds a relative position with respect to the others. Some possible unordered list operations are given below.

List() creates a new list that is empty. It needs no parameters and returns an empty list.

add(item) adds a new item to the list. It needs the item and returns nothing. Assume the item is not already in the list.

remove(item) removes the item from the list. It needs the item and modifies the list. Assume the item is present in the list.

search(item) searches for the item in the list. It needs the item and returns a boolean value.

isEmpty() tests to see whether the list is empty. It needs no parameters and returns a boolean value.

size() returns the number of items in the list. It needs no parameters and returns an integer.

append(item) adds a new item to the end of the list making it the last item in the collection. It needs the item and returns nothing. Assume the item is not already in the list.

index(item) returns the position of item in the list. It needs the item and returns the index. Assume the item is in the list.

insert(pos,item) adds a new item to the list at position pos. It needs the item and returns nothing. Assume the item is not already in the list and there are enough existing items to have position pos.

pop() removes and returns the last item in the list. It needs nothing and returns an item. Assume the list has at least one item.

pop(pos) removes and returns the item at position pos. It needs the position and returns the item. Assume the item is in the list.

Implementing an Unordered List: Linked Lists

The Node Class

The basic building block for the linked list implementation is the node. Each node object must hold at least two pieces of information. First, the node must contain the list item itself. We will call this the data field of the node. In addition, each node must hold a reference to the next node.

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

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self,newdata):
        self.data = newdata

    def setNext(self,newnext):
        self.next = newnext


temp = Node(93)
temp.getData()
print(temp.data)

The Unordered List Class
As we suggested above, the unordered list will be built from a collection of nodes, each linked to the next by explicit references. As long as we know where to find the first node (containing the first item), each item after that can be found by successively following the next links. With this in mind, the UnorderedList class must maintain a reference to the first node. Listing 2 shows the constructor. Note that each list object will maintain a single reference to the head of the list.

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

    def getData(self):
        return self.data

    def getNext(self):
        return self.next

    def setData(self,newdata):
        self.data = newdata

    def setNext(self,newnext):
        self.next = newnext

class UnorderedList:
    def __init__(self):
        self.head = None

    def isEmpty(self):
        return self.head == None

    def add(self,item):#在頭部加
        temp = Node(item)#創建一個NODE
        temp.setNext(self.head)#把頭加到後面
        self.head = temp#把新頭給新的NODE

    def size(self):
        current = self.head
        count = 0#初始化爲0
        while current != None:#當頭不爲空的時候
            current = current.getNext()#向下移動
            count = count + 1#
        return count#循環結束後返回值

    def search(self,item):
        current = self.head#從頭開始找
        found = False#設置found爲否
        while current != None and not found:#當Head不爲空,並且也沒找到
            if current.getData() == item:#找到
                found = True#找到,結束循環
            else:
                current = current.getNext()#如果沒有找到,去下一個

        return found#循環結束後返回FOUND

    #去除
    def remove(self,item):
        current = self.head#指針爲當前
        previous = None#前一個爲NODE
        found = False#沒發現
        while not found:#當沒有被發現的時候
            if current.getData() == item:#如果找到
                found = True#找到結束循環
            else:
                previous = current#如果沒找到,那麼前一個就變成了目前這個
                current = current.getNext()#目前這個就變成了

        if previous == None:#如果前面爲空,說明當前就爲頭
            self.head = current.getNext()#那麼把頭給下一個,去除掉頭
        else:
            previous.setNext(current.getNext())#把目前的前一個的後一個設置爲目前的後一個,就去除掉了目前這個節點


temp = Node(93)
temp.getData()
print(temp.data)

linklist=UnorderedList()

linklist.add(20)
linklist.add(10)
linklist.size()
linklist.search(10)
linklist.remove(10)
print(linklist.size())
print(linklist.search(10))

總結:

  • LINKEDLIST:是由NODE組成的。初始化,下一個指針爲空
  • NODE包括:變量:VAL, NEXT指針,
    它的性質:設置他的VAL,得到他的VAL,設置他的指針,得到他的指針

LINKEDLIST: 變量head爲空,一開始沒頭
性質:
isEmpty是否爲空,判斷是否有頭結點

add:在前面添加節點
1. 先創建一個NODE,
2. 再把這個NODE的下一個設爲鏈表的頭
3. 再把鏈表現在的頭給新的NODE

size 得到大小
1. 把目前的NODE設置爲頭
2. 初始化COUNT=0
3. 當目前NODE不爲空,開始循環
目前的NODE 爲下一個NODE
COUNT+1
4. 當所有NODE遍歷完,返回COUNT

search(被搜索的)
1.把當前的NODE設置爲頭
2. FOUND爲否
3. 當目前節點不爲空,FOUND爲否的時候 開始循環
當目前節點的值爲被搜索的值時
found爲TRUE
如果不是:
目前爲下一個
4. 當循環結束的時候,返回FOUND

remove(要被去除的):先找-》再刪除
1.把當前的NODE設置爲頭
2.前一個爲空
3.found=false
4. 當沒有發現的時候循環
當目前節點的值爲被搜索的值時
found爲TRUE
如果不是:
目前爲下一個
5.找到後退出循環, 如果 前一個NODE爲NODE, 說明此時找的的節點爲頭結點。
那麼把鏈表的頭給下一個(說明去除了之前的頭結點)
如果不是, 設置前一個節點的後一個節點爲目前的下一個,去除掉了目前的接點

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章