已实现功能:

1.链表首部插入元素
2.从链表中删除指定元素:
(1)删除第一次出现的位置
(2)删除最后一次出现的位置
(3)删除所有出现的位置
3.返回链表中元素个数
4.链表比较大小
5.链表首尾连接
6.翻转链表
7.链表从小到大排序

代码:

#include<bits/stdc++.h>
using namespace std;
struct node{
    int data;
    node *next;
};
void list_add(node *head,int _data){//头插入元素_data
    node *temp=new node;
    temp->data=_data;
    temp->next=head->next;
    head->next=temp;
}
int list_size(node *head){
    int ret=0;
    node *now=head;
    while(now->next!=NULL){
        ret++;
        now=now->next;
    }
    return ret;
}
void list_out(node *head){
    node *now=head;
    printf("[]");
    while(now->next!=NULL){
        now=now->next;
        printf(" -> ");
        printf("%d",now->data);
    }
    printf("\n");
}
bool list_del(node *head,int data,int op){
    if(op==1){//删除第一个为data的元素
        node *now=head, *pre=head;
        bool first=0;
        while(now->next!=NULL){
            if(first==0) first=1;
            else pre=pre->next;
            now=now->next;
            if(now->data==data){
                pre->next=now->next;
                return 1;
            }
        }
        return 0;
    }
    if(op==2){//删除最后一个为data的元素
        node *now=head, *pre=head;
        bool flag=0;
        while(now->next!=NULL){
            now=now->next;
            if(now->data==data){
                flag=1;
                while(pre->next!=now){
                    pre=pre->next;
                }
            }
        }
        if(flag==1){
            pre->next=(pre->next)->next;
            return 1;
        }
        else return 0;
    }
    if(op==3){//删除所有为data的元素
        node *now=head, *pre=head;
        bool flag=0,first=0;
        while(now->next!=NULL){
            if(first==0) first=1;
            else pre=pre->next;
            now=now->next;
            if(now->data==data){
                pre->next=now->next;
                flag=1;
            }
        }
        return flag;
    }
}
bool list_cmp(node *head1,node *head2){
    node *now1=head1, *now2=head2;
    while(now1->next!=NULL && now2->next!=NULL){
        now1=now1->next;
        now2=now2->next;
        if(now1->data!=now2->data){
            return now1->data < now2->data;
        }
    }
    if(now1->next==NULL)return 1;
    else return 0;
}
node *list_connect(node *head1, node *head2){//链表连接
    node *now=head1;
    while(now->next != NULL){
        now=now->next;
    }
    now->next=head2->next;
    return head1;
}
node *list_reversal(node *head){//将链表翻转
    if(head->next==NULL)return head;
    node *temp=new node, *now=head->next, *pre=NULL;
    while(now->next!= NULL){
        temp=now->next;
        now->next=pre;
        pre=now;
        now=temp;
    }
    now->next=pre;
    node *ret=new node;
    ret->next=now;
    return ret;
}
node *list_sort(node *head){//归并排序
    if(head->next==NULL || head->next->next==NULL){
        return head;
    }
    node *mid=head, *tail=head;
    while(tail->next!=NULL&& tail->next->next!= NULL){
        mid=mid->next;
        tail=tail->next->next;
    }
    node *right=new node;
    right->next=mid->next;
    mid->next=NULL;
    node *l=list_sort(head);//left
    node *r=list_sort(right);//right
    node *ret=new node, *now=ret;
    l=l->next;
    r=r->next;
    ret->next=NULL;
    while(l!=NULL&& r!=NULL){
        if(l->data < r->data){
            now->next=l;
            now=now->next;
            l=l->next;
        }
        else {
            now->next=r;
            now=now->next;
            r=r->next;
        }
    }
    while(l!=NULL){
        now->next=l;
        now=now->next;
        l=l->next;
    }
    while(r!=NULL){
        now->next=r;
        now=now->next;
        r=r->next;
    }
    return ret;
}
int main(){
    node *x=new node;
    x->next=NULL;
    while(1){
        int op,s;
        scanf("%d",&op);
        if(op==0){//链表首部插入元素
            scanf("%d",&s);
            list_add(x,s);
        }
        if(op==1){//输出链表大小和链表内容
            printf("size=%d ",list_size(x));
            list_out(x);
        }
        if(op==2){//删除链表中元素
            scanf("%d",&s);
            int kind=0;
            printf("选择删除类型:1.删除第一个%d\n2.删除最后一个%d\n3.删除所有%d\n",s,s,s);
            scanf("%d",&kind);
            int ans=list_del(x,s,kind);
            if(ans==0){
                printf("error\n");
            }
            else {
                printf("successful\n");
            }
        }
        if(op==3){//翻转链表
            x=list_reversal(x);
        }
        if(op==4){//链表排序
            x=list_sort(x);
        }
    }
    return 0;
}