• 单链表


    链表

    用不连续的、或连续的存储单元存储线性表元素

    每个数据元素后,加上一个地址域,其地址为其直接后继的地址;数据元素和地址域组成结点。

     头指针:指向链表的第一个结点,是第一个节点的地址,或链表在存储器中的首地址

    头指针的类型与其他节点指针域的指针类型一致,都是指向同一类型的结点

    单链表由头指针唯一确定。

    typedef int elementType;
    struct slNode
    {
        elementType data;//数据域
        struct slNode* next;//指针域
            //对自身的引用    
    }node;
    /*以下两种方式也都是同样的把node定义为slNode类型
    typedef struct slNode node;
    typedef slNode node;*/
    class SingleLinkedList
    {
    public:
        SingleLinkedList();//初始化链表
        int length();                    //求链表长度
        bool getElement(int i,elementType& x);//按序号返回元素
        node* loccate(elementType x);//查找元素 返回目标指针
        bool listinsert(int i,elementType x);
        bool listdelete(int i);
        bool InclistJudge();//判断是否是递增链表
        void incListInsert(elementType x);//递增链表插入元素
        void createListR();//尾插法创建单链表
        void createListH();//头插法创建单链表
        void print();//打印单链表元素
        void destory();//销毁所有node结点 否则内存泄露
        
        ~SingleLinkedList();
    private:
        node* head;
        //定义头指针  初始化
    
        
    };
    SingleLinkedList::SingleLinkedList()
    {
        head = new node;   //为head请求内存
        head->next = NULL; //置为空表
    }
    int SingleLinkedList::length(){
        node* p = head->next;
        int len =0;
        
        while (p)://最后一个元素的值为null
            len++;
            p = p->next;//每次指向下一个元素  
        return len;
    }
    bool SingleLinkedList::getElement(int i, elementType& x)
    {
        node* p = head->next;
        int j = 1;
        while(p!=NULL && j!=i){
            j++;
            p = p->next;
        }
        if (p==NULL){
            return false;
        }
        x = p->data;
        return true;
    }
    node* SingleLinkedList::loccate(elementType x){
        node*p = head->next;
        while(p){
            if (p->data == x)
                return p;
            else
                p = p->next;
        }    
        return NULL;
    }
    
    bool SingleLinkedList::listinsert(int i,elementType x)
    {
        node*p = head;
        node*s
        int j = 0
        while (p && j!=i-1){
            p =p->next;    //先利用j去找到位置
            j++;
        }
        if(p==NULL){
            return false;
        }
        else{
            s = new node;
            s->next = p->next;
            p->next = s;
            s->data = x;
            return true;
    
        }
    }
    bool SingleLinkedList::listdelete(int i){
        int j = 0;
        node* p = head;
        while(p && j != i-1)
        {
            p = p->next;
            j++;
        }
        if (p == NULL)
            return false;
        else{
            node* d = p->next;
            p->next = d->next;
            delete d;
            return true;
        }
    }
    bool SingleLinkedList::InclistJudge(){
        node *p,*h;
        p = head;
        if (p==NULL)
            return true;
        else{
            h = p->next;
            if (h->data < p->data)
                return false;
            p = h;
    
        }
        return true;
    }
    void SingleLinkedList::createListR(){
        elementType x;
        node *R,*p;
        if(head == NULL){
            head = new node;
            head->next = NULL;
        }
        R = head;
        cout << "尾插法 输入(-100)退出"<<endl;
        cin >>x;
        while(x != -100){
            p = new node;
            p = R->next;
            p->data = x;
            cin >> x;
            R = p;
        }
        R->next = NULL;
        R = NULL;
        p = NULL;//最后将两个指针都放空
    
    }
    void SingleLinkedList::createListH(){
        if(head == NULL){
            head = new node;
            head ->next = NULL;
        }
        elementType x;
        node *p,*R;
        cout<<"头插法创建单链表 输入(-100)退出"<<endl;
        cin >> x;
        while(x != -100){
            R = new node;
            head -> next = R->next;
            R->data = x;
            head ->next = R;
            cin >> x;
        }
    }
    void SingleLinkedList::print(){
        node* p;
        p = head;
        while(p){
            cout<<p->data<<"	";
            p = p->next;
        }
        cout << endl;
    }
    void SingleLinkedList::destory(){
        node *p,*u;
        p =head;
        while(p){
            u = p->next;
            delete p;
            p = u;
        }
        head = NULL;
    }
    void SingleLinkedList::incListInsert(elementType x){
        node *p,*q;
        p=head;
        while(p->next != NULL && p->next->data<x){
            p = p->next;
        }
        q = new node;
        q->data = x;
        q->next = p->next;
        p->next = q;
    }
  • 相关阅读:
    [luoguP1098] 字符串的展开(模拟)
    [luoguP1033] 自由落体(模拟?)
    [luoguP1011] 车站(递推)
    [luoguP1097] 统计数字(水)
    [luoguP2672] 推销员(贪心 + 树状数组 + 优先队列)
    [luoguP1043] 数字游戏(DP)
    [luoguP1058] 立体图(超级大模拟(¬︿̫̿¬☆))
    [luoguP1021] 邮票面值设计(DFS + dp)
    POJ 2184 Cow Exhibition (带负值的01背包)
    POJ 2392 Space Elevator (DP)
  • 原文地址:https://www.cnblogs.com/suizhixxie/p/10462803.html
Copyright © 2020-2023  润新知