第五周作业总结(内含用Junit测试ArrayStack和LinkedStack课堂练习报告)

---恢复内容开始---

# 学号 20162310《程序设计与数据结构》第五周学习总结

教材学习内容总结

  • 集合分为线性集合(集合中的元素排成一行)和非线性集合(按不同于一行的方式来组织元素,例如按层次或是按网络的方式)。
  • 集合中元素之间的组织方式通常由两个因素决定,一、它们加入集合的次序。二、元素之间的固有关系。
  • 抽象数据类型(ADT)忽略许多的细节为的是方便控制复杂的系统。
  • 栈集合以LIFO的方式处理元素
  • 泛型是本章的重点,有及表示某类某方法某变量为泛型,但有些时候用具体的类来代替,从而实例化
  • 使用栈来实现计算后缀表达式
  • 使用数组或链表来实现栈是本章的练习重点

教材学习中的问题和解决过程

  • 问题1:对于使用链表来实现栈还有很多疑问
  • 问题1解决方案:相比于数组的简单结构,链表多了一个使用LinearNode结点类,来指向表中下一个LinearNode的引用,和指向该结点中保存的元素的引用。每个结点保存一个泛型类型,当实例化结点时才确定其类型。LinearNode不仅仅在栈中实现,在下一章的队列中也有使用到LinearNode

代码调试中的问题和解决过程

  • 问题1:在课上练习的用数组实现栈和用链表实现栈没有完成,以下是对这两个项目的详细分析

用数组实现栈,题目:实现ArrayStack,完成peek,isEmpty,size和toString方法,并用Junit测试

  public T pop() throws EmptyCollectionException {
    if (this.isEmpty()) {
        throw new EmptyCollectionException("No elements available");
    } else {
        T result = this.stack[this.count]; // 设置泛型变量result用来存储pop出来的元素;
        this.stack[this.count] = null; // 将原位置的数组位清除元素,设为null;
        return result; // 返回pop出来的元素
    }
}
public T peek() throws EmptyCollectionException {
    if (this.isEmpty()) {
        throw new EmptyCollectionException("No elements available");
    } else {
        return this.stack[this.count - 1];   // 弹出元素但不返回
    }
}
public boolean isEmpty() {
    return this.count == 0;  //另count=0,即空
}
public int size() {
    return this.count; //count记录长度,返回count即为长度
}
public String toString() {
    String result = "<top of stack>\n"; // 注释栈顶

    for(int index = this.count - 1; index >= 0; index--) {  //用for循环打印栈中元素
        result = result + this.stack[index] + "\n";
    }
    return result + "<bottom of stack>";  // 注释栈底
}
  • 用Junit测试ArrayStack

用链表实现栈,完成LinkedStack,给出size,isEmpty和toString方法的定义,并用Junit测试

 public T pop() throws EmptyCollectionException {
    if (this.isEmpty()) {
        throw new EmptyCollectionException("Pop operattion failed.The stack is empty."); //pop操作的时候若链表为空报出异常
    } else {
        T result = this.top.getElement();  
        this.top = this.top.getNext(); 
        this.count--;  
        return result;
    }
}


public void push(T element) {
    LinearNode<T> temp = new LinearNode(element);
    temp.setNext(this.top);
    this.top = temp;
    this.count++;
}

public T peek() throws EmptyCollectionException {
    if (this.isEmpty()) {
        throw new EmptyCollectionException("Pop operattion failed.The stack is empty.");
    } else {
        return this.top.getElement();
    }
}

public boolean isEmpty() {
    return this.count == 0;
}

public int size() {
    return this.count;
}

public String toString() {
    String result = "";

    for(LinearNode current = this.top; current != null; current = current.getNext()) {
        result = result + current.getElement().toString() + "\n";
    }
    return result;
}


    - 结点类LinearNode
public class LinearNode<T> {
private LinearNode<T> next = null;
private T element;

public LinearNode() {
    this.element = null;
}

public LinearNode(T elem) {
    this.element = elem;
}

public LinearNode<T> getNext() {
    return this.next;
}

public void setNext(LinearNode<T> node) {
    this.next = node;
}

public T getElement() {
    return this.element;
}


public void setElement(T elem) {
    this.element = elem;
}
}
  • 用Junit测试LinkedStack

上周考试错题总结

本周结对学习情况

- [20162314](博客链接)
- 结对照片
- 结对学习内容

学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 5000行 30篇 400小时
第一周 200/200 1/1 20/20
第二周 200/200 1/1 20/20
第三周 200/200 1/1 22/22
第四周 1000/1000 1/1 30/30
第五周 1000/1000 1/1 22/22
posted @ 2017-10-15 22:56  林臻  阅读(402)  评论(0编辑  收藏  举报