链表

单链表:


#include<iostream>
using namespace std;
typedef int T;

class List
{
    //内部的自定义成员,内部成员
    struct Node{
        T data;
        Node* next;
        Node(const T& d=T()):data(d),next(NULL){};//T() 匿名变量 ,0初始化
    };
    Node* head;//头指针,用来保存头节点的地址  list对象只保存这这个,其他节点在动态内存中开辟
    int len;
public:
    List():head(NULL),len(0){};
    void push_front(const T& d)
    {
        /*Node* p = new Node(d);
        p->next = head;
        head = p;*/
        insert(d,0);
    }
    void push_back(const T& d)
    {
        insert(d,size());
    }
    int size()const//j计算链表节点个数
    {
        return len;
    }

    Node*& getptr(int pos)//找链表中指定位置的指针,引用原始指针过去,不是复制
    {
        if(pos<0||pos>size()) pos=0;
        if(pos==0)  return head;
        Node* p =head;
        for(int i=1; i<pos; i++)
        {
            p = p->next;
        }
        return (*p).next;
    }

    void insert(const T& d,int pos)//在任意位置出插入
    {
        //1.在链表里找到指向那个位置的指针,起别名pn
        //2.让新节点的next跟找到的指针指向同一个地方
        //3.让找到的指针指向新节点
        //位置0 是head
        Node*& pn = getptr(pos);//引用,保证pn是原始指针
        Node* p =new Node(d);
        p->next = pn;
        pn = p;
        len++;
    }

    void travel() const//遍历
    {
        Node* p = head;
        while(p)
        {
            cout<< p->data<<' ';
            p = p->next;
        }
        cout << endl;
    }

    void clear()//清空链表
    {
        while(head){
            Node* p = head->next;
            delete head;
            head = p;
        }
        len = 0;
    }

    ~List()
    {
        clear();
    }
    void erase(int pos)//有效位置为0~size()-1 0是头节点
    {
        if(pos<0||pos>=size())
        {
            cout<<"error:位置不存在"<<endl;
            return;
        }
        Node*& pn = getptr(pos);
        Node* p = pn;
        pn = pn->next;
        delete p;
        len--;
    }

    void remove(const T& d)//删除d
    {
        int pos;
        while((pos = find(d))!=-1)
            erase(pos);
    }

    int find(const T& d)const//找到d的位置
    {
        int pos = 0;
        Node* p = head;
        while(p){
            if(p->data==d) return pos;
            p = p->next;
            pos++;
        }
        return -1;
    }

    void set(int pos,const T& d)
    {
        if(pos<0||pos>=size()) return;
        getptr(pos)->data=d;
    }

    bool empty()const{return head==NULL;}

    T front()const{if(empty()) throw "空";return head->data;}

    T back()const{
        if(empty()) throw "空";
        Node* p=head;
        while(p->next!=NULL)
            p = p->next;
        return p->data;
    }
};

int main()
{
    List l;
    l.push_front(5);
    l.push_front(8);
    l.push_front(20);
    l.insert(7,2);
    l.insert(7,4);
    l.insert(6,-100);
    l.push_back(100);
    l.travel();
    l.remove(7);
    l.travel();
    l.set(0,100);
    l.travel();
    cout<<l.size();
    cout<<l.front()<<"   "<<l.back()<<endl;
    //cout<<sizeof(l);//4  只是头的大小,指针:4字节
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值