zoukankan      html  css  js  c++  java
  • 单向链表类

    #pragma once
    #include "stdafx.h"
    
    template<class T>
    class CLinkList
    {
    public:
        typedef struct LNode{
            T data;
            struct LNode * next;
        }LNode;//单向链表
        LNode * list;//头指针
        CLinkList()
        {
            length=0;
            list=new LNode;
            list->next=NULL;//头指针data不赋值
        }
    
    
    
        void Add(T data)
        {
            length++;       //元素增加
            LNode * now=list;
            while(now->next) now=now->next;
            LNode * tmp=new LNode;
            tmp->next=NULL;
            tmp->data=data;
            now->next=tmp;
        }
    
        void Delete(int index)
        {
            if(index<0) return;//防止下溢
            int i=0;
            LNode * now=list;
            while(now->next && i<index) //防止上溢
            {
                now=now->next;
                i++;
            }
            if(now->next)//防止上溢
            {
                length--;     //元素减少
                LNode * del=now->next;
                now->next=del->next;
                delete del;
            }
        }
    
        void Insert(int index,T data)
        {
            if(index<0) return;//防止下溢
            int i=0;
            LNode * now=list;
            while(now->next && i<index) //防止上溢
            {
                now=now->next;
                i++;
            }
            if(now->next)//防止上溢
            {
                length++;    //元素增加
                LNode * tmp=new LNode;
                tmp->next=now->next;
                tmp->data=data;
                now->next=tmp;
            }
        }
    
        T GetAt(int index)
        {
            if(index<0) return list->next->data;//防止下溢
            int i=0;
            LNode * now=list;
            while(now->next && i<index) //防止上溢
            {
                now=now->next;
                i++;
            }
            return now->next->data;
        }
    
        void SetAt(int index,T data)
        {
            if(index<0) return;//防止下溢
            int i=0;
            LNode * now=list;
            while(now->next && i<index) //防止上溢
            {
                now=now->next;
                i++;
            }
            now->next->data=data;
        }
    
    /*
        T * GetRange(int a,int b)
        {
            
        }*/
    
        T operator [] (int index)
        {
            return GetAt(index);
        }
    
        int GetLen(){return length;}
    
        void display()
        {
            LNode * now=list->next;
            while(now)
            {
                printf("%d,",now->data);
                now=now->next;
            }
            printf("
    ");
        }
    
    
    
    protected:
    
    private:
        int length;
    };
  • 相关阅读:
    PCB设计实战项目指导班26层板的设计
    AT2171 [AGC007D] Shik and Game 题解
    UVA11327 Enumerating Rational Numbers 题解
    P6222 「P6156 简单题」加强版 题解
    CF702F TShirts 题解
    P3747 [六省联考 2017] 相逢是问候 题解
    『学习笔记』PollardRho 算法 题解
    P7453 [THUSCH2017] 大魔法师 题解
    CF1575L Longest Array Deconstruction 题解
    最大公约数之和
  • 原文地址:https://www.cnblogs.com/TQCAI/p/7606783.html
Copyright © 2011-2022 走看看