go version
package main
import "fmt"
type Objecter interface {} //任意对象类型
type Node struct{
Data Objecter
Next *Node //指针类型
}
type List struct {
headNode *Node
}
//判断是否为空的单链表
func (l *List) IsEmpty() bool {
if l.headNode == nil{
return true
}else{
return false
}
}
//单链表的长度
func (l *List) Length() int{
cursor :=l.headNode
count := 0
for cursor != nil{
count++
cursor = cursor.Next
}
return count
}
//获取头部节点
func (l *List) GetHeadNode() *Node{
return l.headNode
}
//从头部添加元素
func (l *List) Add (data Objecter){
node:=&Node{Data:data} //传入值转为node对象
node.Next = l.headNode //将节点的next指针指向head即可。
l.headNode = node //再把当前的节点指向head,也就是head前移了
}
//从尾部添加元素
func (l *List) Append (data Objecter){
//首先根据传入的值转成node节点
node := &Node{Data:data}
//判断是否为空
if l.IsEmpty(){
//将head指向当前的节点
l.headNode = node
}else{
cursor:=l.headNode
for cursor.Next!=nil{
//游标往后元素移动
//知道下个
cursor = cursor.Next
}
cursor.Next = node
}
}
//在指定位置添加元素
func (l *List) Insert(index int,data Objecter){
if index <0 {
//如果index小于0,加在头上
l.Add(data)
}else if index > l.Length(){
l.Append(data)
}else{
pre :=l.headNode
count:=0
for count<(index-1){
pre = pre.Next
count++
}
node :=&Node{Data:data}
//pre.Next表示需要插入位置的下一个节点
//node.Next指向后面的节点
//然后前面的节点指向node
node.Next = pre.Next
pre.Next = node
}
}
func (l *List) Remove(data Objecter){
pre := l.headNode
if pre.Data ==data{
l.headNode = pre.Next
}else{
for pre.Next != nil{
if pre.Next.Data ==data{
pre.Next = pre.Next.Next
}else{
pre = pre.Next
}
}
}
}
func (l *List) Contain(data Objecter) bool {
cursor:=l.headNode
for cursor!=nil{
if cursor.Data ==data{
return true
}
cursor = cursor.Next
}
return false
}
func traverseLinkList(l *List){
head:=l.GetHeadNode()
for head!=nil{
fmt.Println(head.Data)
head = head.Next
}
fmt.Println("======")
}
func main() {
list := List{}
//尾部添加
list.Append(1)
list.Append(2)
list.Append(3)
list.Append(4)
//头部添加
list.Add(5)
fmt.Println("长度===",list.Length())
//判断是否为空链表
bool:=list.IsEmpty()
fmt.Println(bool)
//在指定位置2插入23
list.Insert(2,23)
traverseLinkList(&list)
//是否包含元素23
isContain :=list.Contain(23)
fmt.Println("isContain: ",isContain)
//是否包含元素57
isContain =list.Contain(57)
fmt.Println("isNotContain: ",isContain)
//删除57
list.Remove(23)
traverseLinkList(&list)
}
python version
class Node:
def __init__(self,value=None):
self.value = value
self.next = None
class LinkList:
def __init__(self,head = None):
self.head = head
def is_empty(self):
"""
判断链表是否为空
"""
flag = False
pre = self.head
if pre.value is None:
flag = True
return flag
def length(self):
"""
获取链表的长度
"""
cursor = self.head
count = 0
while cursor is not None:
cursor = cursor.next
count+=1
return count
def get_head_node(self):
"""
获取头部节点
"""
return self.head
def add(self,value):
"""
从头部添加元素
"""
node = Node(value =value)
node.next = self.head
self.head = node
def append(self,value) :
"""
从尾部添加元素
"""
node = Node(value = value)
cursor = self.head
if self.head is None:
self.head = node
else:
while cursor.next is not None:
cursor = cursor.next
cursor.next = node
def insert(self,index,value):
if index < 0:
self.add(value)
elif index > self.length():
self.append(value)
else:
count = 0
cursor = self.head
while count < index -1:
cursor = cursor.next
count+=1
node = Node(value = value)
# cursor.next = node.next
# node = cursor.next
node.next = cursor.next
cursor.next = node
def remove(self,value):
cursor = self.head
if cursor.value == value:
self.head = cursor.next
else:
#判断next的值比较好
while cursor.next is not None:
if cursor.next.value==value:
cursor.next = cursor.next.next
else:
cursor = cursor.next
#如果使用cursor则需要知道前面的节点是什么。
# pre = None
# while cursor is not None:
# curosr = cursor.next
# pre = cursor
# if cursor.value==value:
# pre.next = cursor.next
def contains(self,value):
cursor = self.head
flag = False
if cursor.value == value:
flag = True
else:
while cursor is not None:
if cursor.value == value:
flag = True
break
else:
cursor = cursor.next
else:
flag = False
return flag
def traverse_list(self):
head = self.get_head_node()
cursor = head
while cursor is not None:
print(cursor.value)
cursor = cursor.next
print("traverse_over")
def main():
l = LinkList()
l.append(1)
l.append(2)
l.append(3)
l.append(4)
l.add(5)
print("长度===",l.length())
#判断是否为空链表
bool=l.is_empty()
print(bool)
#在指定位置2插入23
l.insert(2,23)
l.traverse_list()
#是否包含元素23
is_contain =l.contains(23)
print("is_contain: ",is_contain)
#是否包含元素57
is_contain =l.contains(57)
print("is_not_contain: ",is_contain)
#删除23
l.remove(23)
l.traverse_list()
if __name__ == "__main__":
main()
请发表评论