c++创建线性链表记录学生信息

来源:互联网 发布:sigmaplot linux 编辑:程序博客网 时间:2024/06/16 06:07

记录如图所示的学生信息表格


这里写图片描述


调用程序时先输入学生的个数,然后依次输入学生的学号,姓名等信息

#include<iostream>#include<string>using namespace std;/* 创建节点 */struct Student{    char num[10];    string name;    string sex;    int age;    string nation;    string political;    string place;    Student *next; //存放下一个节点的地址};/* 创建一个链表 */Student *Creat(){    Student *p, *q, *head; //p存放新节点地址,q存放上一个节点地址(当前链表的最后一个节点地址),head存放头节点地址       int i = 1, number; //number记录学生个数       cout << "please input numbers' number: " << endl;       cin >> number;       cout << endl;       head = NULL; q = NULL;// 指针初始化       while (i <= number)       {           p = new Student; //创建一个新节点(空间大小为Student型占用的空间大小)           cout << "please input number: "<< i << endl;           cin >> p->num;           cout << "please input name: "<< i << endl;           cin >> p->name;           cout << "please input sex: "<< i << endl;           cin >> p->sex;           cout << "please input age: "<< i << endl;           cin >> p->age;           cout << "please input nation: "<< i << endl;           cin >> p->nation;           cout << "please input political: "<< i << endl;           cin >> p->political;           cout << "please input place: "<< i << endl;           cin >> p->place;           if (head == NULL) //头节点为0,表示链表为空           {               head = p; //将p设置为头节点的地址               q = p; //创建第一个节点时,q也指向头节点           }           else //已有头节点,将新创建的节点p放在上一个节点q后面           {               q->next = p; //新创建的节点放在q后面               q = p; //将q指向新链表的最后一个节点           }           i++; //当前节点数加1       }       q->next = NULL; //最后一个节点的next成员为NULL       return head;}int main(){    Student *head,*p;    cout<<"start\n"<<endl;    head = Creat();    p=head;    cout<<"print\n"<<endl;    cout<<"number name sex age nation political place"<<endl;    while(p!=NULL)    {        cout<<p->num<<" "<<p->name<<"  "<<p->sex<<"  "<<p->age;        cout<<p->nation<<"  "<<p->political<<"  "<<p->place<<endl;        p = p->next;    }    return 0;}

附:查找年龄为20岁的算法,在上面主程序的main函数 return 0 语句前面添加一句find_age(head,20);,然后将查找子函数放到main函数前面即可,如图所示:


这里写图片描述


查找函数程序为:

Student *find_age(Student *f,int age){    cout<<"start to find age!"<<endl;    while(f!=NULL)    {        if(f->age == age)        {            cout<<f->num<<" "<<f->name<<"  "<<f->sex<<"  "<<f->age;            cout<<f->nation<<"  "<<f->political<<"  "<<f->place<<endl;        }        f = f->next;    }    return 0;}

顺便再补充打印链表长度和删除链表元素的函数:
(一)首先是打印链表长度函数:

Student *lengthList(Student *l){    cout<<"start to compute length of list !"<<endl;    int i=0;    while (l!=NULL)    {        i++;        l = l->next;    }    cout<<"the length of list is "<< i << endl;    return 0;}

(二)然后是删除链表元素函数:(此函数功能是删除链表,并打印删除后的链表以及链表长度)

Student *delete_one(Student *l){    string num1;    string num2;    num1 = "003";    num2 = "005";    Student *s,*p; s = l;p=l;    cout<<"start to delet !"<<endl;    while(l!=NULL)    {        if(l->next==NULL)            break;        if(l->next->num==num1)            l->next = l->next->next;        else if (l->next->num == num2)            l->next=l->next->next;        else            l=l->next;    }    cout<<"number name sex age nation political place"<<endl;    while (s!=NULL)    {        cout<<s->num<<" "<<s->name<<"  "<<s->sex<<"  "<<s->age;        cout<<s->nation<<"  "<<s->political<<"  "<<s->place<<endl;        s = s->next;    }    lengthList(p);    return 0;}
0 0
原创粉丝点击