链表的基本操作(手写、STL)

1 插入 

2 删除

3 输出

手写版本:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
using namespace std;
//带头节点版 头插法
struct ListNode
{
    int val;
    ListNode* next;
};
void Insert(ListNode* head, const int &pos, const int &num){
    int cnt = 1;
    ListNode* p = head->next;
    while(p->next!=nullptr && cnt<pos){
        p = p->next;
        cnt++;
    }
    ListNode* tmp = (ListNode*)malloc(sizeof(ListNode));
    tmp->val = num;
    tmp->next = p->next;
    p->next = tmp;

}
void Display(ListNode* head){
    ListNode* p = head;
    if(p->next == nullptr){
        cout<<"list is empty!!!"<<endl;
        return;
    }
    p = p->next;
    while(p->next){
        cout<<p->val<<" ";
        p = p->next;
    }
    cout<<p->val<<endl;
}
void Delete(ListNode* head, int &pos){
    int cnt = 1;
    ListNode* p = head->next;
    while(p->next!=nullptr && cnt<pos){
        p = p->next;
        cnt++;
    }
    p->next = p->next==nullptr ? nullptr:p->next->next;

}
//选择排序
void cSort(ListNode* head){
    if(!head) return;
    ListNode* h = head;
    ListNode* m;
    for(ListNode* i = h->next->next; i->next; i = i->next){
        m = i;
        ListNode* j;
        for(j = i->next; j; j = j->next){

            if(m->val > j->val) m = j;
        }
        int tmp;
        if(m->val!=i->val){
            tmp = i->val;
            i->val = m->val;
            m->val = tmp;
        }
    }
}
//逆转链表
void rever(ListNode* head){
    ListNode* h = head;
    ListNode* first = head->next;
    ListNode* go = h->next->next;
    while(go->next){
         ListNode* tmp = go->next;
         go->next = go->next->next;
         tmp->next = first->next;
         first->next = tmp;
    }
}
int main()
{
    ListNode* head = (ListNode*)malloc(sizeof(ListNode));
    ListNode* first = (ListNode*)malloc(sizeof(ListNode));
    head->next = first;
    first->next = nullptr;
    int n, op;//操作次数,操作类型
    cin>>n;
    while(n--)
    {
        cin>>op;
        switch(op)
        {
           case 1 :
               int num, pos;
               cin>>pos>>num;
               Insert(head, pos, num);//如果想顺次插入,传入较大的pos即可
               break;
           case 2 :
               cin>>pos;
               Delete(head, pos);
               break;
           case 3 :
               Display(head->next);
               break;
           default :
               cout<<"no such operation!!!"<<endl;
               break;
        }

    }
    return 0;
}

STL版本:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<list>
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
using namespace std;

int main()
{
    list<int>l;
    int n, op;//操作次数,操作类型
    cin>>n;
    list<int>::iterator it;
    while(n--)
    {
        cin>>op;
        switch(op)
        {
           case 1 :
               int num, pos;
               cin>>pos>>num;
               it = l.begin();
               while(--pos) it++;
               l.insert(it, num);
               break;
           case 2 :
               cin>>pos;
               it = l.begin();
               while(--pos) it++;
               l.erase(it);
               break;
           case 3 :
               for(auto i:l) cout<<i<<" ";
               cout<<endl;
               break;
           default :
               cout<<"no such operation!!!"<<endl;
               break;
        }

    }
    return 0;
}
/****************stl::list的其他用法***************/
//头部添加元素:l.push_front(const T& x);
//末尾添加元素:l.push_back(const T& x);
//任意位置插入 n 个相同元素:l.insert(iterator it, int n, const T& x);
//删除 [first,last] 之间的元素:l.erase(iterator first, iterator last);
//合并两个列表的元素(默认升序排列):lst.merge(lst1);
//在任意位置拼接入另一个list:lst.splice(iterator it, list&);
//删除容器中相邻的重复元素:lst.unique();
//删除所有大于10的数l.remove_if([](int n){ return n > 10; });
/****************注意事项***********/
//list 为双向迭代器,故不支持it+=i;
//list 不支持下标访问和at方法访问。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值