The first is a simple example of the establishment and output of a single linked list.
Program 1.1
Copy Code code as follows:
#include <iostream>
#include <string>
using namespace Std;
struct student{
String name;
String score;
Student *next;//defines a pointer to a candidate type variable
};
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;
Set up a linked list
for (; i<=n;i++) {
Node=new Student;
cout<< "Please enter the first" <<i<< "classmate's name:";
cin>>node->name;
cout<< "Please enter the first" <<i<< "Student's score:";
cin>>node->score;
if (head==null)
Head=node;
Else
p->next=node;
P=node;
if (i==n) {
p->next=null;
}
}
Output linked list
P=head;
cout<< "Linked list has been established!" <<endl;
cout<< "\n========== Just enter the data below =============\n" <<endl;
I=1;
while (P!=null) {
cout<< "First" <<i<< "classmate = = =" <<p->name<< "= = Grade = =" <<p->score<<endl;
p=p->next;
i++;
}
Destroy a linked list
Student *d;
P=head;
while (P!=null) {
D=p;
p=p->next;
Delete D;
}
return 0;
}
In program 1.1, we have established a linked list. Then we inserted a Zokiai student's score between Sakura and Naruto.
Copy Code code as follows:
#include <iostream>
#include <string>
using namespace Std;
struct student{
String name;
String score;
Student *next;//defines a pointer to a candidate type variable
};
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<< "classmate's name:";
cin>>node->name;
cout<< "Please enter the first" <<i<< "Student's score:";
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<< "Linked list has been established!" <<endl;
cout<< "\n========== Just enter the data below =============\n" <<endl;
int i=1;
while (P!=null) {
cout<< "First" <<i<< "classmate = = =" <<p->name<< "= = Grade = =" <<p->score<<endl;
p=p->next;
i++;
}
cout<< "\" <<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<< "classmate's name:";
cin>>node->name;
"cout<<" <<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 linked list
Head=create (head);
Output linked list
Print (head);
Inserting data
int k;
cout<< "Please enter the serial number of the classmate you want to insert:";
cin>>k;
Insert (HEAD,K);
Output linked list
Print (head);
Destroy a linked list
Destory (head);
return 0;
}
Now, the results of Zokiai students have been inserted.
However, Kakashi teacher found that Naruto's score is wrong, is actually 100, need to modify the results; Then, help the classmate drop out of school, so, also want to delete his results.
Copy Code code as follows:
#include <iostream>
#include <string>
using namespace Std;
struct student{
String name;
String score;
Student *next;//defines a pointer to a candidate type variable
};
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<< "classmate's name:";
cin>>node->name;
cout<< "Please enter the first" <<i<< "Student's score:";
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<< "Linked list has been established!" <<endl;
cout<< "\n========== Just enter the data below =============\n" <<endl;
int i=1;
while (P!=null) {
cout<< "First" <<i<< "classmate = = =" <<p->name<< "= = Grade = =" <<p->score<<endl;
p=p->next;
i++;
}
cout<< "\" <<endl;
}
void Insert (Student * head,int k) {
Student *p=null;
Student *node=null;
P=head;
if (k==1) {
Node=new Student;
cout<< "1th Classmate's name:";
cin>>node->name;
cout<< "1th Classmate's score:";
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<< "classmate's name:";
cin>>node->name;
"cout<<" <<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<< "classmate's name:";
cin>>p->name;
"cout<<" <<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 linked list
Head=create (head);
Output linked list
Print (head);
Inserting data
int k;
cout<< "Please enter the serial number of the classmate you want to insert:";
cin>>k;
Insert (HEAD,K);
Output linked list
Print (head);
Modify a linked list
cout<< "Please input the serial number of the classmate you want to revise:";
cin>>k;
Alter (HEAD,K);
Output linked list
Print (head);
Delete one of the items
cout<< "Please enter the serial number of the classmate you want to delete:";
cin>>k;
Head=delete (HEAD,K);
Output linked list
Print (head);
Destroy a linked list
Destory (head);
return 0;
}