C++ 建立一个职员的工作链表,编写函数del,能删除节点,并且增加功能:按工号num1修改员工的工资wage。
#include<iostream>usingnamespacestd;structstaff{longnum;charname[20];doublewage;struc...
#include <iostream>
using namespace std;
struct staff
{
long num;
char name[20];
double wage;
struct staff *next;
};
struct staff *del(staff *head,long num)
{
struct staff *p,*q;
if(head==NULL)
cout<<"list null!"<<endl;
else
{
p=head;
while(num!=p->num&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(num==p->num)
{
if(p==head)
head=p->next;
else
q->next=p->next;
delete p;
}
else
cout<<num<<"not been found!"<<endl;
}
return(head);
}
void main()
{
int n;
staff *head=NULL,*newnode,*tail=NULL;
cout<<"请输入初始的节点数"<<endl;
cin>>n;
cout<<"input num, name and wage"<<endl;
for(int i=0;i<n;i++)
{
newnode=new staff;
cin>>newnode->num>>newnode->name>>newnode->wage;
if(head==NULL)
head=newnode;
else
tail->next=newnode;
tail=newnode;
}
tail->next=NULL;
cout<<"请输入所要删除的员工工号:"<<endl;
long num,num1;
cin>>num;
double change;
head=del(head,num);
cout<<"请输入需要修改工资的员工工号:"<<endl;
cin>>num1;
cout<<"请输入修改后的工资值:"<<endl;
cin>>change;
staff *q=head;
while(q!=NULL)
{
if(q->num==num1)
q->wage=change;
else
q=q->next;}
staff *p=head;
while(p!=NULL)
{
cout<<p->num<<" "<<p->name<<" "<<p->wage<<endl;
p=p->next;
}
}
求改错。。。谢谢 展开
using namespace std;
struct staff
{
long num;
char name[20];
double wage;
struct staff *next;
};
struct staff *del(staff *head,long num)
{
struct staff *p,*q;
if(head==NULL)
cout<<"list null!"<<endl;
else
{
p=head;
while(num!=p->num&&p->next!=NULL)
{
q=p;
p=p->next;
}
if(num==p->num)
{
if(p==head)
head=p->next;
else
q->next=p->next;
delete p;
}
else
cout<<num<<"not been found!"<<endl;
}
return(head);
}
void main()
{
int n;
staff *head=NULL,*newnode,*tail=NULL;
cout<<"请输入初始的节点数"<<endl;
cin>>n;
cout<<"input num, name and wage"<<endl;
for(int i=0;i<n;i++)
{
newnode=new staff;
cin>>newnode->num>>newnode->name>>newnode->wage;
if(head==NULL)
head=newnode;
else
tail->next=newnode;
tail=newnode;
}
tail->next=NULL;
cout<<"请输入所要删除的员工工号:"<<endl;
long num,num1;
cin>>num;
double change;
head=del(head,num);
cout<<"请输入需要修改工资的员工工号:"<<endl;
cin>>num1;
cout<<"请输入修改后的工资值:"<<endl;
cin>>change;
staff *q=head;
while(q!=NULL)
{
if(q->num==num1)
q->wage=change;
else
q=q->next;}
staff *p=head;
while(p!=NULL)
{
cout<<p->num<<" "<<p->name<<" "<<p->wage<<endl;
p=p->next;
}
}
求改错。。。谢谢 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询