Project-linked list class "
Dynamic linked list is also a very useful data structure in program design. It can be said that whether you can understand the principle of operation, determines whether you are eligible to be called "a". In the subsequent professional basic courses, the relevant content will be from different angles, repeated understanding, repeated practice. However, it is necessary to have more experience at this stage.
#include <iostream>using namespace Std;class Student//Node class {public:student (int n,double s); ~student () {if (!next) {delete next; } next=null; } Student *next; Points to the next node int num; Double score;}; student::student (int n,double s) {num=n; Score=s; Next=null;} Class MyList//list category, where the members are students {Public:mylist () {head=null; } MyList (int n,double s); Student (N,s) as a single-node linked list ~mylist (); int display (); Output linked list, return value is the number of nodes in the linked list void insert (int n,double s); Insert: Inserts the student (n,s) node into the linked list, which acts as the first node void append (int n,double s); Append: Inserts the student (n,s) node into the linked list, which acts as the last node void cat (MyList &il); Joins the list IL to the following int length () of the current object; Returns the number of nodes in the list (another processing, which can be a node number as a data member) Private:student *head; The head node of the linked list};//The following is the definition of the class member function mylist::mylist (int n,double s)//with Student (N,s) as a single-node linked list {head=new Student (n,s);} Mylist::~mylist () {Student *p,*q; P=head; if (p!=null) {q=p; p=p->next; Delete Q } Head=null;} int MyList::d isplay ()//Output list, the return value is the node number in the linked list {if (head==null) {cout<< "empty\n"; return 0; } int con=0; Student *p; P=head; if (p!=null) {++con; cout<<p->num<< "" <<p->score<<endl; p=p->next; }}void Mylist::insert (int n,double s)//insert: Inserts the Student (n,s) node into the linked list, which acts as the first node {Student * pt=new Student (n,s); Pt->next =head; Head=pt;} void Mylist::append (int n,double s)//append: Inserts the Student (n,s) node into the linked list, which acts as the last node {Student *pt=new Student (n,s); if (head==null) {head=pt; } else {Student *pts=head; Student *pte=pts->next; while (PTE) {pts=pte; pte=pts->next; } pts->next=pt; }}void Mylist::cat (MyList &il)//Connect the list Il to the back of the current object {Student *pt=il.head; while (PT) {append (Pt->num,pt->score); pt=pt->next; }}int mylist::length ()//Returns the number of nodes in the list (another processing that canNode count as a data member)//test function {int con=0; Student *p=head; while (p) {++con; p=p->next; } return con; int main () {int n; Double S; MyList Head1; cout<< "input head1:" <<endl; Enter head1 linked list for (int i=0; i<3; i++) {cin>>n>>s; Head1.insert (n,s); By "inserting" the Way} cout<< "Head1:" <<endl; Output Head1 head1.display (); MyList head2 (1001,98.4); Establish head2 linked list head2.append (1002,73.5); Add node Head2.append (1003,92.8) by "append" method; Head2.append (1004,99.7); cout<< "head2:" <<endl; Output head2 head2.display (); Head2.cat (HEAD1); Append head1 to head2. cout<< "Length of head2 after Cat:" <
Operation Result:
Knowledge point application and experience:
Linked list has been not quite understand, today is a list of the deepening of a lost lost.
Linked list Topics