import sys import random class employee: def __init__(self): self.num=0 self.salary=0 self.name='' self.next=None findword=0 data=[[None]*2 for row in range(12)] namedata=['Allen','Scott','Marry','Jon', 'Mark','Ricky','Lisa','Jasica', 'Hanson','Amy','Bob','Jack'] for i in range(12): data[i][0]=i+1 data[i][1]=random.randint(51,100) head1=employee() #建立第一组链表的头部 if not head1: print('Error!! 内存分配失败!!') sys.exit(0) head1.num=data[0][0] head1.name=namedata[0] head1.salary=data[0][1] head1.next=None ptr=head1 for i in range(1,12): #建立第一组链表 newnode=employee() newnode.num=data[i][0] newnode.name=namedata[i] newnode.salary=data[i][1] newnode.next=None ptr.next=newnode ptr=ptr.next ptr=head1 print('结果为:') k=0 while ptr!=None: #打印链表的数据 print('[%2d %6s %3d] => ' %(ptr.num,ptr.name,ptr.salary),end='') k=k+1 if k>=3: print() k=0 ptr=ptr.next
输出
结果为: [ 1 Allen 76] => [ 2 Scott 72] => [ 3 Marry 84] => [ 4 Jon 60] => [ 5 Mark 64] => [ 6 Ricky 80] => [ 7 Lisa 84] => [ 8 Jasica 91] => [ 9 Hanson 95] => [10 Amy 56] => [11 Bob 76] => [12 Jack 82] =>