2017.7.24-2017.7.30

本周任务:

1.lecture10、11、12、13

2.lab4&hw4

3.pj1尽量

7.24

lecture10完成

lab4打卡:

DList1:

/* DList1.java */

/**
 *  A DList1 is a mutable doubly-linked list.  (No sentinel, not
 *  circularly linked.)
 */

public class DList1 {

  /**
   *  head references the first node.
   *  tail references the last node.
   *
   *  DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
   */

  protected DListNode1 head;
  protected DListNode1 tail;
  protected long size;

  /* DList1 invariants:
   *  1)  head.prev == null.
   *  2)  tail.next == null.
   *  3)  For any DListNode1 x in a DList, if x.next == y and x.next != null,
   *      then y.prev == x.
   *  4)  For any DListNode1 x in a DList, if x.prev == y and x.prev != null,
   *      then y.next == x.
   *  5)  The tail can be accessed from the head by a sequence of "next"
   *      references.
   *  6)  size is the number of DListNode1s that can be accessed from the
   *      head by a sequence of "next" references.
   */

  /**
   *  DList1() constructor for an empty DList1.
   */
  public DList1() {
    head = null;
    tail = null;
    size = 0;
  }

  /**
   *  DList1() constructor for a one-node DList1.
   */
  public DList1(int a) {
    head = new DListNode1();
    tail = head;
    head.item = a;
    size = 1;
  }

  /**
   *  DList1() constructor for a two-node DList1.
   */
  public DList1(int a, int b) {
    head = new DListNode1();
    head.item = a;
    tail = new DListNode1();
    tail.item = b;
    head.next = tail;
    tail.prev = head;
    size = 2;
  }

  /**
   *  insertFront() inserts an item at the front of a DList1.
   */
  public void insertFront(int i) {
      DListNode1 NewNode=new DListNode1(i);
      NewNode.next=head;
      head=NewNode;
      size++;
      if(size==1) {
          tail=head;
          tail.prev=null;
      }else if(size==2) {
          tail.prev=head;
      }
    // Your solution here.
  }

  /**
   *  removeFront() removes the first item (and node) from a DList1.  If the
   *  list is empty, do nothing.
   */
  public void removeFront() {
    // Your solution here.
      if(size!=0) {
          if(size==1) {
              head=null;
              tail=null;
          }else if(size==2) {
              head.prev=null;
              head.next=null;
              tail.next=null;
              tail.prev=null;
              head=tail;
          }
          size--;
      }

  }

  /**
   *  toString() returns a String representation of this DList.
   *
   *  DO NOT CHANGE THIS METHOD.
   *
   *  @return a String representation of this DList.
   */
  public String toString() {
    String result = "[  ";
    DListNode1 current = head;
    while (current != null) {
      result = result + current.item + "  ";
      current = current.next;
    }
    return result + "]";
  }

  public static void main(String[] args) {
    // DO NOT CHANGE THE FOLLOWING CODE.

    DList1 l = new DList1();
    System.out.println("### TESTING insertFront ###\nEmpty list is " + l);

    l.insertFront(9);
    System.out.println("\nInserting 9 at front.\nList with 9 is " + l);
    if (l.head == null) {
      System.out.println("head is null.");
    } else {
      if (l.head.item != 9) {
        System.out.println("head.item is wrong.");
      }
      if (l.head.prev != null) {
        System.out.println("head.prev is wrong.");
      }
    }
    if (l.tail == null) {
      System.out.println("tail is null.");
    } else {
      if (l.tail.item != 9) {
        System.out.println("tail.item is wrong.");
      }
      if (l.tail.next != null) {
        System.out.println("tail.next is wrong.");
      }
    }
    if (l.size != 1) {
      System.out.println("size is wrong.");
    }

    l.insertFront(8);
    System.out.println("\nInserting 8 at front.\nList with 8 and 9 is " + l);
    if (l.head == null) {
      System.out.println("head is null.");
    } else {
      if (l.head.item != 8) {
        System.out.println("head.item is wrong.");
      }
      if (l.head.prev != null) {
        System.out.println("head.prev is wrong.");
      }
      if (l.head.next != l.tail) {
        System.out.println("head.next is wrong.");
      }
    }
    if (l.tail == null) {
      System.out.println("tail is null.");
    } else {
      if (l.tail.item != 9) {
        System.out.println("tail.item is wrong.");
      }
      if (l.tail.next != null) {
        System.out.println("tail.next is wrong.");
      }
      if (l.tail.prev != l.head) {
        System.out.println("tail.prev is wrong.");
      }
    }
    if (l.size != 2) {
      System.out.println("size is wrong.");
    }

    l = new DList1(1, 2);
    System.out.println("\n\n### TESTING removeFront ###\nList with 1 and 2 is "
                       + l);

    l.removeFront();
    System.out.println("\nRemoving front node.\nList with 2 is " + l);
    if (l.head.item != 2) {
      System.out.println("head.item is wrong.");
    }
    if (l.head.prev != null) {
      System.out.println("head.prev is wrong.");
    }
    if (l.tail.item != 2) {
      System.out.println("tail.item is wrong.");
    }
    if (l.tail.next != null) {
      System.out.println("tail.next is wrong.");
    }
    if (l.size != 1) {
      System.out.println("size is wrong.");
    }

    l.removeFront();
    System.out.println("\nRemoving front node.\nEmpty list is " + l);
    if (l.head != null) {
      System.out.println("head is wrong.");
    }
    if (l.tail != null) {
      System.out.println("tail is wrong.");
    }
    if (l.size != 0) {
      System.out.println("size is wrong.");
    }

    l.removeFront();
    System.out.println("\nRemoving front node.\nEmpty list is " + l);
    if (l.head != null) {
      System.out.println("head is wrong.");
    }
    if (l.tail != null) {
      System.out.println("tail is wrong.");
    }
    if (l.size != 0) {
      System.out.println("size is wrong.");
    }
  }

}

运行结果:

DList2:

/* DList2.java */

/**
 *  A DList2 is a mutable doubly-linked list.  Its implementation is
 *  circularly-linked and employs a sentinel (dummy) node at the head
 *  of the list.
 */

public class DList2 {

  /**
   *  head references the sentinel node.
   *
   *  DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
   */

  protected DListNode2 head;
  protected long size;

  /* DList2 invariants:
   *  1)  head != null.
   *  2)  For any DListNode2 x in a DList2, x.next != null.
   *  3)  For any DListNode2 x in a DList2, x.prev != null.
   *  4)  For any DListNode2 x in a DList2, if x.next == y, then y.prev == x.
   *  5)  For any DListNode2 x in a DList2, if x.prev == y, then y.next == x.
   *  6)  size is the number of DListNode2s, NOT COUNTING the sentinel
   *      (denoted by "head"), that can be accessed from the sentinel by
   *      a sequence of "next" references.
   */

  /**
   *  DList2() constructor for an empty DList2.
   */
  public DList2() {
    head = new DListNode2();
    head.item = Integer.MIN_VALUE;
    head.next = head;
    head.prev = head;
    size = 0;
  }

  /**
   *  DList2() constructor for a one-node DList2.
   */
  public DList2(int a) {
    head = new DListNode2();
    head.item = Integer.MIN_VALUE;
    head.next = new DListNode2();
    head.next.item = a;
    head.prev = head.next;
    head.next.prev = head;
    head.prev.next = head;
    size = 1;
  }

  /**
   *  DList2() constructor for a two-node DList2.
   */
  public DList2(int a, int b) {
    head = new DListNode2();
    head.item = Integer.MIN_VALUE;
    head.next = new DListNode2();
    head.next.item = a;
    head.prev = new DListNode2();
    head.prev.item = b;
    head.next.prev = head;
    head.next.next = head.prev;
    head.prev.next = head;
    head.prev.prev = head.next;
    size = 2;
  }

  /**
   *  insertFront() inserts an item at the front of a DList2.
   */
  public void insertFront(int i) {
      DListNode2 NewNode=new DListNode2(i);
      NewNode.prev=head;
      NewNode.next=head.next;
      head.next=NewNode;
      head.next.next.prev=NewNode;

      size++;

    // Your solution here.
  }

  /**
   *  removeFront() removes the first item (and first non-sentinel node) from
   *  a DList2.  If the list is empty, do nothing.
   */
  public void removeFront() {
      head.next=head.next.next;
      head.next.prev=head;
      if(size!=0) {
          size--;
      }
    // Your solution here.
  }

  /**
   *  toString() returns a String representation of this DList.
   *
   *  DO NOT CHANGE THIS METHOD.
   *
   *  @return a String representation of this DList.
   */
  public String toString() {
    String result = "[  ";
    DListNode2 current = head.next;
    while (current != head) {
      result = result + current.item + "  ";
      current = current.next;
    }
    return result + "]";
  }

  public static void main(String[] args) {
    // DO NOT CHANGE THE FOLLOWING CODE.

    DList2 l = new DList2();
    System.out.println("### TESTING insertFront ###\nEmpty list is " + l);

    l.insertFront(9);
    System.out.println("\nInserting 9 at front.\nList with 9 is " + l);
    if (l.head.next.item != 9) {
      System.out.println("head.next.item is wrong.");
    }
    if (l.head.next.prev != l.head) {
      System.out.println("head.next.prev is wrong.");
    }
    if (l.head.prev.item != 9) {
      System.out.println("head.prev.item is wrong.");
    }
    if (l.head.prev.next != l.head) {
      System.out.println("head.prev.next is wrong.");
    }
    if (l.size != 1) {
      System.out.println("size is wrong.");
    }

    l.insertFront(8);
    System.out.println("\nInserting 8 at front.\nList with 8 and 9 is " + l);
    if (l.head.next.item != 8) {
      System.out.println("head.next.item is wrong.");
    }
    if (l.head.next.prev != l.head) {
      System.out.println("head.next.prev is wrong.");
    }
    if (l.head.prev.item != 9) {
      System.out.println("head.prev.item is wrong.");
    }
    if (l.head.prev.next != l.head) {
      System.out.println("head.prev.next is wrong.");
    }
    if (l.head.next.next != l.head.prev) {
      System.out.println("l.head.next.next != l.head.prev.");
    }
    if (l.head.prev.prev != l.head.next) {
      System.out.println("l.head.prev.prev != l.head.next.");
    }
    if (l.size != 2) {
      System.out.println("size is wrong.");
    }

    l = new DList2(1, 2);
    System.out.println("\n\n### TESTING removeFront ###\nList with 1 and 2 is "
                       + l);

    l.removeFront();
    System.out.println("\nList with 2 is " + l);
    if (l.head.next.item != 2) {
      System.out.println("head.next.item is wrong.");
    }
    if (l.head.next.prev != l.head) {
      System.out.println("head.next.prev is wrong.");
    }
    if (l.head.prev.item != 2) {
      System.out.println("head.prev.item is wrong.");
    }
    if (l.head.prev.next != l.head) {
      System.out.println("head.prev.next is wrong.");
    }
    if (l.size != 1) {
      System.out.println("size is wrong.");
    }

    l.removeFront();
    System.out.println("\nEmpty list is " + l);
    if (l.head.next != l.head) {
      System.out.println("head.next is wrong.");
    }
    if (l.head.prev != l.head) {
      System.out.println("head.prev is wrong.");
    }
    if (l.size != 0) {
      System.out.println("size is wrong.");
    }

    l.removeFront();
    System.out.println("\nEmpty list is " + l);
    if (l.head.next != l.head) {
      System.out.println("head.next is wrong.");
    }
    if (l.head.prev != l.head) {
      System.out.println("head.prev is wrong.");
    }
    if (l.size != 0) {
      System.out.println("size is wrong.");
    }
  }

}

运行结果:

时间: 2024-10-02 20:53:08

2017.7.24-2017.7.30的相关文章

Becoming inspired - ASC - 2017 MARCH 24

Becoming inspired - The 11 questions to ask yourself when you feel uninspired @ Advanced Studio Classroom Vol: 2017 MARCH 24 Maybe you're struggling to find out what it is you care most about. Be struggling to.. To find out + what it is you care most

2017年最全的30个Android面试题,你将如何回答?

百度首页 登录 2017年最全的30个Android面试题,你将如何回答? 机翼技术 百家号 03-10 02:32 "三金四银"又是一年一度的跳槽季,相信有不少Android程序员开始摩拳擦掌蠢蠢欲动了.然而,面对面试官的问题,你将如何回答? 一.如何理解Activity,View,Window三者之间的关系? 这个问题真的很不好回答.所以先比较恰当的比喻来形容下它们的关系,Activity像一个工匠(控制单元),Window像窗户(承载模型),View像窗花(显示视图)Layout

2017.12.24(查找最接近元素等)

2017.12.24  贪心,分治综合习题(2) 1.查找最接近元素 思路:由题可知,n<=100000,m<=10000,如果每一个m都把这个非降序序列扫一遍的话,那么时间复杂的将要到达1010那么多,明显不合题意:所以,只能用二分查找来优化时间复杂度. 核心代码: int left=1,right=n,mid,bz=0; while(left<right-1){ bz=0; mid=(left+right)/2; if(k==num[mid]){ printf("%d\n&

2017年应试 | 9.考前30天如何冲刺?

这是我们第9篇讨论考试的文章了....更多的文章在公众号 ruankao580 中. 我们认为,考前30天的冲刺的关键是: 1.考点,考什么? 2.计划,如何分配时间? 3.题目,模拟应试现场. [考点] 注意,知识点并不完全等同于考点,中级和高级的知识点也不尽相同,教辅覆盖了主要的知识点,但具体到每一次考试其实仍然有所差异.中级是19个知识域,高级是22个知识域,但并非所有的知识域中的考点都要求掌握.我们主要的考点都在知识图谱或教辅的学习卡片中发布了. [计划] 根据攻克要塞的老师们多年培训的

2017冬季24集训模拟-3.耀西岛

--------------------------题解 路径的长度是1-200000 然后路径的条数有n*(n+1)/2 根据鸽巢原理n*(n+1)/2 > 200000就一定是YES 所以复杂度只有n^2 1 #include <iostream> 2 #include <queue> 3 #include <set> 4 #include <cstdio> 5 #include <cstring> 6 #include <vec

2017冬季24集训模拟-1.寻找幽灵

--------------------------------------------题解 把最短路处理出来然后做背包 没有把head数组和all初始化qwq 1 #include <iostream> 2 #include <queue> 3 #include <set> 4 #include <cstdio> 5 #include <cstring> 6 #include <vector> 7 #include <algo

【2017 4 24 - B】 组合数

[题目描述] 输入格式: 一行一个正整数n 输出格式: 一行一个数f(n)对1000000007取余的值 [分析] 就是乱搞?? 就是问根到叶子有多少条路径嘛. 然后路径可以π.1.1.π...这样表示 枚举有多少个$π$,算出最后一个π前面最多多少个1[这样比较不容易算重复什么的],然后用组合数算一算. 有一个比较坑的地方就是比如3.2是-π是大于0但是是不能减的因为3.2已经小于4了. 然后就是假设枚举了i个π,最后一个π前面最多y个1. 就是 $\sum_{j=0}^{y} C_{i+j-

2017冬季24集训模拟-4.排座椅

--------------------题解 统计这一列或行放通道能隔开几个人,然后贪心 输出没有排序QWQ 1 #include <iostream> 2 #include <queue> 3 #include <set> 4 #include <cstdio> 5 #include <cstring> 6 #include <vector> 7 #include <algorithm> 8 #define siji(i

2017第24周日

生在哪里不重要,活成什么样才最重要.美貌终会衰老,而智慧.善良.勇气,会随着岁月的流逝,而历久弥新,化成你身上退之不去的魅力. 哈马德和第一任妻子,本来就有两个儿子,但是他还是不顾卡塔尔立储立长的传统,王位传给他和莫扎的儿子.真正的珍珠,不会被埋没,但首先,你得把自己磨砺成珍珠. 这是刚看到的莫扎的故事,从囚犯子女成长为倾国皇妃,从被人蔑视嘲笑到受到国人崇敬,她用自己的努力智慧行动改变了卡塔尔,造福万千国民,她是很多出身不幸的人应该学习的榜样. 周日618,京东购物节,但感觉很一般,远没有那么多

2017.09.24校内训练

T1:个人卫生综合征 题目描述: 每天BBS都要从家里经过城市中的一段路到学校刷五三.城市中一共有n个路口和m条双向道路,每条双向道路都连接着两个路口ai.bi且有一定的时间花费vi.BBS家编号为1,学校编号为n.今天,BBS由于个人卫生综合征导致他很迟才离开家,他想用膜法改变k条道路的长度使通过其的时间花费vi变为0.现在他问你改变道路长度之后他到学校的最小时间花费是多少? 输入格式: 第一行为三个整数n.m.k,接下来的m行每行三个整数ai,bi,vi,分别表示这条路连着的两个路口和通过其