有這么一句話說“程序=數據結構+算法”,也有人說“如果把編程比作做菜,那么數據結構就好比食材(菜),算法就好比廚藝(做菜的技巧)”。
當然這是籠統的說法,不過也稍微懂得了數據結構和算法的重要性了。
其實,數據結構是數據間的有機關系,而算法是對數據的操作步驟;兩者不可分開來談,不能脫離算法來討論數據結構,也不能脫離數據結構研究算法。
在網上看到這樣一段話:
數據結構并不是來教你怎樣編程的,同樣編程語言的精練也不在數據結構的管轄范圍之內,那是教你學習一門語言的老師的事情,他肯定不想因為數據結構而失業。數據結構是教你如何在現有程序的基礎上把它變得更優(運算更快,占用資源更少),它改變的是程序的存儲運算結構而不是程序語言本身。
如果把程序看成一輛汽車,那么程序語言就構成了這輛車的車身和輪胎。而算法則是這輛車的核心——發動機。這輛車跑得是快是慢,關鍵就在于發動機的好壞(當然輪胎太爛了也不行),而數據結構就是用來改造發動機的。
可以這么說,數據結構并不是一門語言,它是一種思想,一種方法,一種思維方式。它并不受語言的限制,你完全可以在gave 中輕而易舉地實現一個用C語言給出的算法。
或許你在初入職場的時候并不會涉及到需要使用到數據結構的地方,也因而覺得數據結構貌似沒用,但這和“農民工也能蓋大樓,干嘛還學建筑呢?” 是一個道理,應該都懂。
前面說了超長的廢話,其實我是想介紹一些數據結構的學習資源,主要針對編程新手,希望可以解決新手們“如何學習數據結構”的困惑,若對你有所幫助,那真是極好的。
?
淺談單鏈表與雙鏈表的區別
用代碼解釋兩種結構---
單鏈表:
class Node:def __init__(self, data):self.data = dataself.next = Nonedef __str__(self):return str(self.data)# 通過單鏈表構建一個list的結構: 添加 刪除 插入 查找 獲取長度 判斷是否為空...
# list1 = [] list1.append(5) [5,] slist = SingleList() slist.append(5)
class SingleList:def __init__(self, node=None):self._head = nodedef isEmpty(self):return self._head == Nonedef append(self, item):# 尾部添加node = Node(item)if self.isEmpty():self._head = nodeelse:cur = self._headwhile cur.next != None:cur = cur.nextcur.next = node# 求長度def len(self):cur = self._headcount = 0while cur != None:count += 1cur = cur.nextreturn count# 遍歷def print_all(self):cur = self._headwhile cur != None:print(cur)cur = cur.nextdef pop(self, index):if index < 0 or index >= self.len():raise IndexError('index Error')if index == 0:self._head = self._head.nextelse:cur = self._head# 找到當前下標的前一個元素while index - 1:cur = cur.nextindex -= 1# 修改的next的指向位置cur.next = cur.next.nextdef insert(self, index, item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(item, Node):raise TypeError('不能是Node類型')else:node = Node(item)if index == 0:node.next = self._headself._head = nodeelse:cur = self._headwhile index - 1:cur = cur.nextindex -= 1node.next = cur.nextcur.next = nodedef update(self, index, new_item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(new_item, Node):raise TypeError('不能是Node類型')else:node = Node(new_item)if index == 0:node.next = self._head.nextself._head = nodeelse:cur = self._headnode.next = cur.next.nextcur.next = nodedef remove(self, item):if isinstance(item, Node):raise TypeError('不能是Node類型')else:node = Node(item)cur = self._headwhile cur == node:cur = cur.nextcur.next = cur.next.nextif __name__ == '__main__':slist = SingleList()print(slist.isEmpty()) # Trueprint(slist.len())slist.append(5)print(slist.isEmpty()) # Falseprint(slist.len()) # 1slist.append(8)slist.append(6)slist.append(3)slist.append(1)print(slist.isEmpty()) # Trueprint(slist.len())print('---------------------')slist.print_all()print('----------pop-------------')slist.pop(2)slist.print_all()print('--------insert-------')slist.insert(1, 19)slist.print_all()print('--------update-------')slist.update(1, 18)slist.print_all()print('--------remove-------')slist.remove(18)slist.print_all()
雙鏈表:
'''
雙向鏈表
'''class Node:def __init__(self, data):self.data = dataself.next = Noneself.prev = Nonedef __str__(self):return str(self.data)class DoubleList:def __init__(self):self._head = Nonedef isEmpty(self):return self._head == Nonedef append(self, item):# 尾部添加node = Node(item)if self.isEmpty():self._head = nodeelse:cur = self._headwhile cur.next != None:cur = cur.nextcur.next = node# 求長度def add(self, item):node = Node(item)if self.isEmpty():self._head = nodeelse:node.next = self._headself._head.prev = nodeself._head = nodedef len(self):cur = self._headcount = 0while cur != None:count += 1cur = cur.nextreturn countdef print_all(self):cur = self._headwhile cur != None:print(cur)cur = cur.nextdef insert(self, index, item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(item, Node):raise TypeError('不能是Node類型')else:node = Node(item)if index == 0:node.next = self._headnode.prev = self._head.prevself._head = nodeelse:cur = self._headwhile index - 1:cur = cur.nextindex -= 1node.next = cur.nextnode.prev = cur.prevcur.next = nodecur.prev = node.prevdef remove(self, item):if isinstance(item, Node):raise TypeError('不能是Node類型')else:node = Node(item)cur = self._headwhile cur == node:cur = cur.nextcur.next = cur.next.nextcur.prev = cur.prevdef update(self, index, new_item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(new_item, Node):raise TypeError('不能是Node類型')else:node = Node(new_item)if index == 0:node.next = self._head.nextnode.prev = self._head.prevself._head = nodeelse:cur = self._headnode.next = cur.next.nextnode.prev = cur.prevcur.next = nodecur.prev = nodeif __name__ == '__main__':dlist = DoubleList()print(dlist.len())print(dlist.isEmpty())# dlist.append(6)# dlist.append(9)# dlist.append(5)# print(dlist.len())# print(dlist.isEmpty())# dlist.print_all()dlist.add(6)dlist.add(9)dlist.add(5)dlist.print_all()print('--------insert-------')dlist.insert(1, 19)dlist.print_all()print('--------update-------')dlist.update(1, 18)dlist.print_all()print('--------remove-------')dlist.remove(18)dlist.print_all()
?