Python模擬 堆疊,佇列,連結串列
阿新 • • 發佈:2019-01-04
1. 堆疊
class my_stack(object): def __init__(self, value): self.value = value # 前驅 self.before = None # 後繼 self.behind = None def __str__(self): return str(self.value) def top(stack): if isinstance(stack, my_stack): # 判斷物件是否合法 ifstack.behind is not None: # 如果behind屬性不為空 return top(stack.behind) # 模擬物件的連線,直至找到為空的物件,並返回 else: return stack # 如果behind 為空,返回其 def push(stack, ele): # 進棧 push_ele = my_stack(ele) # 例項物件 push_ele if isinstance(stack, my_stack): stack_top= top(stack) # 得到一個behind為空的物件 push_ele.before = stack_top # push_ele 的前驅 before 連線 stack物件 push_ele.before.behind = push_ele # stack物件的後繼 behind 連線 push_ele else: raise Exception('不要亂扔東西進來好麼') # 非法物件 def pop(stack): # 出棧 if isinstance(stack, my_stack): #判斷物件是否合法 stack_top = top(stack) # 得到一個behind為空的物件 if stack_top.before is not None: # 如果其前驅不為空,代表連線了物件 stack_top.before.behind = None # 前驅連線的後繼設為空 stack_top.behind = None # 後繼設為空 return stack_top else: print('已經是棧頂了') # 前驅為None,代表空棧
2. 佇列
class MyQueue(): def __init__(self, value=None): self.value = value # 前驅 self.before = None # 後繼 self.behind = None def __str__(self): if self.value is not None: return str(self.value) else: return 'None' def create_queue(): """僅有隊頭""" return MyQueue() # 例項 def last(queue): if isinstance(queue, MyQueue): if queue.behind is not None: # 返回一個後繼為空的物件,不為空繼續找 return last(queue.behind) else: return queue def push(queue, ele): # 入列 if isinstance(queue, MyQueue): last_queue = last(queue) new_queue = MyQueue(ele) last_queue.behind = new_queue # new_queue連線last(queue)的後繼 def pop(queue): # 出列 if queue.behind is not None: get_queue = queue.behind queue.behind = queue.behind.behind # queue.behind不為空時連線下一個物件的後繼 return get_queue else: print('佇列裡已經沒有元素了') def print_queue(queue): print(queue) if queue.behind is not None: print_queue(queue.behind)
3. 雙端佇列
class Deque: """模擬雙端佇列""" def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def addFront(self, item): self.items.append(item) # 新增到最後 def addRear(self, item): self.items.insert(0, item) # 插入到第一 def removeFront(self): return self.items.pop() # 刪除最後一個 def removeRear(self): return self.items.pop(0) # 刪除第一個 def size(self): return len(self.items)
4. 優先順序佇列
import queue as Q class Skill(object): def __init__(self, priority, description): self.priority = priority self.description = description def __lt__(self, other): return self.priority < other.priority def __str__(self): return '(' + str(self.priority) + ',\'' + self.description + '\')' def PriorityQueue_class(): que = Q.PriorityQueue() # 優先順序佇列 que.put(Skill(7, 'proficient7')) # 第一個引數作為判斷 que.put(Skill(5, 'proficient5')) que.put(Skill(6, 'proficient6')) que.put(Skill(10, 'expert')) que.put(Skill(1, 'novice')) print('end') while not que.empty(): print(que.get()) PriorityQueue_class()
5. 單向連結串列
class Node(object): def __init__(self, value): # 元素域 self.value = value # 變數 # 連結域 self.next = None # 指標 class LinkedListOneway(object): # 單向連結串列 def __init__(self, node=None): self.__head = node def __len__(self): # 遊標,用來遍歷連結串列 cur = self.__head # 記錄遍歷次數 count = 0 # 當前節點為None則說明已經遍歷完畢 while cur: count += 1 cur = cur.next # cur.next 指向None ,遍歷完成,迴圈結束 return count def is_empty(self): # 頭節點不為None則不為空 return self.__head == None def add(self, value): """ 頭插法 先讓新節點的next指向頭節點 再將頭節點替換為新節點 順序不可錯,要先保證原連結串列的鏈不斷,否則頭節點後面的鏈會丟失 """ node = Node(value) node.next = self.__head self.__head = node def append(self, value): """尾插法""" node = Node(value) cur = self.__head if self.is_empty(): self.__head = node else: while cur.next: cur = cur.next cur.next = node def insert(self, pos, value): # 應對特殊情況 if pos <= 0: self.add(value) elif pos > len(self) - 1: self.append(value) else: node = Node(value) prior = self.__head count = 0 # 在插入位置的前一個節點停下 while count < (pos - 1): prior = prior.next count += 1 # 先將插入節點與節點後的節點連線,防止連結串列斷掉,先連結後面的,再連結前面的 node.next = prior.next prior.next = node def remove(self, value): cur = self.__head prior = None while cur: if value == cur.value: # 判斷此節點是否是頭節點 if cur == self.__head: self.__head = cur.next else: prior.next = cur.next break # 還沒找到節點,有繼續遍歷 else: prior = cur cur = cur.next def search(self, value): cur = self.__head while cur: if value == cur.value: return True cur = cur.next return False def traverse(self): cur = self.__head while cur: print(cur.value) cur = cur.next
6. 雙向連結串列
class LinkedList(): def __init__(self, value=None): self.value = value # 前驅 self.before = None # 後繼 self.behind = None def __str__(self): if self.value is not None: return str(self.value) else: return 'None' def init(): return LinkedList('HEAD') def delete(linked_list): if isinstance(linked_list, LinkedList): if linked_list.behind is not None: delete(linked_list.behind) linked_list.behind = None linked_list.before = None linked_list.value = None def insert(linked_list, index, node): node = LinkedList(node) if isinstance(linked_list, LinkedList): i = 0 while linked_list.behind is not None: if i == index: break i += 1 linked_list = linked_list.behind if linked_list.behind is not None: node.behind = linked_list.behind linked_list.behind.before = node node.before, linked_list.behind = linked_list, node def remove(linked_list, index): if isinstance(linked_list, LinkedList): i = 0 while linked_list.behind is not None: if i == index: break i += 1 linked_list = linked_list.behind if linked_list.behind is not None: linked_list.behind.before = linked_list.before if linked_list.before is not None: linked_list.before.behind = linked_list.behind linked_list.behind = None linked_list.before = None linked_list.value = None def trave(linked_list): if isinstance(linked_list, LinkedList): print(linked_list) if linked_list.behind is not None: trave(linked_list.behind) def find(linked_list, index): if isinstance(linked_list, LinkedList): i = 0 while linked_list.behind is not None: if i == index: return linked_list i += 1 linked_list = linked_list.behind else: if i < index: raise Exception(404) return linked_list