Data Structures and Algorithms (10) - ordered list OrderedList

  • Basic definitions

Is a sorted list of data items in accordance with certain of its properties Bryant (e.g., integer size, has the alphabet) to determine the position in the list. The smaller the data closer to the head of the list, the more forward.

 

  •  The basic properties

 

orderedList()

Create an ordered list
add(item) Add a data item, and to maintain the overall sequence
remove(item) Remove a data item from a sorted list, the sorted list is modified
search(item) Find items, return type bool
isEmpty()

Whether empty table

size()

Returns the number of data items in the table

index(item) Returns the index of items in the table
pop() Ordered to remove the last entry table
pop(pos) Removes the specified item

 

  • Orderly table with the list

It should be noted that the relative position of the sorted list of data items, depending on the comparison "size" therebetween.

 

. 1  from Node Import the Node
 2  class orderedList ():
 . 3      DEF  the __init__ (Self):
 . 4          self.head = None # here first head to define header tables without property, save for the first node to the reference head is empty table . none 
. 5      DEF isEmpty (Self):
 . 6          return self.head == none
 . 7      DEF the Add (Self, Item):
 . 8          Current = self.head
 . 9          Previous = none
 10          STOP = False
 . 11          the while Current = none! and  Not stop:
12             if current.getData() > item:
13                 stop = True
14             else:  
15                 previous = current
16                 current = current.getNext()
17         temp = Node(item)
18         if previous == None:
19             temp.setNext(self.head)
20             self.head = temp
21         else:
22             temp.setNext(current)
23             previous.setNext(temp)
24 
25     def size(self):
26         current = self.head
27         count = 0
28         while current != None:
29             count += 1
30             current = current.getNext()
31         return count
32 
33     def search(self,item):
34         current = self.head
35         found = False
36         while current != None and not found:
37             if current.getData() == item:
38                 found = True
39             else:
40                 if current.getData() > item:
41                     stop = True
42                 else:
43                     current = current.getNext()
44         return found
45 
46 s = OrderedList()
47 s.add(5)
48 s.add(6)
49 s.add(7)
50 s.add(6)
51 print(s.head.getData())

 

Reference: https://www.bilibili.com/video/BV1QJ411w7bB?p=2 9

 

Guess you like

Origin www.cnblogs.com/yeshengCqupt/p/12617186.html