算法界内的线性数据结构(我所了解的)都有链表、栈、队列与数组。今天我们用Python一一实现一遍。
栈:
class Stack:
def __init__(self,size):
self.size = size
self.stack = []
for i in range(self.size):
self.stack += [None].copy()
self.used = 0
def push(self,value):
if self.used==self.size:
raise Exception("栈已满!")
for i in range(self.size):
if self.stack[i] == None:
self.stack[i] = value
break
self.used+=1
def pop(self):
tmp = -1
try:
for i in range(self.size-1,-1,-1):
if self.stack[i] == None:
continue
tmp = self.stack[i]
del self.stack[i]
self.stack+=[None]
break
except:
raise Exception("栈已经为空!")
else:
return tmp
def getValue(self):
return self.stack
栈是FILO(First In Last Out,先进后出)上面的代码为栈实现了三个功能,一个是出栈,一个是进栈,还有一个是获取值。
队列:
class Queue:
def __init__(self,size):
self.size = size
self.queue = []
for i in range(self.size):
self.queue += [None].copy()
self.used = 0
def enqueue(self,value):
if self.used==self.size:
raise Exception("队列已满!")
for i in range(self.size):
if self.queue[i] == None:
self.queue[i] = value
break
self.used+=1
def dequeue(self):
try:
tmp = self.queue[0]
del self.queue[0]
self.queue+=[None]
return tmp
except:
raise Exception("队列已经为空!")
def getValue(self):
return self.queue
队列是FIFO(First In First Out,先进先出),上面也为队列实现了同样的三个功能。
链表:
class Node:
def __init__(self,data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.size = 0
self.head = None
self.tail = None
def get(self,index):
p = self.head
for i in range(index):
p = p.next
return p
def add(self,data):
node = Node(data)
self.last.next = node
self.last = node
def insert(self,data,index):
node = Node(data)
if self.size == 0:
self.head = node
self.last = node
elif index == 0:
node.next = self.head
self.head = node
elif self.size == index:
self.last.next = node
self.last = node
else:
tmp = self.get(index-1)
node.next = tmp.next
tmp.next = node
self.size += 1
def remove(self,index):
if index==0:
remove = self.head
self.head = self.head.next
elif index == self.size - 1:
tmp = self.get(index-1)
remove = tmp.next
tmp.next = None
self.last = tmp
else:
tmp = self.get(index-1)
next_node = tmp.next.next
remove = tmp.next
tmp.next = next_node
self.size -= 1
return remove
def getValue(self):
p = self.head
while p is not None:
print(p.data)
p = p.next
上面的链表有四个方法,第一个是获取指定索引上的节点get,第二个是在链表末尾添加元素,第三个是在指定位置插入元素节点,第四个是删除指定位置上的节点。
数组:
class Array:
def __init__(self,size):
self.size = size
self.array = []
for i in range(self.size):
self.array += [None]
def insert(self,index,data):
if self.size >= len(self.array):
self.resize
for i in range(self.size-1,-1,-1):
self.array[i+1] = self.array[i]
self.array[index] = data
self.size += 1
def resize(self):
array_new = []
for i in range(len(self.array)*2):
array_new += [None]
for i in range(self.size):
array_new[i] = self.array[i]
self.array = array_new.copy()
def remove(self,index):
for i in range(index,self.size):
self.array[i] = self.array[i+1]
self.size -= 1
def getValue(self):
for i in range(self.size):
print(self.array[i],end=' ')
print()
上面的代码为数组类提供了4个方法,第一个是在指定位置插入元素。第二个是把数组扩大到原来的两倍。第三个是删除指定位置的元素,第四个是打印出数组所有的元素。
以上就是我给大家提供的四大线性数据类型的代码,希望大家把这些代码吃透,以后做项目的时候会受益无穷!