动态链表常用函数

· · 算法·理论

函数库:

#include<bits/stdc++.h>
using namespace std;

// 改为模板结构体,支持任意类型
template <typename T>
struct node{
    T value; 
    node *next;
};

// 所有函数改为模板函数,参数/逻辑完全不变
template <typename T>
unsigned listlen(node<T> *h){
    node<T> *p;
    p=h->next;
    unsigned len=0;
    while(p!=NULL){
    len++;
    p=p->next;}
    return len;
}

template <typename T>
T listmax(node<T> *h){
    node<T> *p;
    p=h->next;
    T maxx=p->value;
    while(p!=NULL){
    if(p->value>maxx) maxx=p->value;
    p=p->next;}
    return maxx;
}

template <typename T>
T listmin(node<T> *h){
    node<T> *p;
    p=h->next;
    T minn=p->value;
    while(p!=NULL){
    if(p->value<minn) minn=p->value;
    p=p->next;}
    return minn;
}

template <typename T>
T listsum(node<T> *h){
    node<T> *p;
    p=h->next;
    T sum=0;
    while(p!=NULL){
    sum+=p->value;
    p=p->next;}
    return sum;
}

template <typename T>
void insert(node<T> *head,int i,T x){ 
    node<T> *p,*s;
    s=new node<T>;// 模板节点new
    s->value=x;
    p=head;
    for(int j=0;j<i;j++)
        p=p->next;
    s->next=p->next;
    p->next=s;
}

template <typename T>
void del(node<T> *head,int x=-1){
    node<T> *p,*p1;
    p=head;
    if(x==-1) {
    head->next=NULL;
    return;}
    p=p1=head;
    for(int j=0;j<x+1;j++)
        p1=p1->next;
    for(int j=0;j<x-1;j++)
        p=p->next;
    p->next=p1; 
}

template <typename T>
void listfree(node<T> *head){
    delete head;
}

template <typename T>
T pop(node<T> *head,int x){
    node<T> *p,*p1,*p2;
    p=head;
    p=p1=p2=head;
    for(int j=0;j<x+1;j++)
        p1=p1->next;
    for(int j=0;j<x-1;j++)
        p=p->next;
    for(int j=0;j<x;j++)
        p2=p2->next;
    T s=p2->value; 
    p->next=p1;return s; 
}

template <typename T>
void input(node<T> *head){
    node<T> *p;
    p=head->next;
    while(p!=NULL){
    cin>>p->value;
    p=p->next;
    }
}

template <typename T>
void print(node<T> *head){
  node<T> *p;
    p=head->next;
    while(p!=NULL){
    cout<<p->value<<' ';
    p=p->next;}
}

template <typename T>
int index(node<T> *head,T i){
    node<T> *p;int j=1;
    p=head->next;

    while(p!=NULL){
        if(p->value==i) return j;
        p=p->next;
    j++;
    }
    return 0;
}

template <typename T>
unsigned listcount(node<T> *head){
    node<T> *p;int j=0;
    p=head->next;
    while(p!=NULL){
        j++;
        p=p->next;
    }
    return j;
}

template <typename T>
T getl(node<T> *head,int i){
    node<T> *p;
    p=head;
    for(int j=0;j<i;j++)
        p=p->next;
    return p->value;
}

template <typename T>
void inputn(node<T> *head,int x){
    node<T> *p;
    p=head->next;
    for(int i=0;i<x;i++){
    cin>>p->value;
    p=p->next;}
}

template <typename T>
void printn(node<T> *head,int x){
    node<T> *p;
    p=head->next;
    for(int i=0;i<x;i++){
    cout<<p->value<<' ';
    p=p->next;}
}

template <typename T>
T sumif(node<T> *h,bool a){
    node<T> *p;
    p=h->next;
    T sum=0;
    while(p!=NULL){
    if(a) sum+=p->value;
    p=p->next;}
    return sum;
}

template <typename T>
T maxif(node<T> *h,int a){
    node<T> *p;
    p=h->next;
    T maxx=p->value;
    while(p!=NULL){
    if(p->value>maxx&&a) maxx=p->value;
    p=p->next;}
    return maxx;
}

template <typename T>
T minif(node<T> *h,int a){
    node<T> *p;
    p=h->next;
    T minn=p->value;
    while(p!=NULL){
    if(p->value<minn&&a) minn=p->value;
    p=p->next;}
    return minn;
}

template <typename T>
node<T> *creat(int n){
    node<T> *h,*p,*r;
    h=new node<T>; // 模板节点new
    r=h;
    for(int i=0;i<n;i++){
        p=new node<T>; // 模板节点new
        p->value=0;
        p->next=NULL;
        r->next=p;
        r=p;
    }
    return h;
}

template <typename T>
void change(node<T> *h,int a,T b){
    node<T> *p;
    p=h->next;
    for(int i=0;i<a-1;i++){
        p=p->next;
    } 
    p->value=b;
}

template <typename T>
void clear(node<T> *h,int b){
    node<T> *p;
    p=h->next;
    for(int i=0;i<b-1;i++)
        p=p->next;
    p->next=NULL;
}

template <typename T>
T *tuple(node<T> *h){
  node<T> *p;
  T *a=new T[listlen(h)]; // 模板数组new
  p=h->next;
  int i=0;
  while(p!=NULL){
    a[i]=p->value;
    p=p->next;
    i++;
}
return a;}
template <typename T>
T& operator[](node<T> *h,int n){
    node<T> *p;
    p=h;
    for(int j=0;j<i;j++)
        p=p->next;
    return p->value;
} 

int main(){
    node<int>*a=creat<int>(10);
    input(a);
    print(a);
    return 0;
}
int main(){

}