#include <iostream>using namespace std;/* object Chain list (new/delete) */#define LEN sizeof (STU)//defines the data type of the Unidirectional Link table node class stu{ Public:char name[20];int age;int sex;stu * Next;}; void createlist (int, stu *); void Printlist (Stu *); void Delete (Stu *);//The main function simply defines a head node pointer to int main (void) {int n;puts ("Please enter Number of Students N: "); Cin>>n;stu * Head;head = NULL; CreateList (n, &head);//address transfer, head is an address variable, to modify the value of the head itself must pass the address of the head, if the value stored in the head variable <null>//is passed, The purpose of modifying the head's own value cannot be achieved. Printlist (head);//The pointer address value is passed only a copy of the value of its storage space and cannot change its value. Delete (head);//Destroy the list in the heap, new create the linked list return 0;} Create a linked list and implement data input void createlist (int n, stu * * p) {int i = 1;stu * St1;stu * st2;st1 = new stu;* p = st1;while (i<=n)//loop for connecting Continue to generate new objects in the heap and link them together. {st2 = st1;printf ("Please enter the information for%d students at the time of name, age and gender (gender male 1, female 0): \ n", i);cin>>st2->name>>st2->age> >st2->sex;st1 = new Stu;st2->next = st1;i++;} St2->next = Null;st1 = null;//The unused pointer is empty ST2 = NULL;} Output list contents void Printlist (Stu * head) {stu * p1;stu * P2;char * sex;p1 = HEAD;P2 = p1;cout<< "\ n YouThe student information entered is as follows: \ n "; while (P2! = NULL) {if (P2->sex = = 0) Sex =" female "; elsesex =" male ";cout<<p2->name<< ' \ t ' << p2->age<< ' \ t ' <<sex;p1 = P2;P2 = P1->next;puts ("");//puts () output string will terminator the string to enter the output, so puts (""); The purpose of the output line break is}p1 = P2 = null;//Good habit, the unused pointer is empty. sex = NULL;} The function ends when the free request for all heap space void Delete (STU * p) {stu * p1;stu * p2;p1 = P;P2 = P1;while (p2->next! = NULL) {p1 = P2->next;delet e p2;p2 = p1;} P2 = P1 = NULL;} * * Please enter the number of students N:5 Please enter the 1th student's information, the second is the name, age and gender (gender male 1, female 0): Eason Chan 23 1 Please enter the information for the 2nd student, the second is the name, age and gender (gender male 1, female 0 is indicated): Jion 22 1 Please enter the 3rd student's information, the second is the name, age and gender (gender male 1, female 0): yonges 15 0 Please enter the information for the 4th student, at the time of the name, age and gender (gender 1, female 0): Winsoe 16 0 Please enter the information for the 5th student at the time of the name , age and gender (gender male 1, female 0): Susan 14 0 The student information you entered is as follows: Eason Chan 23 male jion 22 male yonges 15 female Winsoe 16 female Susan 14 Female Press any key to continue*/
Creation and destruction of C + + object linked list