c++实验答案

内容发布更新时间 : 2024/5/13 7:13:51星期一 下面是文章的全部内容请认真阅读。

} }

node *Del( node *head, int no) { node *pc,*pa,*headtemp; headtemp=pc=pa=head;

if (head=NULL) //链表为空的情况 { cout << \链表为空,无结点可删!\\t\ return NULL; }

if (pc->no==no) // 第一个结点为要删除结点的情况

{ head=pc->next; //将第二个结点的地址赋给head,

//使首结点从链表中分离出来

delete pc; //删除首结点 cout<<\删除了一个结点!\\n\ }

else //第一个结点不是要删除的结点

{ while (pc->no!=no && pc->next!=0 ) //查找要删除的结点

{ pa=pc; //当前结点地址由pc赋给pa

pc=pc->next; //pc指向下一个结点 }

if (pc==NULL) //若pc为空表示链表中无要删除的结点

cout << \链表中没有要删除的结点!\\n\ else

{ pa->next=pc->next;//将下结点地址赋给上结点,使删除结点从链表分离出来 delete pc; //删除指定结点 cout<<\删除一个结点!\\n\ }

head=headtemp; }

return head; //返回链表头指针 }

node * Insert(node * head, int no)

{ node *pc,*pa,*pn; //定义指向插入点前、后的指针pc与pa

pn=new node;

cout<<\请输入工号、姓名、应发工资、税金 :\ cin>>pn->no>>pn->name>>pn->salary>>pn->tax;

pn->fsalary=pn->salary+pn->tax; pc=pa=head;

if (head==0) //若链表为空,则新结点插入到链表首 { head=pn;

pn->next=0; return head; }

if (pc->no==no) // 第一个结点为要插入结点的情况

{ pn->next=head; head=pn; return head; }

else //第一个结点不是要插入的结点

{ while (pc->no!=no && pc->next!=0 ) //查找要插入的结点位置

{ pa=pc; //当前结点地址由pc赋给pa

pc=pc->next; //pc指向下一个结点 }

pc->next=pn; //新结点插入到链尾 pn->next=0; }

return head; //返回链表头指针 }

void main(void) //主函数 { node * head; int no;

head=Create(); //产生无序链表 Calcu(head);

Print(head); //输出无序链表 cout<<\输入要删除结点上职工工号:\\n\ cin>>no;

head=Del(head,no); //删除指定工号的结点 Print(head); //输出显示删除后的链表。 cout<<\输入要插入结点职工工号:\\n\ cin>>no;

head=Insert(head,no); //插入指定工号的结点

Print(head); //输出显示更新后的链表。 }

程序运行结果:

产生无序链表,请输入工号、姓名、应发工资、税金,以工号为-1结束:

1001

Zhang 1900 85 1002 Wang 1800 80 1003 Li 1700 70 1004 Zhou 2000 90 -1

输出链表中各结点值:

1001 Zhang 1900 85 1815 1002 Wang 1800 80 1720 1003 Li 1700 70 1630 1004 Zhou 2000 90 1910 输入要删除结点上职工工号: 1003

删除一个结点!

输出链表中各结点值:

1001 Zhang 1900 85 1815 1002 Wang 1800 80 1720 1004 Zhou 2000 90 1910 输入要插入结点职工工号: 1005

请输入工号、姓名、应发工资、税金 : 1005 wu 2100 100 输出链表中各结点值:

1001 Zhang 1900 85 1815 1002 Wang 1800 80 1720 1004 Zhou 2000 90 1910 1005 wu 2100 100 2000 (3)

# include # include struct node { int no;

char name[8]; float salary,tax; float fsalary; node *next; };

void Print(const node *head) { const node *p; p=head;

cout<<\输出链表中各结点值:\

while (p!=0 )

{ cout<no<<'\\t'<name<<'\\t'<salary <<'\\t'<tax<<'\\t'<fsalary<<'\\t'<next; } }

void Delchain(node * head) { node * p;

p=head; //链表头指针赋给p

while (head) //当链表非空时删除结点

{ head=p->next; //将链表下一个结点指针赋给head delete p; //删除链表第一个结点 p=head; //再将头指针赋给p } }

node * Insert(node * head, node *pn)

{ node *pc,*pa; //定义指向插入点前、后的指针pc与pa pc=pa=head;

if (head==0) //若链表为空,则新结点插入到链表首 { head=pn;

pn->next=0; return head; }

if (pn->fsalary<=head->fsalary) //若新结点实发工资≥首结点实发工资, //则新结点插在链首 { pn->next=head; head=pn; return head; }

while (pc->next!=0 && pn->fsalary>=pc->fsalary) //若链表非空,则按实发工资查找插 //入点

{ pa=pc; // pc、pa移到插入点前、后结点处

pc=pc->next; }

if (pn->score >= pc->score) //新结点插入到链尾 { pc->next=pn; pn->next=0; }

else //新结点插入到链表中间 { pn->next=pc; pa->next=pn;

}

return head; //返回链表头指针 }

node *Create_sort( void)

{ node *pn,*head=0; //定义指向新结点的指针变量pn及链表头指针head int no; //定义输入职工工号临时变量no cout<<\产生一条有序链表,请输入数据,以-1结束!\\n\ cin>>no; //输入职工工号

while (no!= -1) //工号不等于-1则循环

{ pn=new node; //动态分配node类型结点空间,并将其地址赋给pn pn->no=no;

cin>>pn->name; //输入职工姓名 cin>>pn->salary; cin>>pn->tax;

pn->fsalary=pn->salary-pn->tax;

head=Insert(head,pn); //调用结点插入函数,将新结点按实发工资降序插入链表 cin>>no; //输入职工工号 }

return head; //返回链表头指针 }

void main(void) //主函数 { node * head;

head=Create_sort(); //产生一个有序链表 Print (head); //输出显示有序链表 Delchain(head); //删除整个链表 }

程序运行结果:

产生一条有序链表,请输入数据,以-1结束! 1001 Zhang 1900 85 1002 Wang 1800 80 1003 Li 1700 70 1004 Zhou 2000 90 -1

输出链表中各结点值:

1003 Li 1700 70 1630 1002 Wang 1800 80 1720 1001 Zhang 1900 85 1815 1004 Zhou 2000 90 1910

实验十一 1.实验目的

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4 ceshi