python 工资单向链表的节点插入的三种方式(头,中,尾)

import sys
#链表的三种插入形式
class employee:
    def __init__(self):
        self.num=0
        self.salary=0
        self.name=''
        self.next=None
def findnode(head,num):
    ptr=head
    while ptr!=None:
        if ptr.num==num:
            return ptr
        ptr=ptr.next
    return ptr
def insertnode(head,ptr,num,salary,name):
    InsertNode=employee()
    if not InsertNode:
        return None
    InsertNode.num=num
    InsertNode.salary=salary
    InsertNode.name=name
    InsertNode.next=None
    if ptr==None:
        InsertNode.next=head #插入链表头
        return InsertNode
    else:
        if ptr.next==None:
            ptr.next=InsertNode#插入链表尾
        else:
            InsertNode.next=ptr.next#插入链表中
            ptr.next=InsertNode
    return head
position=0
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]]
namenode=['Allen','Scott','Marry','John','Mark','Ricky','Lisa','Jasica','Hanson','Amy','Bob','Jack']
print('员工编号  薪水  员工编号  薪水  员工编号  薪水 员工编号  薪水')
print('------------------------------------------------------')
for i in range(3):
    for j in range(4):
        print('[%4d]$%5d'%(data[j*3+i][0],data[j*3+i][1]),end='')
    print()
print('------------------------------------------------------')

head=employee()
head.next=None

if not head:
    print('分配内存失败!!\n')
    sys.exit(1)
head.num=data[0][0]
head.salary=data[0][1]
head.name=namenode[0]
head.next=None

ptr=head
for i in range(1,12):
    newnode=employee()
    #newnode.next=None
    newnode.num=data[i][0]
    newnode.salary=data[i][1]
    newnode.name=namenode[i]
    newnode.next=None
    ptr.next=newnode
    ptr=ptr.next
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)
    print()
    
ptr=head
print('\t 员工编号      姓名\t 薪水')
print('\t=========================')
while ptr!=None:
    print('\t[%2d]\t[%-7s]\t[%3d]'%(ptr.num,ptr.name,ptr.salary))
    ptr=ptr.next

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值