建立一个链表,每个节点包括学生的学号、姓名、性别、年龄,学号

要能运行的,发到邮箱[email protected] 谢谢。。

# include"stdio.h"

#include"stdlib.h"

struct data

{

int num;

char name[20];

char sex[4];

int age;

struct data*next;

};

int main()

{

int i,k;

struct data*head,*p,*q,*t;

head=t=p=q=(struct data*)malloc(sizeof(struct data));

printf("输入学号:");

scanf("%d",&p->num);

getchar();

printf("输入姓名:");

gets(p->name);

printf("输入性别:");

gets(p->sex);

        printf("输入年龄:");

scanf("%d",&p->age);

while(p->num!=-1)

{

q->next=p;

q=p;

      p=(struct data*)malloc(sizeof(struct data));

    printf("输入学号:");

scanf("%d",&p->num);

getchar();

if(p->num==-1)  break;

printf("输入姓名:");

gets(p->name);

printf("输入性别:");

gets(p->sex);

printf("输入年龄:");

scanf("%d",&p->age);

}

    q->next=NULL;

free(p);

printf("输出的结果是:\n");

while(t!=NULL) 

{ printf("%-5d%8s%8s%8d",t->num,t->name,t->sex,t->age);

           t=t->next;

   printf("\n");

}

    

printf("请输入你要查找的学生的学生号:");

scanf("%d",&i);

p=head;

while(p!=NULL)

{

        if(i == p->num)

{

printf("\n%-5d%8s%8s%8d\n",p->num,p->name,p->sex,p->age);

                                  

}

p=p->next;

}

return 0;

}

姐姐啊!你要两个学号啊!我给你编了可以查学号的!祝你成功!

我运行的情况!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-27
# include "iostream"
# include "String.h"
using namespace std;
struct List
{
char name[20];
int NO;
};
template <class List>
struct Node
{
List data;
Node<List> *next;
};
template <class List>
class Student
{
Node<List> *head;
public:
Student(int m);
void Setdata(int n);
void PrintStudent();
int Get(char b[]);
void Delete(int i);
void Insert(int i,List item);
void Change(char a[],int num);
~Student();
};
template <class List>
Student<List>::Student(int m)
{
Node<List> *r,*s;
head = new Node<List>;
r = head;
for (int i = 0;i < m;i++)
{
s = new Node<List>;
r->next = s;
r = s;
}
r->next = NULL;
}
template <class List>
void Student<List>::Setdata(int n)
{
Node<List> *p;
p = head->next;
for(int i = 0;i < n;i++)
{
cout<<"请输入姓名:";
cin>>p->data.name;
cout<<"请输入身份证号码:";
cin>>p->data.NO;
p = p->next;
}
}
template <class List>
void Student<List>::PrintStudent()
{
Node<List> *p;
p = head->next;
while (p)
{
cout<<p->data.name<<","<<p->data.NO<<endl;
p = p->next;
}
}
template <class List>
int Student<List>::Get(char b[])
{
Node<List> *p;
p = head->next;
while(p && strcmp(p->data.name,b) != 0)
{
p = p->next;
}
return p->data.NO;
}
template <class List>
void Student<List>::Delete(int i)
{
Node<List> *p,*q;
p = head;
int j = 0;
while(p && j < i-1)
{
p = p->next;
j++;
}
if(!p || !p->next) {cerr<<"删除位置非法";exit(1);}
else
{
List x;
q = p->next;
x = q->data;
p->next = q->next;
delete q;
cout<<x.name<<","<<x.NO<<endl;
}
}
template <class List>
void Student<List>::Insert(int i,List item)
{
Node<List> *p,*s;
p = head;
int j = 0;
while (p && j < i-1)
{
p = p->next;
j++;
}
if(!p) {cerr<<"插入位置非法";exit(1);}
else
{
s = new Node<List>;
strcpy(s->data.name,item.name);
s->data.NO = item.NO;
s->next = p->next;
p->next = s;
}
}
template <class List>
void Student<List>::Change(char a[],int num)
{
Node<List> *p;
p = head->next;
while (p && strcmp(p->data.name,a) != 0)
{
p = p->next;
}
p->data.NO = num;
}
template <class List>
Student<List>::~Student()
{
Node<List> *p,*q;
p = head;
while (p)
{
q = p;
p = p->next;
delete q;
}
head = NULL;

}
void main()
{
cout<<"请建立学籍系统"<<endl;
int m;
cout<<"请输入总人数:";
cin>>m;
Student<List> stu(m);
int n = m;
stu.Setdata(n);
stu.PrintStudent();
int choice;
do
{
cout<<"********"<<endl;
cout<<"1.查询"<<endl;
cout<<"2.删除"<<endl;
cout<<"3.插入"<<endl;
cout<<"4.修改"<<endl;
cout<<"0.退出"<<endl;
cout<<"********"<<endl;
cout<<"请输入您的选择:";
cin>>choice;
switch(choice)
{
case 1:
{
char b[20];
cout<<"请输入需查询的姓名:";
cin>>b;
cout<<"查询人的学号为:";
cout<<stu.Get(b)<<endl;
}break;
case 2:
{
int i;
cout<<"请输入需删除数据的位置:";
cin>>i;
cout<<"删除的数据为:";
stu.Delete(i);
}break;
case 3:
{
List item;
cout<<"请输入需插入学生的姓名:";
cin>>item.name;
cout<<"请输入需插入学生的学号:";
cin>>item.NO;
int i;
cout<<"请输入需插入的位置:";
cin>>i;
stu.Insert(i,item);
stu.PrintStudent();
}break;
case 4:
{
char a[20];
cout<<"请输入需修改人的姓名:";
cin>>a;
int num;
cout<<"请输入修改的新学号:";
cin>>num;
stu.Change(a,num);
stu.PrintStudent();
}break;
case 0:
{
exit(1);
}break;
default:
{
cout<<"选择项非法,请重新选择";
cout<<endl;
cout<<endl;
}
}

} while(choice);

}
第2个回答  2011-06-09
#include<stdio.h>
#include<malloc.h>

struct student
{
int num;
char name [20];
int sex;
int age;
struct student*next;
};
struct student *creat(struct student *head)
{
struct student *p1,*p2;
p1=(struct student*) malloc(sizeof(struct student));
p2=head;
head=p1;
p1->next =p2;
printf ("input the student`s date:\n[as: name number sex(1 as male;2 as female) age]\n");
gets(p1->name);
scanf ("%d%d%d",&p1->num,&p1->sex,&p1->age);
while ((p1->sex!=1)&&(p1->sex!=2))
{
printf ("the sex is wrong,please input again:\n(1 as male;2 as female)\n");
scanf ("%d",&p1->sex);
}
return (head);
}

struct student *delete_date (struct student *head)
{
struct student *temp,*p;
int number;
if (head==NULL)
printf ("list is null!\n");
else
{
printf ("please input the delete number:");
scanf ("%d",&number);
temp=head;
while (((temp->num)!=number)&&((temp->next)!=NULL))
{
p=temp;
temp=temp->next;
}
if ((temp->num)==number)
{
if (temp==head)
{
printf ("delete date's number: %d\n ",temp->num);
head=head->next;
}
else
{
p->next=temp->next;
printf ("delete date's number: %d\n ",temp->num);
free (temp);
}
}
else printf ("no find date!\n");
}
return (head);
}

void *output(struct student *head)
{
struct student *temp;
temp=head;
printf ("the all student`s date is :\n[name number age sex]");
{
printf ("%s %d %d",temp->name,temp->num,temp->age);
if (temp->sex==1)
printf ("male");
else printf ("female");
temp=temp->next;
}while (temp->next!=NULL);
}

void main ()
{
struct student *head;
int i=2;
head=NULL;
while (i!=0)
{
int clrscr();
printf ("please chose the number:\n");
printf ("1.add the date;\n");
printf ("2.delete the date;\n");
printf ("3.out put the date;\n");
printf ("0.end the program;\n");
scanf ("%d",&i);
switch (i)
{
case 1:head=creat(head);
case 2:head=delete_date(head);
case 3:output(head);
}
}
}
可以运行,我试过了
相似回答