vc++6.0 编程问题 输入员工信息链表,编写deln函数,删除第n个节点; 10
两个程序第一个运行没问题,运行完,输出删完节点的链表后,程序出错。也就是说,第一个程序和第二个程序运行结果一样,第二个正常结束,第一个出现错误。你们自己运行一下好了。求大...
两个程序第一个运行没问题,运行完,输出删完节点的链表后,程序出错。
也就是说,第一个程序和第二个程序运行结果一样,第二个正常结束,第一个出现错误。
你们自己运行一下好了。
求大神解答,谢谢。
#include "iostream.h"
#include "string.h"
struct node
{
char num[6];
char name[20];
double wage;
node *next;
};
struct node *create(int n)
{
struct node *head=NULL;
struct node *tail,*newnode;
for(int i=0;i<n;i++)
{
newnode=new node;
cin>>newnode->num >>newnode->name >>newnode->wage;
if(head==NULL)
head=newnode;
else
tail->next=newnode;
tail=newnode;
}
return head;
}
struct node *delnode(node *head,char x[])
{
struct node *p,*q;
p=head;
if(head==NULL)
cout<<"The list is null!\n";
else
{
while(p!=NULL&&strcmp(p->num,x)!=0)
{
q=p;
p=p->next;
}
if(p==head)
{
head=p->next;
delete p;
}
else if(p!=NULL)
{
q->next=p->next;
delete p;
}
else
cout<<x<<"dose not exist in the list!\n";
}
return head;
}
void print(node *head )
{
struct node*p=head;
while(p!=NULL)
{
cout<<p->num<<'\t'<<p->name<<'\t'<<p->wage<<endl;
p=p->next;
}
}
void main()
{
int n;
char c[10];
double wage;
cout<<"输入员工数"<<endl;
cin>>n;
cout<<"输入工号,姓名,工资"<<endl;
struct node *head;
head=create(n);
cout<<"输入要删除员工工号"<<endl;
cin>>c;
head=delnode(head,c);
cout<<"删除后"<<endl;
print(head);
}
#include "iostream.h"
struct node
{
char num[6];
char name[20];
double wage;
node *next;
} *q,*p,*head;
void deln(int n)
{
int i=0;
if (n==1)
{
head = head->next;
return;
}
p=head;
while (p!=NULL)
{
i++;
if (i==n)
{
q->next = p->next;
return;
}
q = p;
p = p->next;
}
}
void main()
{
int i,m,n;
cin >> m;
for (i=1; i<=m; i++)
{
p = new node;
cin >> p->num >> p->name >> p->wage;
if (i==1)
head =p;
else
q->next = p;
q = p;
}
p->next = NULL;
cin >> n;
deln(n);
p=head;
while (p!=NULL)
{
cout << p->num << ends << p->name << ends << p->wage << endl;
p = p->next;
}
} 展开
也就是说,第一个程序和第二个程序运行结果一样,第二个正常结束,第一个出现错误。
你们自己运行一下好了。
求大神解答,谢谢。
#include "iostream.h"
#include "string.h"
struct node
{
char num[6];
char name[20];
double wage;
node *next;
};
struct node *create(int n)
{
struct node *head=NULL;
struct node *tail,*newnode;
for(int i=0;i<n;i++)
{
newnode=new node;
cin>>newnode->num >>newnode->name >>newnode->wage;
if(head==NULL)
head=newnode;
else
tail->next=newnode;
tail=newnode;
}
return head;
}
struct node *delnode(node *head,char x[])
{
struct node *p,*q;
p=head;
if(head==NULL)
cout<<"The list is null!\n";
else
{
while(p!=NULL&&strcmp(p->num,x)!=0)
{
q=p;
p=p->next;
}
if(p==head)
{
head=p->next;
delete p;
}
else if(p!=NULL)
{
q->next=p->next;
delete p;
}
else
cout<<x<<"dose not exist in the list!\n";
}
return head;
}
void print(node *head )
{
struct node*p=head;
while(p!=NULL)
{
cout<<p->num<<'\t'<<p->name<<'\t'<<p->wage<<endl;
p=p->next;
}
}
void main()
{
int n;
char c[10];
double wage;
cout<<"输入员工数"<<endl;
cin>>n;
cout<<"输入工号,姓名,工资"<<endl;
struct node *head;
head=create(n);
cout<<"输入要删除员工工号"<<endl;
cin>>c;
head=delnode(head,c);
cout<<"删除后"<<endl;
print(head);
}
#include "iostream.h"
struct node
{
char num[6];
char name[20];
double wage;
node *next;
} *q,*p,*head;
void deln(int n)
{
int i=0;
if (n==1)
{
head = head->next;
return;
}
p=head;
while (p!=NULL)
{
i++;
if (i==n)
{
q->next = p->next;
return;
}
q = p;
p = p->next;
}
}
void main()
{
int i,m,n;
cin >> m;
for (i=1; i<=m; i++)
{
p = new node;
cin >> p->num >> p->name >> p->wage;
if (i==1)
head =p;
else
q->next = p;
q = p;
}
p->next = NULL;
cin >> n;
deln(n);
p=head;
while (p!=NULL)
{
cout << p->num << ends << p->name << ends << p->wage << endl;
p = p->next;
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询