#include <iostream>
using namespace std;
struct staff
{
char num[6];
char name[20];
double wage;
struct staff*next;
};
staff*del(struct staff*head,int n)
{
int i=1;
struct staff*p,*q;
p=head;
if(head==NULL)
cout<<"The list is null!"<<endl;
else
{
if(n==1)
{
head=p->next;
delete p;
}
else
{
while(i!=n&&p!=NULL)
{
i++;
q=p;
p=p->next;
}
if(n==i&&p!=NULL)
{
q->next=p->next;
delete p;
}
else
cout<<"can't find it!"<<endl;
}
}
return head;
}
void print(struct staff*head)
{
struct staff *p=head;
while(p!=NULL)
{
cout<<p->num<<' '<<p->name<<' '<<p->wage<<' ';
p = p->next;
}
cout<<endl;
}
int main()
{
int x;
cout<<"Please input the acount of the staff:"<<endl;
cin>>x;
struct staff*head=NULL;
struct staff*newstaff,*tail;
for(int i=0;i<x;i++)
{
newstaff=new staff;//指针指向的地址需要储存数据需要申请空间
cin>>newstaff->num>>newstaff->name>>newstaff->wage;
if(head==NULL)
head=newstaff;
else
tail->next=newstaff;
tail=newstaff;
}
tail->next=NULL;
print(head);
cout<<"Please input the number of the staff you want to delete:"<<endl;
int n;
cin>>n;
struct staff*y=del(head,n);
print(y);
system("pause");
return 0;
}
为何在最后要新建一个变量y,直接使用head进行print就会报错