The structure of an ordered list is a collection of items where each item holds a relative position that is based upon some underlying characteristic of the item. The ordering is typically either ascending or descending and we assume that list items have a meaningful comparison operation that is already defined. Many of the ordered list operations are the same as those of the unordered list.
OrderedList() creates a new ordered list that is empty. It needs no parameters and returns an empty list.
add(item) adds a new item to the list making sure that the order is preserved. 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.
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.
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 Ordered List
class OrderedList:
def __init__(self):
self.head = None
def search(self,item):
current = self.head
found = False
stop = False
while current != None and not found and not stop:
if current.getData() == item:#找到
found = True
else:#如果没有找到
if current.getData() > item:#当前的值大于被找的值
stop = True#停止,没有必须向下找了
else:
current = current.getNext()#如果不是,下一个
return found
def add(self,item):#添加节点
current = self.head
previous = None
stop = False
#1. 先查找合适的位置
while current != None and not stop:
if current.getData() > item:#当当前节点的值大于添加的节点
stop = True#停止退出循环,再这个节点前添加
else:
previous = current
current = current.getNext()
#2.新建NODE
temp = Node(item)#创建NODE
#3.判断前一个节点的是头还是不是
if previous == None:#如果前面为空,说明为头结点
temp.setNext(self.head)#设置NODE为头
self.head = temp
else:
#设置前后
temp.setNext(current)#如果前面有,在当前节点前面添加,
previous.setNext(temp)#前一个下一个为新节点
Summary
Linear data structures maintain their data in an ordered fashion.
Stacks are simple data structures that maintain a LIFO, last-in first-out, ordering.
The fundamental operations for a stack are push, pop, and isEmpty.
Queues are simple data structures that maintain a FIFO, first-in first-out, ordering.
The fundamental operations for a queue are enqueue, dequeue, and isEmpty.
Prefix, infix, and postfix are all ways to write expressions.
Stacks are very useful for designing algorithms to evaluate and translate expressions.
Stacks can provide a reversal characteristic.
Queues can assist in the construction of timing simulations.
Simulations use random number generators to create a real-life situation and allow us to answer “what if” types of questions.
Deques are data structures that allow hybrid behavior like that of stacks and queues.
The fundamental operations for a deque are addFront, addRear, removeFront, removeRear, and isEmpty.
Lists are collections of items where each item holds a relative position.
A linked list implementation maintains logical order without requiring physical storage requirements.
Modification to the head of the linked list is a special case.