Academic leave Project 1-Dynamic linked list experience 2

Source: Internet
Author: User



(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

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.