zoukankan      html  css  js  c++  java
  • 单链表——递归求最大整数、节点个数、平均值

    在原本单链表上进行增加

    //"LinkList.h"
    #include<iostream>
    using namespace std;
    
    #define ElemType int
    
    typedef struct LNode{
        ElemType data;
        struct LNode *next;
    }LNode,*LinkList;
    
    string InitList(LinkList &L){
        L = new LNode;
        L->next = NULL;
        return "OK";
    }
    
    string ListInsert(LinkList &L,int i,ElemType e){
        LinkList P = L;
        int j=0;
        while (P && (j<i-1))
        {       
            P = P->next;++j;    
        }
        LinkList S = new LNode;
        S->data = e;
        S->next=P->next;
        P->next=S;
        return "OK";   
    }
    
    LNode* GetElement(LinkList L,int i){
        LinkList p = L->next;
        int j = 1;
        while(p && j<i){
            p=p->next;
            j++;
        }
        // if(!p||j>i){
        //     return -1;
        // }
        return p;
    }
    
    string DeleteList(LinkList &L,int i){
        LinkList p = L;
        int j = 0;
        while ((p->next)&&(j<i-1))
        {
            p=p->next;
            j++;
        }
        if(!(p->next)||(j>i-1))
            return "ERROR";
        LinkList q = p->next;
        p->next=q->next;
        delete q;
        return "OK";
        
    }
    
    string ShowList(LinkList L){
        LinkList p= L;
        while(p->next){
            p=p->next;
            cout<<p->data;
        }
        return "OK";
    }
    
    string FillList(LinkList &L){
        
        int p =1;
        char a = '1';
        while (a != '!')
        {
            ElemType b ;
            cin >> b;
            ElemType e = (ElemType)b - 48;
            a = b;
            if(a != '!')
                cout << ListInsert(L,p,b)<<endl;
            p++;
        }
     return "OK";
    }
        
    string FillListWithNum(LinkList &L,int i){
        
        int e;
        
        for(int j =1;j<=i;j++){
            cin >> e;
            ListInsert(L,j,e);
        }
     return "OK";
    }
    
    void max(LinkList L,ElemType &e){        //最大值
        if(!L) return;
        if(L->data > e) e = L->data;
        max(L->next,e);
    }
    
    void count(LinkList L,ElemType &n){        //个数
        if(!L) return;
        n++;
        count(L->next,n);    
    }
    
    void all(LinkList L,ElemType &al){      //总和
        if(!L) return;
        cout << L->data<<" ";
        al = al + L->data;
        all(L->next,al);    
    }
    
    
    int main(){
    //     ShowList(L);
        LinkList L;
        InitList(L);FillListWithNum(L,8);
        ElemType e =0;
        max(L->next,e);
        cout << "the max is:"<<e<<endl;
        int num = 0;
        count(L->next,num);
        cout << "the number is: "<<num<<endl;
        int all1 = 0;
        all(L->next,all1);
        cout << "all is: " <<all1<<endl;
        cout << "the average is: " << all1/num <<endl;
    
    
    
    // DeleteList(L,2);
    // ShowList(L);
        system("pause");
        return 0;
    }
  • 相关阅读:
    bzoj3530 [SDOI2014]数数
    bzoj3940 Censoring
    线性代数基础
    hdu1085 Holding Bin-Laden Captive!
    hdu1028 Ignatius and the Princess III
    luogu2000 拯救世界
    博弈论入门
    树hash
    luogu2173 [ZJOI2012]网络
    luogu1501 [国家集训队]Tree II
  • 原文地址:https://www.cnblogs.com/LuMinghao/p/14012131.html
Copyright © 2011-2022 走看看