zoukankan      html  css  js  c++  java
  • 单链表的增删查改

     1 /*
     2 寻常的函数实现
     3 并非类定义
     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){ //建表
    14     Node *p;
    15     p=a;
    16     for(int i=0;i<n;i++){
    17         Node *q=(Node *)malloc(sizeof(Node)); //给下一节点开辟空间
    18         cin>>q->value; //按顺序输入每个节点的值value
    19         q->next=NULL;
    20         p->next=q;
    21         p=q;
    22     }
    23 }
    24 bool Add(Node *a,int add_value){  //增加值为add_value的元素
    25     Node *tmp=(Node *)malloc(sizeof(Node));
    26     if(tmp==NULL) return false;  //如果开辟空间不成功返回false
    27     tmp->value=add_value;
    28     tmp->next=a->next;
    29     a->next=tmp;
    30     return true; //增加元素成功
    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_value的结点
    37         }
    38         a=a->next;
    39     }
    40     return false;//说明无法找到值为clear_value的结点,返回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;  //返回值为q_value的结点的地址
    46         }
    47         a=a->next;
    48     }
    49     return NULL; //无法找到值为q_value的结点,返回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; //说明已经将值为now_value的结点的值改为chang_value
    56         }
    57         a=a->next;
    58     }
    59     return false; //无法找到值为now_value的结点,返回false
    60 }
    61 int main(){
    62     Node *a=new Node; //创建头指针
    63     int n;
    64     cin>>n; //输入链表的长度n
    65     a->next=NULL;
    66     build_link(a,n);
    67     Node *pmax=Find(a,3); //测试查找操作
    68     if(Add(a,6)){ //测试增加操作
    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 }
  • 相关阅读:
    无限维
    黎曼流形
    why we need virtual key word
    TOJ 4119 Split Equally
    TOJ 4003 Next Permutation
    TOJ 4002 Palindrome Generator
    TOJ 2749 Absent Substrings
    TOJ 2641 Gene
    TOJ 2861 Octal Fractions
    TOJ 4394 Rebuild Road
  • 原文地址:https://www.cnblogs.com/ISGuXing/p/8939861.html
Copyright © 2011-2022 走看看