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 = ;
Node* node = that.first;
while (node != ) {
if (current == ) current= first = new Node();
else {
current->next = new Node();
current = current->next;
}
current->data = node->data;
current->next = ;
node = node->next;
}
} template <typename E>
LinkedList<E>& LinkedList<E>::operator= (const LinkedList & that) {
LinkedList<E> tmp(that);
while (first != ) removeFirst();
Node* current = ;
Node* node = tmp.first;
while (node != ) {
if (current == ) current= first = new Node();
else {
current->next = new Node();
current = current->next;
}
current->data = node->data;
current->next = ;
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 = ;
} ~LinkedList() {
while (first != ) {
removeFirst();
}
} E getFirst() {
return first->data;
} bool isEmpty() {
return first == ;
} // 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 = ; i < ; ++ 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);
}

sicily 1000. LinkedList的更多相关文章

  1. leetcode@ [273] Integer to English Words (String & Math)

    https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...

  2. java中三种for循环之间的对比

    普通for循环语法: for (int i = 0; i < integers.length; i++) { System.out.println(intergers[i]); } foreac ...

  3. LinkedList竟然比ArrayList慢了1000多倍?(动图+性能评测)

    数组和链表是程序中常用的两种数据结构,也是面试中常考的面试题之一.然而对于很多人来说,只是模糊的记得二者的区别,可能还记得不一定对,并且每次到了面试的时候,都得把这些的概念拿出来背一遍才行,未免有些麻 ...

  4. To Java程序员:切勿用普通for循环遍历LinkedList

    ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: p ...

  5. ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...

  6. arraylist与linkedlist的区别与性能测试

    /** *arraylist和linkedlist的适用场合. **/ import java.util.List; import java.util.ArrayList; import java.u ...

  7. Java-链表LinkedList源码原理分析,并且通过LinkedList构建队列

    在这里我们介绍一下最简单的链表LinkedList: 看一下add()方法: public boolean add(E e) { linkLast(e); return true; } void li ...

  8. 你真的说的清楚ArrayList和LinkedList的区别吗

    参见java面试的程序员,十有八九会遇到ArrayList和LinkedList的区别?相信很多看到这个问题的人,都能回答个一二.但是,真正搞清楚的话,还得花费一番功夫. 下面我从4个方面来谈谈这个问 ...

  9. Java基础-ArrayList和LinkedList的区别

    大致区别:  1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为Lin ...

随机推荐

  1. [HTML5] Inlining images with SVG and data URIs

    The main reason you want to do I"nlining images with SVG and data URIs" is to reduce http ...

  2. Microsoft Updateclient更新

     大家好, 微软Microsoft Update产品组官方博客于昨天宣布了有关最新的Windows Updateclient更新的消息.依据这则博客.微软从当日開始逐渐向全部Windows 7, ...

  3. EditText电话号码格式化输入、删除案例

    我们在输入电话号码的时候,一般都会切割一个较长的电话号码.这种话效果会好非常多..对EditText的监听能够轻松的实现这个需求.仅仅须要我们给相应的EditText加一个监听就OK了..贴一下我写的 ...

  4. FPGA视频拼接器的放大和缩小功能

    视频视频器能够把信号源放大和缩小. 对于我们的拼接器而言,它的架构这种: 信号源进入到拼接器中.先进入缩小模块.然后存进DDR中.然后从DDR中读出视频.进入到放大模块,最后依据屏幕的位置,输出到屏幕 ...

  5. windows上通过vnc连接虚拟机中linux系统

    首先要在虚拟机中安装vnc. 虚拟机的设置中要启用VNC连接. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHdzc2c=/font/5a6L5L2T/ ...

  6. bzoj1816: [Cqoi2010]扑克牌(二分答案判断)

    1816: [Cqoi2010]扑克牌 题目:传送门 题解: 被一道毒瘤题搞残了...弃了坑来刷刷水题 一开始还想复杂了...结果发现二分水过: 二分答案...然后check一下,joker肯定尽量用 ...

  7. ServiceStack.Redis之IRedisClient<第三篇>【转】

    事实上,IRedisClient里面的很多方法,其实就是Redis的命令名.只要对Redis的命令熟悉一点就能够非常快速地理解和掌握这些方法,趁着现在对Redis不是特别了解,我也对着命令来了解一下这 ...

  8. PowerDesigner 16.5 安装及破解步骤

     安装: 1.双击运行PowerDesigner16.5_Evaluation.exe,进入安装界面,点击(Next)下一步按钮: 2.下拉菜单选择HongKong,选中 I agree to the ...

  9. RabbitMq笔记(2)

    今天收获不少,记个笔记. namespace RabbitMQTest { class Program { static void Main(string[] args) { Consumer(); ...

  10. Python3基础笔记---线程与进程

    参考博客:Py西游攻关之多线程(threading模块) 一.并发与并行的区别 并发:交替做不同事的能力并行:同时做不同事的能力 行话解释:并发:不同代码块交替执行的性能并行:不同代码块同时执行的性能 ...