/*Automatically complete the list of links when creating a linked list the simplest way to sort by a keyword is to create a linked list by inserting nodes in the form of a linked list */C + + code*/#include<iostream>using namespacestd;structNode//node Structure{intdata; Node*Next; }; intMain () {node* CreateList ();//Create a list function declaration voidPrintlist (node *);//node information function declaration in output listNode * INSERTNODE (node *,node *);//Inserting a node function declaration in a linked listNode * DELETEALL (node *);//Delete entire linked list intLinklength (node * head);//get the number of nodes in a linked listNode * head=NULL; cout<<"To start creating the linked list, enter the node data, and when the input data is 0 o'clock, set up the linked list ."<<Endl; /*Create a linked list*/Head=createlist ();//call the linked list to create a function that returns the head pointer of the linked list /*traversing the output chain list*/Printlist (head); //Call the list traversal function to output all the node information . /*Delete entire linked list*/cout<<"\ n The number of nodes on the list is:"<<linklength (head) <<Endl; Head=DeleteAll (head); cout<<"after deleting the entire list, the number of nodes on the linked list is:"<<linklength (head) <<Endl; return 0;} Node* CreateList ()//set up a list function declaration and create a linked list by inserting nodes .{node * INSERTNODE (node *,node *);//Inserting a node function declaration in a linked listNode *head=NULL; Node*s; S=Newnode; CIN>>s->data; S->next=NULL; while(s->data!=0) {Head=Insertnode (head,s); S=Newnode; CIN>>s->data; S->next=NULL; } returnHead;} Node* Insertnode (node *head,node * s)//A function that inserts a node, head is a chain-head pointer, and s points to a new node to be inserted{Node *p,*P; P=head;//make P point to the first node in the list if(Head==null)//The original linked list is an empty table{head=s;//The new node that the head points to as the head node .s->next=NULL; } Else //The original linked list is not an empty table{ while((S->data>p->data) && (p->next!=null))//Position the node position p to insert with a loop, so that s is inserted before P{q=p;//Q records The current p, that is, Q points to the previous node of Pp=p->next;//p move back one node. } if(S->data<=p->data)//the node data to be inserted is smaller than the last node data .{if(head==p)//determines whether to insert the first node in the linked list before{head=s;//before inserting it into the original first node.s->next=p; } Else //after inserting the node that the Q points to, p points before the node{q->next=s; S->next=p; } } Else //the node data to be inserted is larger than the last node data .{p->next=s;//after inserting the last node of the list, as the tail node of the linked list .s->next=NULL; }} cout<<"successful completion of a new node insertion ..."<<Endl; return(head); } voidPrintlist (node * head)//node information functions in the output list, linked list traversal{Node *p=Head; intI=1; cout<<endl<<"traverse linked list ..."<<Endl; if(Head!=null)//If the list is not empty, that is, there are nodes in the linked list . Do //The loop outputs the junction data until it moves to the end of the chain, the last node.{cout<<"Section"<<i++<<"node data is:"<<p->data<<Endl; P=p->Next; } while(p!=NULL); Else{cout<<"linked list is empty list!"<<Endl; }} node* DELETEALL (node *head)//Delete entire linked list{ if(head!=NULL) {Node*p,*Q; P=Head; Q=Head; Do{p=p->Next; Head=p; DeleteQ; Q=p; } while(p!=NULL); Head=NULL; } returnHead;} intLinklength (node * head)//get the number of nodes in a linked list { intn=0; Node*p; if(head==NULL)return 0; Else{p=Head; while(p!=NULL) {N++; P=p->Next; } returnN; } }
Auto-complete node data sorting while creating linked lists