Simple analysis of adding deleting modifying and subtracting single linked list in C++

  • 2020-04-02 01:26:13
  • OfStack

The first is a simple example of the creation and output of a single linked list.
Program 1.1

#include<iostream>
#include<string>
using namespace std;
struct Student{
 string name;
 string score;
 Student *next;//Defines a pointer to a variable of type Candidate
};
int main(){
 int n;//
 cout<<" Please enter the total number of students: ";
 cin>>n;
 int i=1;
 Student *p=NULL;
 Student *node=NULL;
 Student *head=NULL;
 //Build list
 for(;i<=n;i++){
  node=new Student;
  cout<<" Please enter the first "<<i<<" Student names :";
  cin>>node->name;
  cout<<" Please enter the first "<<i<<" Students' grades :";
  cin>>node->score;
  if(head==NULL)
   head=node;
  else
   p->next=node;
  p=node;
  if(i==n){
   p->next=NULL;
  }
 }
 //The output list
 p=head;
 cout<<" The linked list has been created !"<<endl;
 cout<<"n========== So let's enter that data =============n"<<endl;
 i=1;
 while(p!=NULL){
  cout<<" The first "<<i<<" A classmate ==="<<p->name<<"== results ===="<<p->score<<endl;
  p=p->next;
  i++;
 }
 //Destruction of the list
 Student *d;
 p=head;
 while(p!=NULL){
  d=p;
  p=p->next;
  delete d;
 }
 return 0;
}

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309020851293.jpg ">

In program 1.1, we have created a linked list. Then we inserted a score from sasai between sakura and naruto

#include<iostream>
#include<string>
using namespace std;
struct Student{
 string name;
 string score;
 Student *next;//Defines a pointer to a variable of type Candidate
};
Student * Create(Student * head){
 Student *p=NULL;
 Student *node=NULL;
 int n;//
 cout<<" Please enter the total number of students: ";
 cin>>n;
 for(int i=1;i<=n;i++){
  node=new Student;
  cout<<" Please enter the first "<<i<<" Student names :";
  cin>>node->name;
  cout<<" Please enter the first "<<i<<" Students' grades :";
  cin>>node->score;
  if(head==NULL)
   head=node;
  else
   p->next=node;
  p=node;
  if(i==n){
   p->next=NULL;
  }
 }
 return head;
}
void Print(Student * head){
 Student *p=NULL;
 p=head;
 cout<<" The linked list has been created !"<<endl;
 cout<<"n========== So let's enter that data =============n"<<endl;
 int i=1;
 while(p!=NULL){
  cout<<" The first "<<i<<" A classmate ==="<<p->name<<"== results ===="<<p->score<<endl;
  p=p->next;
  i++;
 }
 cout<<"n"<<endl;
}
void Insert(Student * head,int k){
 Student *p=NULL;
 Student *node=NULL;
 p=head;
 int i=1;
 while(p!=NULL){
  if(i+1==k){
   node=new Student;
   cout<<" The first "<<k<<" Student's name: ";
   cin>>node->name;
   cout<<" The first "<<k<<" Students' achievements: ";
   cin>>node->score;
   node->next=p->next;
   p->next=node;
  }
  p=p->next;
  i++;
 }
}

void Destory(Student * head){
    Student *d;
 Student *p=NULL;
 p=head;
 while(p!=NULL){
  d=p;
  p=p->next;
  delete d;
 }
}
int main(){
 Student *head=NULL;
 //Create a list
 head=Create(head);
 //The output list
 Print(head);
 //Insert data
 int k;
 cout<<" Please enter the serial number of the student you want to insert: ";
 cin>>k;
 Insert(head,k);
 //The output list
 Print(head);
 //Destruction of the list
    Destory(head);
 return 0;
}

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309020851294.jpg ">

Now, sasai's grade has been inserted.
However, kakashi teacher found that naruto's grades were copied wrong, in fact, is 100, need to modify the grades; Then sasuke dropped out, so he had to delete his grades.

#include<iostream>
#include<string>
using namespace std;
struct Student{
 string name;
 string score;
 Student *next;//Defines a pointer to a variable of type Candidate
};
Student * Create(Student * head){
 Student *p=NULL;
 Student *node=NULL;
 int n;//
 cout<<" Please enter the total number of students: ";
 cin>>n;
 for(int i=1;i<=n;i++){
  node=new Student;
  cout<<" Please enter the first "<<i<<" Student names :";
  cin>>node->name;
  cout<<" Please enter the first "<<i<<" Students' grades :";
  cin>>node->score;
  if(head==NULL)
   head=node;
  else
   p->next=node;
  p=node;
  if(i==n){
   p->next=NULL;
  }
 }
 return head;
}
void Print(Student * head){
 Student *p=NULL;
 p=head;
 cout<<" The linked list has been created !"<<endl;
 cout<<"n========== So let's enter that data =============n"<<endl;
 int i=1;
 while(p!=NULL){
  cout<<" The first "<<i<<" A classmate ==="<<p->name<<"== results ===="<<p->score<<endl;
  p=p->next;
  i++;
 }
 cout<<"n"<<endl;
}
void Insert(Student * head,int k){
 Student *p=NULL;
 Student *node=NULL;
 p=head;
 if(k==1){
   node=new Student;
   cout<<" The first 1 Student's name: ";
   cin>>node->name;
   cout<<" The first 1 Students' achievements: ";
   cin>>node->score;
   node->next=head->next;
   head=node;
 }
 int i=1;
 while(p!=NULL){
  if(i+1==k){
   node=new Student;
   cout<<" The first "<<k<<" Student's name: ";
   cin>>node->name;
   cout<<" The first "<<k<<" Students' achievements: ";
   cin>>node->score;
   node->next=p->next;
   p->next=node;
  }
  p=p->next;
  i++;
 }
}

void Destory(Student * head){
    Student *d;
 Student *p=NULL;
 p=head;
 while(p!=NULL){
  d=p;
  p=p->next;
  delete d;
 }
}
void Alter(Student * head,int k){
 int i=1;
 Student *p=head;
 while(p!=NULL){
  if(i==k){
   cout<<" The first "<<k<<" Student's name: ";
   cin>>p->name;
   cout<<" The first "<<k<<" Students' achievements: ";
   cin>>p->score;
  }
  p=p->next;
  i++;
 }
}
Student * Delete(Student * head,int k){
 int i=1;
 Student *p=head;
 Student *d=head;
 if(k==1){
  head=head->next;
 }else{
  while(p!=NULL){
   if(i+1==k){
    p->next=p->next->next;
   }
   p=p->next;
   i++;
  }
 }
 return head;
}
int main(){
 Student *head=NULL;
 //Create a list
 head=Create(head);
 //The output list
 Print(head);
 //Insert data
 int k;
 cout<<" Please enter the serial number of the student you want to insert: ";
 cin>>k;
 Insert(head,k);
 //The output list
 Print(head);
 //Modify the list
 cout<<" Please enter the serial number of the student you want to modify :";
 cin>>k;
 Alter(head,k);
 //The output list
 Print(head);
 //Delete one of the entries
 cout<<" Please enter the serial number of the student you want to delete :";
 cin>>k;
 head=Delete(head,k); 
 //The output list
 Print(head);
 //Destruction of the list
    Destory(head);
 return 0;
}

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/20130812152348734.png ">

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/20130812152400265.png ">

Related articles: