请求帮助
请你编一个c或c++程序;要求如下:
文件student(二进制)存放了已初始化好的四个链表节点,节点结构体定义如下:
struct student
{int number;
char name[15];
student *next;}
已初始化好的四个节点,每个包括student.number(学生学号), student.name(学生姓名),next指针设为NULL.
3 从文件student中依次读出四个节点,连成链表,按学号大小排序.
4 定义add(),del(),print()函数,分别实现对链表节点的插入、删除、输出。
或帮我改一下我的程序:
#include "iostream.h"
#include "stdio.h"
#include "iomanip.h"
#include "string.h"
typedef struct student
{short number;char name[15];student *next;}students;
void add(short a,char *x,students *head)
{students *pr,*p=new students;p->number=a;strcpy(p->name,x);p->next=NULL;
for(pr=head;pr->next!=NULL;pr=pr->next);pr->next=p;
}
students * del(short a,students *head)
{students *p,*pr;
if(head->number==a){p=head;head=head->next;delete p;}
for(p=head->next,pr=head;p!=NULL;p=p->next,pr=pr->next)
if(p->number==a){pr->next=p->next;delete p;p=pr->next;}
else cout<<"The number is error\n";return head;
}
void print(students *head)
{students *p,*pr,*k1;
while(head->next!=NULL)
{short k=head->number;k1=head;
for(pr=head,p=head->next;p!=NULL;pr=pr->next,p=p->next)
if(k>p->number) {k=p->number;k1=p;}
cout<<setw(10)<<k<<k1->name<<endl;
if(k1==head)head=head->next;
else pr->next=p->next;
delete k1;
}
cout<<setw(10)<<head->number<<head->name<<endl;delete head;
}
void main()
{students *p,*pr,*head;
FILE *in;
if((in=fopen("students","rb"))==NULL)
cout<<"can not open students-file"<<endl;
head=new students;fread(head,19,1,in);pr=head;
for(int i=1;i<4;i++)
{p=new students;fread(p,19,1,in);pr->next=p;pr=p;}
fclose(in);
for(p=head;p!=NULL;p=p->next)
cout<<setw(10)<<p->number<<p->name<<endl;
cout<<"D or d------------delete\n"<<"A or a------------add\n"
<<"N or n------------no adding no deleting\n"
<<"Please enter your choose\n";
char ch=getchar();
if(ch=='d'||ch=='D')
{cout<<"Please enter the number you want to delete\n";short a;cin>>a;head=del(a,head);}
if(ch=='a'||ch=='A')
{cout<<"Please enter student's massage\n";
int b;cin>>b;char ch1[15];cin>>ch1;add(b,ch1,head);
}
for(p=head;p!=NULL;p=p->next)
cout<<setw(10)<<p->number<<p->name<<endl;
//print(head);
}