类链表的简单实现

来源:互联网 发布:中国域名注册商 编辑:程序博客网 时间:2024/06/10 05:44

刚学完类和对象,做了个简单的类指针,有插入和遍历的功能,没有删除。

类里有四个数据成员:data(用在判断函数),number(学号),name(姓名),*next(指针)。
两个成员函数:输入和输出函数。
有三个函数:judge(判断函数)、create(建立结点的函数)、show(遍历函数)。

#include<iostream>using namespace std;class node{public :    int data;    int number;    char name[20];    void in()    {        cout<<"请输入姓名"<<endl;        cin>>name;        cout<<"请输入学号"<<endl;        cin>>number;    }    void out()    {        cout<<"姓名是:"<<name<<endl;        cout<<"学号是"<<number<<endl;    }    node *next;};node *head=NULL;void create(node *&head){    node *p,*s=head;    p=new node;    p->in();    p->next=NULL;    if(head==NULL)    {        head=p;        s=p;        return;    }    p->next=s->next;    s->next=p;}void show(node *head){    node *showname=head;    while(showname)    {        showname->out();        showname=showname->next;    }}void judge(){       node *p=new node;    while(cin>>p->data)    {    if(p->data==1)    create(head);    if(p->data==2)    show(head);    if(p->data==0)        break;    cout<<"1--输入,2--遍历,0--退出"<<endl;    }}int main(){    cout<<"1--输入,2--遍历,0--退出"<<endl;    judge();}
0 0
原创粉丝点击