zoukankan      html  css  js  c++  java
  • define a class for a linked list and write a method to delete the nth node.

    1、问题

    define a class for a linked list and write a method to delete the nth node.


    2、算法

    template <typename C>

    struct Node{

            C content ;

            Node<C>* next ;

    }


    template <typename T>

    class List{

    private:

            Node<T>* head ;

           unsigned int size ;

    public:

            List()

            {

                    size = 0 ;

                    head = NULL ;

            }


            Node<T>* getHead() { return head ; } ;


            bool insert(const T& data)

            {

                    Node<T>* node = new Node<T> ;

                    node->content = data ;

                    node->next = head;


                    head = node ;

                    size++ ;


                    return true ;

            }


            bool insert(const T&data, int pos)

            {

                    if( pos > size )

                    {

                            return false ;

                    }else if( pos == 0 )

                    {

                            return insert(data) ;

                    }


                    Node<T>* node = new Node<T> ;

                    node->content = data ;

                    node->next = NULL ;


                    unsigned int idx = 1 ;

                    Node<T>* frontNode = head ;

                    while(idx < pos)

                    {

                            idx++ ;

                            frontNode = frontNode->next ;

                    }


                    node->next = frontNode->next ;

                    frontNode ->next = node ;

                    size++ ;


                    return true ;

            }


            bool remove( int pos )

            {

                    if( pos > size )

                    {

                            return false ;

                    }


                    unsigned int idx = 0 ;

                    Node<T>* frontNode = NULL ;

                    Node<T>* node = head ;


                    while(idx < pos)

                    {

                            idx++ ;

                            frontNode = node ;

                            node = node->next ;

                    }

                    if( frontNode  )

                    {

                            frontNode->next = node->next ;

                    }else{

                            head = head->next ;

                    }

                    delete node ;

                    size-- ;


                    return true ;

            }


    }

  • 相关阅读:
    使用Python验证常见的50个正则表达式
    空气开关为何叫空气开关?它跟空气有何关系?
    YOLO 算法最全综述:从 YOLOv1 到 YOLOv5
    深入理解部分 SQL 语句执行慢的原因
    KNN(k-nearest-neighbor)算法
    聚类分析方法
    SQL Database学习笔记
    statistic学习笔记
    MapReduce中的排序
    weka打开提示内存不足的解决方法
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4590530.html
Copyright © 2011-2022 走看看