#环形链表插入新节点
class employee:
def __init__(self):
self.num=0
self.salary=0
self.name=''
self.next=0
def findnode(head,num):
ptr=head
while ptr.next!=head:
if ptr.num==num:
return ptr
ptr=ptr.next
return ptr
def insertnode(head,after,num,salary,name):
InsertNode=employee()
CurNode=None
InsertNode.num=num
InsertNode.salary=salary
InsertNode.name=name
InsertNode.next=None
if InsertNode==None:
print('内存分配失败')
return None
else:
if head==None:
head=InsertNode
InsertNode.next=head
return head
else:
if after.next==head:
InsertNode.next=head
CurNode=head
while CurNode.next!=head:
CurNode=CurNode.next
CurNode.next=InsertNode
head=InsertNode
return head
else:
InsertNode.next=after.next
after.next=InsertNode
return head
position=0
namedata=['Allen','Scott','Marry','John','Mark','Ricky','Lisa','Jasica','Hanson','Amy','Bob','Jack']
data=[[1001,32367],[1002,24388],[1003,27556],[1007,31299],
[1012,42660],[1014,25675],[1018,44145],[1043,52182],
[1031,32769],[1037,21100],[1041,32196],[1046,25776]]
print('员工编号 薪水 员工编号 薪水 员工编号 薪水 员工编号 薪水')
print('-------------------------------------------------------------')
for i in range(3):
for j in range(4):
print('[%2d] [%3d]'%(data[i+j*3][0],data[i+3*j][1]),end='')
print()
head=employee()
if not head:
print('Error!!内存分配失败!!')
sys.exit(0)
head.num=data[0][0]
head.salary=data[0][1]
head.name=namedata[0]
head.next=None
ptr=head
for i in range(1,12):
newnode=employee()
newnode.num=data[i][0]
newnode.salary=data[i][1]
newnode.name=namedata[i]
newnode.next=None
ptr.next=newnode
ptr=newnode
newnode.next=head
while True:
print('请输入要插入其后的员工编号,如果输入的编号不在此链表中,')
position=int(input('则新输入的员工编号将视为此链表的第一个节点,要结束过程请输入-1:'))
if position==-1:
break
else:
ptr=findnode(head,position)
new_num=int(input('请输入新插入的员工编号:'))
new_salary=int(input('请输入新插入员工的薪水:'))
new_name=input('请输入新插入员工的姓名:')
head=insertnode(head,ptr,new_num,new_salary,new_name)
ptr=head
print('\t员工编号 姓名\t 薪水')
print('\t======================')
while True:
print('\t[%2d]\t[%-10s]\t[%3d]'%(ptr.num,ptr.name,ptr.salary))
ptr=ptr.next
if head==ptr or head==head.next:
break