def GetNo(self,e): #查找第一个为e的元素的序号 j=0 p=self.head.next while p is not None and p.data!=e: j+=1 #查找元素e p=p.next if p is None: return -1 #未找到时返回-1 else: return j #找到后返回其序号 def Insert(self, i, e): #在线性表中序号i位置插入元素e def Delete(self,i): #在线性表中删除序号i位置的元素 def display(self): #输出线性表 p=self.head.next while p is not None: print(p.data,end=' ') p=p.next; print()
时间: 2024-01-17 13:05:43 浏览: 97
好的,以下是补齐后的完整代码:
```python
class LinkNode: #单链表结点类
def __init__(self,data=None): #构造函数
self.data=data #data属性
self.next=None #next属性
class LinkList: #单链表类
def __init__(self): #构造函数
self.head=LinkNode() #头结点head
self.head.next=None
def CreateListF(self, a): #头插法:由数组a整体建立单链表
for i in range(len(a)):
node = LinkNode(a[i])
node.next = self.head.next
self.head.next = node
def CreateListR(self, a): #尾插法:由数组a整体建立单链表
p = self.head
for i in range(len(a)):
node = LinkNode(a[i])
p.next = node
p = node
def geti(self, i): #返回序号为i的结点
p = self.head
j = -1
while (j < i and p is not None):
j += 1
p = p.next
return p
def Add(self, e): #在线性表的末尾添加一个元素e
p = self.head
while p.next is not None:
p = p.next
node = LinkNode(e)
p.next = node
def GetNo(self,e): #查找第一个为e的元素的序号
j = 0
p = self.head.next
while p is not None and p.data != e:
j += 1 #查找元素e
p = p.next
if p is None:
return -1 #未找到时返回-1
else:
return j #找到后返回其序号
def Insert(self, i, e): #在线性表中序号i位置插入元素e
p = self.head
j = -1
while p is not None and j < i - 1:
j += 1
p = p.next
if p is None or j > i - 1:
print("插入位置错误")
return
node = LinkNode(e)
node.next = p.next
p.next = node
def Delete(self,i): #在线性表中删除序号i位置的元素
p = self.head
j = -1
while p.next is not None and j < i - 1:
j += 1
p = p.next
if p.next is None or j > i - 1:
print("删除位置错误")
return
p.next = p.next.next
def display(self): #输出线性表
p = self.head.next
while p is not None:
print(p.data,end=' ')
p = p.next
print()
def getsize(self): #返回长度
p = self.head
cnt = 0
while p.next is not None: #找到尾结点为止
cnt += 1
p = p.next
return cnt
```
阅读全文