(2) write the function void search (int x), and whether there is a node with a value of x in the output list.
#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; int x,flag=0; cout<< "Please enter a value to look for:"; cin>>x; cout<< "The data in the list is:" <<endl; while (P!=null) {if (p->data==x) flag++; cout<<p->data<< ""; p=p->next; } cout<<endl; if (flag!=0) cout<< "has a value in the list" <<x<< "Knot" <<endl; Else cout<< "There are no nodes with a value of" <<x<< "in the list <<endl; return;}
Operation Result:
Summary of Knowledge points:
Insert code to choose a good location
Learning experience:
Good study Day Day up
Academic leave Project 1-Dynamic linked list experience 2