zoukankan      html  css  js  c++  java
  • sicily 1000. LinkedList

    Description

     template <typename E>

    class LinkedList
    {
    private:
     
      // inner class: linked-list node
      class Node
      {
      public:
        E data;
        Node * next;
      };
     
      Node * first;
     
    public:
      LinkedList() {
        first = 0;
      }
     
      ~LinkedList() {
        while (first != 0) {
          removeFirst();
        }
      }
     
      E getFirst() {
        return first->data;
      }
     
      bool isEmpty() {
        return first == 0;
      }
     
    // 实现下列4个函数:
      LinkedList(const LinkedList & that);
      LinkedList & operator= (const LinkedList & that);
      void removeFirst() ;
      void addFirst(E data);
    };

    Hint

     链表的插入使用头插法,只需提交模板类函数的实现即可,不需要提交main函数,如下列代码所示:

    template <typename E>
    void LinkedList<E>::removeFirst()
    {
        Node * node = first;
        first = node->next;
        delete node;
    }
     
    代码如下:
    template <typename E>
    LinkedList<E>::LinkedList(const LinkedList & that) {
        Node* current = 0;
        Node* node = that.first;
        while (node != 0) {
            if (current == 0) current= first = new Node();
            else {
                current->next = new Node();
                current = current->next;
            }
            current->data = node->data;
            current->next = 0;
            node = node->next;
        }
    }
    
    template <typename E>
    LinkedList<E>& LinkedList<E>::operator= (const LinkedList & that) {
        LinkedList<E> tmp(that);
        while (first != 0) removeFirst();
        Node* current = 0;
        Node* node = tmp.first;
        while (node != 0) {
            if (current == 0) current= first = new Node();
            else {
                current->next = new Node();
                current = current->next;
            }
            current->data = node->data;
            current->next = 0;
            node = node->next;
        }
        return *this;
    }
    
    template <typename E>
    void LinkedList<E>::removeFirst()
    {
        Node * node = first;
        first = node->next;
        delete node;
    }
    
    template <typename E>
    void LinkedList<E>::addFirst(E data) {
        Node* newFirst = new Node();
        newFirst->data = data;
        newFirst->next = first;
        first = newFirst;
    }

    测试代码:

    template <typename E>
    class LinkedList
    {
    private:
    
      // inner class: linked-list node
      class Node
      {
      public:
        E data;
        Node * next;
      };
    
      Node * first;
    
    public:
      LinkedList() {
        first = 0;
      }
    
      ~LinkedList() {
        while (first != 0) {
          removeFirst();
        }
      }
    
      E getFirst() {
        return first->data;
      }
    
      bool isEmpty() {
        return first == 0;
      }
    
    // TODO:
      LinkedList(const LinkedList & that);
      LinkedList & operator= (const LinkedList & that);
      void removeFirst() ;
      void addFirst(E data);
    };
    
    
    /*template <typename E>
    LinkedList<E>::LinkedList(const LinkedList<E> & that) 
    {
        
    }
    
    template <typename E>
    LinkedList<E> & LinkedList<E>::operator= (const LinkedList<E> & that) 
    {
        
    }
    
    template <typename E>
    void LinkedList<E>::removeFirst() {
        Node * node = first;
        first = node->next;
        delete node;
    }
    
    template <typename E>
    void LinkedList<E>::addFirst(E data)
    {
        
    }
    
    
    */
    
    //#include "source.cpp"
    
    #include <iostream>
    using namespace std;
    
    LinkedList<double> read() {
      LinkedList<double> list;
      for (int i = 0; i < 10; ++ i) {
        double value;
        cin >> value;
        list.addFirst(value);
      }
      return list;
    }
    
    void removeAndPrintAll(LinkedList<double> list) {
      while (! list.isEmpty()) {
        cout << list.getFirst() << endl;
        list.removeFirst();
      }
    }
    
    int main() {
      LinkedList<double> list = read();
      LinkedList<double> list2;
      list2 = list;
      removeAndPrintAll(list2);
    }
  • 相关阅读:
    pthread_once函数的简单示例
    pthread_join直接决定资源是否能够及时释放
    非分离线程未使用join函数例子:
    一个HTTP打趴80%面试者
    BM和KMP字符串匹配算法学习
    STL 几个容器的底层实现
    指针的引用(*&)与指针的指针(**)
    Maven 环境变量设置
    配置JAVA的环境变量
    Maven报错 解决方案。ERROR: No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id
  • 原文地址:https://www.cnblogs.com/zmj97/p/6262064.html
Copyright © 2011-2022 走看看