数据结构之链表的实例

来源:互联网 发布:西门子冰箱知乎 编辑:程序博客网 时间:2024/06/05 09:25
/*学生管理系统*/#include<iostream>#include<string.h>using namespace std;class Stu{public:    char no[20];    string name;    double score;    Stu &operator=(Stu &student);};Stu &Stu::operator=(Stu &student){    strcpy(this->no,student.no);    this->name=student.name;    this->score=student.score;    return *this;}class ListNode{public:    Stu data;    ListNode *next;};class List{public:    List();//创建    ~List();//销毁    bool ListInsertTail(ListNode *pNode);//尾插法    void ListTraverse();//遍历    ListNode *pList;};List::List(){    pList=new ListNode;    pList->next=NULL;}List::~List(){    ListNode *currentNode=pList;    while(currentNode->next!=NULL)    {        ListNode *temp=currentNode->next;        delete currentNode;        currentNode=temp;    }    delete pList;    pList=NULL;}bool List::ListInsertTail(ListNode *pNode){    ListNode *currentNode=pList;    while(currentNode->next!=NULL)    {        currentNode=currentNode->next;    }    ListNode*newNode=new ListNode;    if(newNode==NULL)        return false;    newNode->data=pNode->data;    newNode->next=NULL;    currentNode->next=newNode;    return true;}void List::ListTraverse(){    ListNode *currentNode=pList;    while(currentNode->next!=NULL)    {        currentNode=currentNode->next;        cout<<endl<<currentNode->data.name<<"\t";        cout<<currentNode->data.no<<"\t";        cout<<currentNode->data.score<<endl;    }}void create(List *p){    cout<<"几个学生?\n";    int n;    cin>>n;    for(int i=0;i<n;i++)    {        ListNode q;        cout<<"请输入学生学号和学生姓名和学生成绩";        cin>>q.data.no>>q.data.name>>q.data.score;        p->ListInsertTail(&q);    }}void add(List *p){    cout<<"插入几个学生?\n";    int n;    cin>>n;    for(int i=0;i<n;i++)    {        ListNode q;        cout<<"请输入学生学号和学生姓名和学生成绩";        cin>>q.data.no>>q.data.name>>q.data.score;        p->ListInsertTail(&q);    }}void Delete(List *p){    bool flag=false;    cout<<"请问选择删除的姓名";    string temp;    cin>>temp;    ListNode *currentNode=p->pList;    ListNode *currentNodebefore=currentNode;    while(currentNode->next!=NULL)    {        currentNode=currentNode->next;        if(currentNode->data.name==temp)        {            currentNodebefore->next=currentNode->next;            flag=true;            break;        }    }    if(flag)    {        cout<<"已经删除完毕"<<endl;    }    else    {        cout<<"找不到删除对象"<<endl;    }}void Find(List *p){    cout<<"你要查找的姓名是";    string temp;    cin>>temp;    ListNode *currentNode=p->pList;    while(currentNode->next!=NULL)    {        currentNode=currentNode->next;        if(currentNode->data.name==temp)        {        cout<<endl<<currentNode->data.name<<"\t";        cout<<currentNode->data.no<<"\t";        cout<<currentNode->data.score<<endl;        }    }}void Revise(List *p){    cout<<"请问要修改的名字";    string temp;    cin>>temp;    cout<<"请问要修改的成绩是多少";    double sum;    cin>>sum;    ListNode *currentNode=p->pList;    while(currentNode->next!=NULL)    {        currentNode=currentNode->next;        if(currentNode->data.name==temp)        {            currentNode->data.score=sum;            break;        }    }}void menu(){    bool flag=true;    int n;    List *p=new List();    while(flag)    {    cout<<"******学生成绩管理系统******\n";    cout<<"****0:建立******************\n";    cout<<"****1:添加******************\n";    cout<<"****2:删除******************\n";    cout<<"****3:查询******************\n";    cout<<"****4:显示******************\n";    cout<<"****5:修改******************\n";    cout<<"****6:退出******************\n";    cin>>n;    switch(n)    {        case 0:create(p);break;        case 1:add(p);break;        case 2:Delete(p);break;        case 3:Find(p);break;        case 4:p->ListTraverse();break;        case 5:Revise(p);break;        case 6:flag=false;break;    }    }    delete p;    p=NULL;}int main(void){    menu();}


原创粉丝点击