(1) write the Make_list2 () function to establish a linked list, so that when the linked list is established, the input data will be placed at the end of the linked list with the new input number corresponding to the node. If the input is 3 5 2 9 4 7 0, the established list is:
#include <iostream>using namespace std;struct node{ int data; Node data struct node *next; Point to next node}; Node *head=null; Define the list header as a global variable, so that you can manipulate void make_list () later; Establish a linked list void out_list (); Output list int main () { make_list (); Out_list (); return 0;} void Make_list () { int n; Node *p,*q; cout<< "Enter several positive numbers (ending with 0 or a negative number) to create a linked list:"; cin>>n; while (n>0) //Enter several positive numbers to create a linked list, enter a non-positive number when the build process ends { p=new Node; New node p->data=n;. p->next=null; if (head==null) head=p; Place the node of the first entered number at the end of the list else q->next=p; q=p; cin>>n; Enter next number, prepare to create next node } return; void Out_list () { Node *p=head; cout<< "The data in the list is:" <<endl; while (P!=null) { cout<<p->data<< ""; p=p->next; } cout<<endl; return;}
Operation Result:
Summary of Knowledge points:
while (n>0)//Enter several positive numbers to create a linked list, enter a non-positive number when the build process ends
{
P=new Node; New node
p->data=n;
p->next=null;
if (head==null)
Head=p; Place the node with the first entered number at the end of the list.
Else
q->next=p;
Q=p;
cin>>n; Enter the next
Learning experience:
Good study Day Day up
Academic leave Project 1-Dynamic linked list experience 1