第十五周上機任務項目2-建立專門的鏈表類處理有關動態鏈表的操作

來源:互聯網
上載者:User
01./*     02.* 程式的著作權和版本聲明部分     03.* Copyright (c)2013, 煙台大學電腦學院學生     04.* All rightsreserved.     05.* 檔案名稱:  Student.cpp                                06.* 作    者:趙冠哲                                 07.* 完成日期:2013年6月7日     08.* 版本號碼: v1.0           09.* 輸入描述:     10.* 問題描述:   11.*/        #include<iostream>using namespace std;class Student  //結點類{public:Student(int n,double s){num=n;score=s;next=NULL;}Student *next;   //指向下一個結點int num;double score;};class MyList  //鏈表類{public:MyList(){head=NULL;}MyList(int n,double s); //以Student(n,s)作為單結點的鏈表int display();  //輸出鏈表,傳回值為鏈表中的結點數void insert(int n,double s);  //插入:將Student(n,s)結點插入鏈表,該結點作為第一個結點void append(int n,double s);  //追加:將Student(n,s)結點插入鏈表,該結點作為最後一個結點void cat(MyList &il); //將鏈表il串連到當前對象的後面int length();  //返回鏈表中的結點數private:Student *head;   //鏈表的頭結點};//以下為類成員函數的定義int MyList::display(){    Student *p=head;    int count=0;    while(p!=NULL)    {        cout<<p->num<<" "<<p->score<<endl;        p=p->next;       count++;    }    return count;}void MyList::insert(int n, double s){    Student *p=new Student(n, s);    p->next = head;    head = p;}void MyList::append(int n, double s){    Student *p = head, *q;    while(p!=NULL)    {        q=p;        p=p->next;    }    q->next=new Student(n, s);}void MyList::cat(MyList &il){    Student *p=head,*q;    while(p!=NULL)    {        q=p;        p=p->next;    }}int MyList::length(){   Student *p=head;    int count=0;    while(p!=NULL) {      p=p->next;        count++;    }   return count;}//測試函數int main(){int n;double s;MyList head1;cout<<"input head1: "<<endl;  //輸入head1鏈表for(int i=0;i<3;i++){cin>>n>>s;head1.insert(n,s);  //通過“插入”的方式}cout<<"head1: "<<endl; //輸出head1head1.display();MyList head2(1001,98.4);  //建立head2鏈表head2.append(1002,73.5);  //通過“追加”的方式增加結點head2.append(1003,92.8);head2.append(1004,99.7);cout<<"head2: "<<endl;   //輸出head2head2.display();head2.cat(head1);   //把head1追加到head2後面cout<<"length of head2 after cat: "<<head2.length()<<endl;cout<<"head2 after cat: "<<endl;   //顯示追加後的結果head2.display();return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.