链表
链表
class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext
无序链表
class UnorderedList:
# init_head is last Node.
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
def add(self,item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.getNext()
return count
def search(self,item):
current = self.head
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
current = current.getNext()
return found
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 == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
# 遍历 Python
# 遍历 C++
# 遍历 C++ 递归
[Link][] https://xidianwlc.gitbooks.io/python-data-structrue-and-algrothms/content/3.%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/3.21.%E5%AE%9E%E7%8E%B0%E6%97%A0%E5%BA%8F%E5%88%97%E8%A1%A8%EF%BC%9A%E9%93%BE%E8%A1%A8/
current = myList.head
while current != None :
print(current.getData())
current = current.getNext()
# 遍历 C++
void display(node *head){
node *p = head;
while(p){
cout<<p->data<<"->";
p = p->next;
}
}
# 遍历 C++ 递归
void display(node *head){
if(!head) {
//当head为空的时候,肯定是到达了链表的尾部,然后跳出,这就是传说中的basic case
return ;
}
cout<< head->data << "->"; //只要当前节点不为空,那么肯定能到达这一步然后就会进入下一个节点
return display(head->next);
}
[Link][] https://blog.csdn.net/sdlwzzm19971226/article/details/79845926[Link][] https://xidianwlc.gitbooks.io/python-data-structrue-and-algrothms/content/3.%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/3.21.%E5%AE%9E%E7%8E%B0%E6%97%A0%E5%BA%8F%E5%88%97%E8%A1%A8%EF%BC%9A%E9%93%BE%E8%A1%A8/
评论
发表评论