Addition, deletion, search and modification of singly linked list

1  /* 
2  The usual function implementation
 3  is not a class definition
 4  */ 
5  
6 #include<iostream>
 7 #include<bits/stdc++.h>
 8  using  namespace std;
 9  struct Node{
 10      int value;
 11      Node * next;
 12  };
 13  void build_link(Node * a, int n){ // build table 
14      Node * p;
 15      p= a;
 16      for ( int i= 0;i<n;i++ ){
 17          Node *q=(Node *) malloc ( sizeof (Node)); // Make space for the next node 
18          cin>>q->value; // Enter each node in order value value 
19          q->next= NULL;
 20          p->next= q;
 21          p= q;
 22      }
 23  }
 24  bool Add(Node *a, int add_value){   // Add the element 
25 with the value of      add_value *tmp=(Node *) malloc ( sizeof (Node));
 26      if(tmp==NULL) return  false ;   // If the space creation is unsuccessful, return false 
27      tmp->value= add_value;
 28      tmp->next=a-> next;
 29      a->next= tmp;
 30      return  true ; / / Add element successfully 
31  }
 32  bool Clear(Node *a, int clear_value){
 33      while (a->next!= NULL){
 34          if (clear_value==(a->next-> value)){
 35              a- >next=a->next-> next;
 36              return  true ;  // Clear the node whose value is clear_value 
37          }
 38          a=a-> next;
 39      }
 40      return  false ; // Indicates that the node whose value is clear_value cannot be found, and returns false 
41  }
 42 Node * Find(Node *a, int q_value){
 43      while (a->next!= NULL){
 44          if (q_value==(a->next-> value)){
 45              return a->next;   // The return value of the node whose value is q_value address 
46          }
 47          a=a-> next;
 48      }
 49      returnNULL; // Unable to find the node whose value is q_value, return false 
50  }
 51  bool change(Node *a, int now_value, int change_value){
 52      while (a->next!= NULL){
 53          if (now_value== (a->next-> value)){
 54              a->next->value= change_value;
 55              return  true ; // Indicate that the value of the node whose value is now_value has been changed to chang_value 
56          }
 57          a=a-> next;
 58      }
 59      return  false ; //Unable to find a node whose value is now_value, return false 
60  }
 61  int main(){
 62      Node *a= new Node; // Create a head pointer 
63      int n;
 64      cin>>n; // Enter the length of the linked list n 
65      a->next= NULL;
 66      build_link(a,n);
 67      Node *pmax=Find(a, 3 ); // Test find operation 
68      if (Add(a, 6 )){ // Test add operation 
69          cout << ' a ' << '  ' <<a->next->value<<endl;
70     }else{
71         cout<<"a  false"<<endl;
72     }
73     if(Clear(a,4)){ //测试删除操作
74         Node *tmp=a;
75         while(tmp->next!=NULL){
76             cout<<tmp->next->value<<' ';
77             tmp=tmp->next;
78         }
79         cout<<endl;
80     }else{
81         cout<<"b false"<<endl;
82     }
83     cout<<pmax->value<<endl;
84     if(change(a,5,7)){ //测试修改操作
85         Node *tmp=Find(a,7);
86         cout<<tmp->value<<endl;
87         tmp=a;
88         while(tmp->next!=NULL){
89             cout<<tmp->next->value<<' ';
90             tmp=tmp->next;
91         }
92     }else{
93         cout<<"change false"<<endl;
94     }
95     return 0;
96 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324817254&siteId=291194637