1. Nest a linked list in the class, save the student's name and student number, you can operate it according to the operation of the linked list, but it is required that no other functions outside the class except the public function interface and the friend function can call the data content. You can perform related operations on it.
Why use a class to write a linked list instead of a c language structure to write a linked list?
Personally, I think one advantage of using a class to write a linked list is that it can encapsulate the functions of the linked list, that is, the outside cannot call the functions of the linked list in the class, because the default members of the structure and the class are different, and the default member in the class is private.
The second advantage is that no other functions (except the interface of the class) can be used outside the class to change the relevant content in the linked list, which ensures that the content in the linked list will not be easily changed!
The structure of this class is as follows:
class List{ typedef struct Node{ //Struct Nesting in Classes string name; string num; struct Node *next; }Node; public: List(); ~List(); void print()const; //output linked list bool insert(int pos); //Insertion of linked list bool Delete(int pos); //Deletion of linked list void seek(int pos)const; //linked list query void seek(string name)const; void seek2(string num)const; void fixed(string name); //Modification of the linked list void exchange(List L); //Sort the linked list int getlen(); //get the length of the linked list protected: Node * head; //protected object, pointer to structure (head pointer) };
This class uses the nesting of classes and structures, and defines a pointer to the structure of protected data in the class, which is the head pointer of the structure here. The public functions include some simple operations on the list, such as adding, deleting, modifying, and checking.
Constructor and Destructor:
The role of the constructor and destructor here is to initialize the linked list and delete the linked list one by one.
List::List() //The constructor of the class, which is the initialization of the linked list { head = new Node; head->name='0'; head->num='0'; head->next=NULL; } List::~List() //The destructor of the class, which is to delete each element node { Node *temp; temp=head; while(head) { head=head->next; delete temp; } head=NULL; }
The output and insertion functions are as follows:
The output and insertion function is a simple output function of the linked list, because it does not change its content, so set const after the function, so that the program can run relatively faster. The insert function specifies where to insert what value.
void List::print()const //The output of paying attention to the linked list of structures in the class { Node *p; p = head ->next; cout<<"Output in the order of member name and member student number:\n"; while(p) { cout<<p->name<<" "<<p->num<<endl; p = p->next; } } bool List::insert(int pos) //Insert data into linked list { Node *s,*t; int i; string name,num; if(pos<=0) //If the input position is less than or equal to 0, it means that the input position is incorrect if it is not inserted. { cout<<"The location you entered is incorrect!\n"; return false; } i=0; s=head; //Define a struct pointer to the head pointer while(i<pos-1&& s!=NULL) //Find where to insert { s=s->next; i++; } if(s==NULL) //If the previous position of the inserted position is empty, the position you entered is out of range { cout<<"The location you entered is out of range!\n"; return false; } t = new Node; cout<<"Please enter in the order of name and student number:\n"; //then enter the data you want to insert cin>>name>>num; t->name=name; t->num=num; t->next=s->next; //insert operation s->next=t; return true; }
The delete and find functions are as follows:
The delete function specifies the operation to delete it at a certain position, which is similar to the deletion of ordinary linked lists.
Lookup function Here, different lookup functions are set for the name, student number and location, so that you can observe the code after running it.
bool List::Delete(int pos) //Delete by location { int i; Node *p,*q; if(pos<=0) //If the position to be deleted is less than or equal to 0, the input position is incorrect { cout<<"The location you entered is incorrect!\n"; return false; } i=0; p=head; while(i<pos-1&&p!=NULL) //Find the previous node of the element to be removed { p=p->next; i++; } if(p==NULL) { cout<<"The location you entered is out of range!\n"; return false; } q=p->next; p->next=q->next; delete q; return true; } void List::seek(string name)const //Find data by name { int flag=0; Node *t; t=head->next; while(t) //Circular linked list lookup { if(t->name == name) { cout<<"The member you are looking for is:\n"; cout<<t->name<<" "<<t->num; flag=1; } t=t->next; } if(flag==0) cout<<"The data you are looking for was not found!\n"; } void List::seek2(string num)const //Find content by student ID { int flag=0; Node *t; t=head->next; while(t) { if(t->num == num) { cout<<"The member you are looking for is:\n"; cout<<t->name<<" "<<t->num; flag=1; } t=t->next; } if(flag==0) cout<<"The data you are looking for was not found!\n"; } void List::seek(int pos)const //Find content by location { Node *t; t=head; int i; while(i<pos&&t!=NULL) { t=t->next; i++; } if(t==NULL) { cout<<"The number you entered is out of range!\n"; } cout<<"The member you found is:\n"; cout<<t->name<<" "<<t->num; }
The functions for modification and sorting and length calculation are as follows:
The modification is because the student will change the student number due to changing majors or other things, so here is set to modify a person's student number.
Sorting is because most of the learning is sorted according to the order of names when printing the names of the class members in the class, so related sorting operations are also carried out here.
void List::fixed(string name) //Because some people will change their student number because of changing majors or schools { Node *t; string m_num; t=head; while(t) { if(t->name==name) { cout<<"Please enter the revised student number:"; cin>>m_num; t->num = m_num; } t=t->next; } } void List::exchange(List L) { Node *t,*s; int n; string name,num; t=head->next; s=t->next; n=L.getlen(); for(int i=0;i<n-1;i++) { for(int j=0;j<n-2-i;j++) { if(t->name < s->name) { name=t->name; num=t->num; t->name=s->name; t->num=s->num; s->name=name; s->num=num; } s=s->next; } t=t->next; } L.print(); } int List::getlen() { int i=1; Node *t; t=head->next; while(t) { t=t->next; i++; } return i; }
The running code of the overall operation is as follows: (please leave a message for borrowing, thank you! It is not easy for students to pay)
#include<iostream> #include<cstdlib> #include<cstring> using namespace std; class List{ typedef struct Node{ //Struct Nesting in Classes string name; string num; struct Node *next; }Node; public: List(); ~List(); void print()const; bool insert(int pos); bool Delete(int pos); void seek(int pos)const; void seek(string name)const; void seek2(string num)const; void fixed(string name); void exchange(List L); int getlen(); protected: Node * head; //protected object, pointer to structure (head pointer) }; List::List() //The constructor of the class, which is the initialization of the linked list { head = new Node; head->name='0'; head->num='0'; head->next=NULL; } List::~List() //The destructor of the class, which is to delete each element node { Node *temp; temp=head; while(head) { head=head->next; delete temp; } head=NULL; } void List::print()const //The output of paying attention to the linked list of structures in the class { Node *p; p = head ->next; cout<<"Output in the order of member name and member student number:\n"; while(p) { cout<<p->name<<" "<<p->num<<endl; p = p->next; } } bool List::insert(int pos) //Insert data into linked list { Node *s,*t; int i; string name,num; if(pos<=0) //If the input position is less than or equal to 0, it means that the input position is incorrect if it is not inserted. { cout<<"The location you entered is incorrect!\n"; return false; } i=0; s=head; //Define a struct pointer to the head pointer while(i<pos-1&& s!=NULL) //Find where to insert { s=s->next; i++; } if(s==NULL) //If the previous position of the inserted position is empty, the position you entered is out of range { cout<<"The location you entered is out of range!\n"; return false; } t = new Node; cout<<"Please enter in the order of name and student number:\n"; //then enter the data you want to insert cin>>name>>num; t->name=name; t->num=num; t->next=s->next; //insert operation s->next=t; return true; } bool List::Delete(int pos) //Delete by location { int i; Node *p,*q; if(pos<=0) //If the position to be deleted is less than or equal to 0, the input position is incorrect { cout<<"The location you entered is incorrect!\n"; return false; } i=0; p=head; while(i<pos-1&&p!=NULL) //Find the previous node of the element to be removed { p=p->next; i++; } if(p==NULL) { cout<<"The location you entered is out of range!\n"; return false; } q=p->next; p->next=q->next; delete q; return true; } void List::seek(string name)const //Find data by name { int flag=0; Node *t; t=head->next; while(t) //Circular linked list lookup { if(t->name == name) { cout<<"The member you are looking for is:\n"; cout<<t->name<<" "<<t->num; flag=1; } t=t->next; } if(flag==0) cout<<"The data you are looking for was not found!\n"; } void List::seek2(string num)const //Find content by student ID { int flag=0; Node *t; t=head->next; while(t) { if(t->num == num) { cout<<"The member you are looking for is:\n"; cout<<t->name<<" "<<t->num; flag=1; } t=t->next; } if(flag==0) cout<<"The data you are looking for was not found!\n"; } void List::seek(int pos)const //Find content by location { Node *t; t=head; int i; while(i<pos&&t!=NULL) { t=t->next; i++; } if(t==NULL) { cout<<"The number you entered is out of range!\n"; } cout<<"The member you found is:\n"; cout<<t->name<<" "<<t->num; } void List::fixed(string name) //Because some people will change their student number because of changing majors or schools { Node *t; string m_num; t=head; while(t) { if(t->name==name) { cout<<"Please enter the revised student number:"; cin>>m_num; t->num = m_num; } t=t->next; } } void List::exchange(List L) { Node *t,*s; int n; string name,num; t=head->next; s=t->next; n=L.getlen(); for(int i=0;i<n-1&&t!=NULL;i++) { for(int j=0;j<n-2-i&&s!=NULL;j++) { if(t->name < s->name) { name=t->name; num=t->num; t->name=s->name; t->num=s->num; s->name=name; s->num=num; } s=s->next; } t=t->next; } L.print(); } int List::getlen() { int i=1; Node *t; t=head->next; while(t) { t=t->next; i++; } return i; } int main() { List L; int n,t,x; char c; string name,num; cout<<"Please enter a few data to save:"; cin>>n; for(int j=1;j<=n;j++) { L.insert(j); } L.print(); cout<<"Do you want to use the delete action:\n"; cin>>c; if(c=='y'||c=='Y') { cout<<"Please enter how many times you want to delete:"; cin>>x; while(x) { cout<<"Please enter the location you want to delete:"; cin>>t; L.Delete(t); L.print(); x--; } } cout<<"Do you want to use the find operation:\n"; cin>>c; if(c=='y'||c=='Y') { cout<<"Please enter how many times you want to find:"; cin>>t; while(t) { cout<<"Please enter how you want to find content:1.name 2.position 3.student ID:"; cin>>x; if(x==1) { cout<<"Please enter the name you are looking for:"; cin>>name; L.seek(name); } else if(x==2) { cout<<"Please enter the location you are looking for:"; cin>>t; L.seek(t); } else if(x==3) { cout<<"Please enter the student number you are looking for:"; cin>>num; L.seek2(num); } else cout<<"There is a problem with the data you entered!\n"; cout<<endl; t--; } } cout<<"Do you want to modify a student's student ID number:"; cin>>c; if(c=='y'||c=='Y') { cout<<"Please enter the student IDs of several people you want to modify:"; cin>>t; while(t) { cout<<"Please enter the name of the person you want to modify:"; cin>>name; L.fixed(name); cout<<"The modified content is:\n"; L.print(); t--; } } cout<<"The contents of the sorted list are:\n"; L.exchange(L); return 0; }
The following is the operation of the above program, the picture is as follows:
The above is all about the actual operation of the class linked list.
2. Use classes to write a class array to store some famous sayings (English) and perform some comparison operations. Which needs to contain some friend functions and overloaded operator functions.
This example operation examines my understanding of the related applications of friend functions and overloaded functions, as well as static data members and static data functions. Static data members are not related to objects, and all objects only correspond to the same static member. Static functions can perform some operations on static members. If a static member is a public type, it can be referenced using a qualifier outside the class
Here is the structure of the class:
class String{ private: char * str; int len; static int num_strings; static const int CINLIM = 80; //length of limit public: String(); //default constructor String(const char *s); //Constructor String(const String &); //copy constructor ~String(); //destructor int length()const {return len;}; //function to calculate length String & operator =(const String &); //Overloading the assignment operator equals the object's String & operator =(const char *s); //The overloaded assignment operator is equal to the string pointed to by a character pointer const char & operator [](int i)const; //Overload the value operator to get the contents of a certain position in the object char & operator [](int i); friend bool operator<(const String &st,const String &st2); //overloaded object greater than object operator friend bool operator>(const String &st1,const String &st2); friend bool operator ==(const String &st,const String &st2); //Overloading the equals operator friend ostream & operator<<(ostream &os,const String &st); friend istream & operator>>(istream &is,String &st); static int HowMany(); //count how many };
The overall code is as follows:
It contains some same function names but different parameters to be called, so the class object can call different functions according to the different parameters, because the members in the class are char pointer type, so Chinese characters cannot be input. If you want to use input Chinese characters, you can use It is changed to string type, and all relevant content can be changed to string type. I hope that when you borrow this code, you can leave a comment or give a like in the comment area. It is not easy for college students to write code!
#include <iostream> #include <ctime> #include <cstring> #include <cstdlib> using namespace std; const int ArSize = 10; const int MaxLen = 80; class String{ private: char * str; int len; static int num_strings; static const int CINLIM = 80; //length of limit public: String(); String(const char *s); String(const String &); ~String(); int length()const {return len;}; String & operator =(const String &); String & operator =(const char *s); const char & operator [](int i)const; char & operator [](int i); friend bool operator<(const String &st,const String &st2); friend bool operator>(const String &st1,const String &st2); friend bool operator ==(const String &st,const String &st2); friend ostream & operator<<(ostream &os,const String &st); friend istream & operator>>(istream &is,String &st); static int HowMany(); }; int String::num_strings=0; int String::HowMany() { return num_strings; } String::String(const char *s) { len = strlen(s); str = new char [len+1]; strcpy(str,s); num_strings++; } String::String() { len = 4; str = new char [1]; str = '\0'; num_strings++; } String::String(const String &st) { num_strings++; len = st.len; str = new char [len+1]; strcpy(str,st.str); } String::~String() { --num_strings; delete [] str; } String & String::operator =(const String &st) { if(this == &st) return *this; delete [] str; len = st.len; str = new char [len+1]; strcpy(str,st.str); return *this; } String & String::operator =(const char *s) { delete [] str; len = strlen(s); str = new char [len+1]; strcpy(str,s); return *this; } char & String::operator [](int i) { return str[i]; } const char & String::operator [](int i) const { return str[i]; } bool operator>(const String &st1,const String &st2) { return (strcmp(st1.str,st2.str)<0); } bool operator<(const String &st1,const String &st2) { return (strcmp(st1.str,st2.str)>0); } bool operator==(const String &st1,const String &st2) { return (strcmp(st1.str,st2.str) == 0); } ostream & operator<<(ostream &os,const String &st) { os << st.str; return os; } istream & operator>>(istream &is,String &st) { char temp[String::CINLIM]; is.get(temp,String::CINLIM); if(is) st = temp; while(is&&is.get() !='\n') continue; return is; } int main() { String name; cout<<"Hello, who are you?\n"; cin>>name; cout<<name<<"please enter"<<ArSize<<"short sentences do not allow blank lines\n"; String sayings[ArSize]; char temp[MaxLen]; int i; for(i=0;i<ArSize;i++) { cout<<i+1<<":"; cin.get(temp,MaxLen); while(cin&&cin.get()!='\n') continue; if(!cin||temp[0]=='\0') break; else sayings[i] = temp; } int total=i; if(total>0) { cout<<"The saying you entered is:\n"; for(i=0;i<total;i++) cout<<sayings[i][0]<<":"<<sayings[i]<<endl; int shorts = 0; int first =0; for(i=1;i<total;i++) { if(sayings[i].length() < sayings[shorts].length()) shorts = i; if(sayings[i] < sayings[first]) first=i; } cout<<"The shortest saying is:"<<sayings[shorts]<<endl; cout<<"The first sentence is:"<<sayings[first]<<endl; cout<<"This program uses a total of"<<String::HowMany()<<"objects\n"; cout<<"The program ends here!\n"; } else cout<<"No data entered, the program ends!"; return 0; }
The running structure of the program is shown in the following figure:
I want to say don't feel bad about yourself, don't even respect you if you don't have self-confidence, you deserve an explanation for yourself. Needless to say what should not be explained! See what you can do and live your life.