[笔记]python数据结构之线性表:linkedlist链表,stack栈,queue队列
python数据结构之线性表
1.链表
class ListNode(object): def __init__(self, data): self.data = data self.next = None def getData(self): return self.data def setData(self, newData): self.data = newData def getNext(self): return self.next def setNext(self, nextNode): self.next = nextNode利用链表节点,组成无序链表类:
class UnorderedList(object): def __init__(self): self.head = None def getHead(self): return self.head def isEmpty(self): return self.head is None def add(self, item): node = ListNode(item) node.next = self.head self.head = node # the head is the most recently added node def size(self): current = self.head count = 0 while current is not None: count += 1 current = current.getNext() return count def search(self, item): current = self.head found = False while current is not None and not found: if current.getData() == item: found = True else: current = current.getNext() return found def append(self, item): node = ListNode(item) if self.isEmpty(): self.head = node else: current = self.head while current.getNext() is not None: current = current.getNext() current.setNext(node) def remove(self, item): current = self.head previous = None found = False while not found: if current.getData() == item: found = True else: previous = current current = current.getNext() if previous is None: self.head = current.getNext() else: previous.setNext(current.getNext())在上面的链表中,每次添加元素都直接添加在链表头部,add()的时间复杂度为O(1),而append()操作在队尾,其时间复杂度为O(n)。有没有前后加入操作的时间复杂度都为O(1)的链表呢,当然是有的:
class UnorderedList(object): def __init__(self): self.head = None self.tail = None def getHead(self): return self.head def isEmpty(self): return self.head is None and self.tail is None def add(self, item): node = ListNode(item) if self.isEmpty(): self.head = self.tail = node else: node.next = self.head self.head = node # the head is the most recently added node def size(self): current = self.head count = 0 while current is not None: count += 1 current = current.getNext() return count def search(self, item): current = self.head found = False while current is not None and not found: if current.getData() == item: found = True else: current = current.getNext() return found def append(self, item): node = ListNode(item) self.tail.setNext(node) self.tail = node def remove(self, item): current = self.head previous = None found = False while not found: if current.getData() == item: found = True else: previous = current current = current.getNext() if current.getNext() is None: self.tail = previous if previous is None: self.head = current.getNext() else: previous.setNext(current.getNext())对无序链表类加入一个属性,引用链表末尾节点,即可。做出了这样的改变,在add和remove操作也应作出相应变化。
class OrderedList(object): def __init__(self): self.head = None def isEmpty(self): return self.head is None def search(self, item): stop = False found = False current = self.head while current is not None and not found and not stop: if current.getData() > item: stop = True elif current.getData() == item: found = True else: current = current.getNext() return found def add(self, item): previous = None current = self.head stop = False while current is not None and not stop: if current.getData() >item: stop = True else: previous = current current = current.getNext() node = ListNode(item) if previous is None: node.getNext(current) self.head = node else: previous.setNext(node) node.setNext(current)
2.栈stack
class Stack(object): def __init__(self): self._items = [] def is_empty(self): return self._items == [] def push(self, item): self._items.append(item) def pop(self): return self._items.pop() def peek(self): return self._items[-1]当然了,我们也可以自己实现链栈,跟链表的实现类似。
class StackNode(object): """docstring for StackNode""" def __init__(self, value): self.value = value self.next = None class Stack(object): """docstring for Stack""" def __init__(self, top=None): self.top = top def get_top(self): return self.top def is_empty(self): return self.top is None def push(self, val): if self.is_empty(): self.top = StackNode(val) return else: node = StackNode(val) node.next = self.top.next self.top = node return def pop(self): if self.is_empty(): print("Stack is Empty, cannot pop anymore.\n") return node = self.top self.top = self.top.next return node
3.队列queue
class QueueNode(object): def __init__(self, value): self.value = value self.next = None class Queue(object): def __init__(self): self.front = None self.rear = None def is_empty(self): return self.front is None and self.rear is None def enqueue(self, num): node = QueueNode(num) if self.is_empty(): self.front = node self.rear = node else: self.rear.next = node self.rear = node def dequeue(self): if self.front is self.rear: node = self.front self.front = None self.rear = None return node.value else: node = self.front self.front = node.next return node.value在python的库中,比如collections以及Queue中都有deque模块。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。