Addition, deletion, modification, and reduction of a single-chain table (C ++)

Source: Internet
Author: User

The first is a simple example: The creation and output of a single-chain table.

Program 1.1

# Include <iostream> # include <string> using namespace std; struct Student {string name; string score; Student * next; // defines the pointer to the Candidate type variable }; int main () {int n; // cout <"Enter the total number of students:"; cin> n; int I = 1; Student * p = NULL; student * node = NULL; Student * head = NULL; // create a chain table for (; I <= n; I ++) {node = new Student; cout <"Enter the" <I <"name of the Student:"; cin> node-> name; cout <"Enter the" <I <"score:"; cin> node-> score; if (head = NULL) head = node; elsep-> next = n Ode; p = node; if (I = n) {p-> next = NULL ;}// output linked list p = head; cout <"linked list already created! "<Endl; cout <"\ n ========= enter the data you just entered =======================\ n" <endl; I = 1; while (p! = NULL) {cout <"no" <I <"Student =" <p-> name <"= score =" <p-> <p-> score <endl; p = p-> next; I ++;} // destroy the linked table Student * d; p = head; while (p! = NULL) {d = p; p = p-> next; delete d;} return 0 ;}

In program 1.1, we have created a linked list. Then, we inserted the score of a satay student between Sakura and Naruto.

 

# Include <iostream> # include <string> using namespace std; struct Student {string name; string score; Student * next; // defines the pointer to the Candidate type variable }; student * Create (Student * head) {Student * p = NULL; Student * node = NULL; int n; // cout <"Enter the total number of students :"; cin> n; for (int I = 1; I <= n; I ++) {node = new Student; cout <"Enter the" <I <"name of the Student:"; cin> node-> name; cout <"Enter the" <I <"score:"; cin> node-> score; if (head = NULL) head = node; elsep-> next = node; P = node; if (I = n) {p-> next = NULL;} return head;} void Print (Student * head) {Student * p = NULL; p = head; cout <"linked list already created! "<Endl; cout <"\ n ========= enter the data you just entered =======================\ n" <endl; int I = 1; while (p! = NULL) {cout <"no" <I <"Student =" <p-> name <"= score =" <p-> <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 <"th" <k <"Name of a Student :"; cin> node-> name; cout <"Number" <k <"score:"; 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 chain table head = Create (head); // output chain table Print (head); // insert data int k; cout <"Enter the serial number of the student you want to Insert:"; cin> k; Insert (head, k); // output Print (head ); // destroy the chain table Destory (head); return 0 ;}

At present, the score of saswell students has been inserted.

However, Mr. kakaxi found that the score of the Naruto was incorrect. In fact, the score was 100, and the score needs to be modified. Then, sasu dropped out of school, so he had to delete the score.

 

# Include <iostream> # include <string> using namespace std; struct Student {string name; string score; Student * next; // defines the pointer to the Candidate type variable }; student * Create (Student * head) {Student * p = NULL; Student * node = NULL; int n; // cout <"Enter the total number of students :"; cin> n; for (int I = 1; I <= n; I ++) {node = new Student; cout <"Enter the" <I <"name of the Student:"; cin> node-> name; cout <"Enter the" <I <"score:"; cin> node-> score; if (head = NULL) head = node; elsep-> next = node; P = node; if (I = n) {p-> next = NULL;} return head;} void Print (Student * head) {Student * p = NULL; p = head; cout <"linked list already created! "<Endl; cout <"\ n ========= enter the data you just entered =======================\ n" <endl; int I = 1; while (p! = NULL) {cout <"no" <I <"Student =" <p-> name <"= score =" <p-> <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 <"Name of the 1st-bit Student: "; cin> node-> name; cout <" 1st-bit score: "; cin> node-> score; node-> next = head-> next; head = node;} int I = 1; while (p! = NULL) {if (I + 1 = k) {node = new Student; cout <"th" <k <"Name of a Student :"; cin> node-> name; cout <"Number" <k <"score:"; 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 <"th" <k <"name of a classmate:"; cin> p-> name; cout <"Number" <k <"score:"; 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;} p = p-> next; I ++ ;}} return head;} int main () {Student * head = NULL; // Create a linked list head = Create (head ); // Print (head) of the output linked list; // insert int k; cout <"Enter the serial number of the person you want to insert:"; cin> k; insert (head, k); // Print (head) of the output linked list; // modify the cout of the linked list <"Enter the serial number of the student you want to modify :"; cin> k; Alter (head, k); // Print (head) of the output linked list ); // Delete one of the items cout <"Enter the serial number of the student you want to Delete:"; cin> k; head = Delete (head, k ); // Print (head); // destroy (head); return 0 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.