vc++6.0 编程问题 输入员工信息链表,编写deln函数,删除第n个节点;
展开全部
#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 "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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询