lab4

链表学的有点懵。总之就是SList,DList的head是随着插入的对象变化的,而循环链表的head是恒定的,在循环链表的任何操作中,都不能出现给head赋值的语句。

非循环的DList,在size==1的时候,head=tail;  head.next=null;   tail.pre=null 。此时tail和head均体现不出链表相连的特征,因此在表头插入第二个对象的时候,需要特别对tail.pre进行赋值;从此之后,再从表头插入数据,就不用管tail.pre了。

非循环链表:

package lab4;

/* 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 newLN=new DListNode1();
    newLN.item=i;
    if(size>1){
    newLN.prev=null;
    newLN.next=head;
    head.prev=newLN.next;
    head=newLN;
    }else if(size==1) {
        newLN.prev=null;
        newLN.next=head;
        head.prev=newLN.next;
        tail.prev=newLN;
        head=newLN;
    }else{
        head=newLN;
        head.prev=null;
        head.next=null;
        tail=head;
    }
    size++;// Your solution here.
  }

  /**
   *  removeFront() removes the first item (and node) from a DList1.  If the
   *  list is empty, do nothing.
   */
  public void removeFront() {
    if(size>1){
        head.next.prev=null;
        head=head.next;
        size--;
    }else if(size==1){
        head=null;
        tail=null;
        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 = "[  ";
    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.");
    }
  }

}

运行结果:

循环链表:

package lab4;

/* 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 newLN=new DListNode2();
    newLN.item=i;
    head.next.prev=newLN;
    newLN.next=head.next;
    newLN.prev=head;
    head.next=newLN;
    size++;

    // 新插入的node需要先和head.next建立连接,再和head建立连接,顺序不能颠倒,否则原来的head.next就找不到了。
  }

  /**
   *  removeFront() removes the first item (and first non-sentinel node) from
   *  a DList2.  If the list is empty, do nothing.
   */
  public void removeFront() {
      if(size==0){

      }else{
          head.next.next.prev=head;
          head.next=head.next.next;
          size--;}
/* 此处remove也可以写成
 * head.next=head.next.next;
 * head.next.pre=head;
 * 注意,head永远不可能出现在=的左边,与其他list不同,在有sentinal的链表中head的值是不变的。
 */

  }

  /**
   *  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-12 20:34:53

lab4的相关文章

[Lab4] 路由控制

[Lab4] 路由控制 工具1: Distribute List 过滤路由 试验拓扑 试验目的: R1 过滤掉R3的环回口3.3.3.3 1-1 DL Use For RIP R1/R2/R3跑 RIP 配置省略 R1的回环口1.1.1.1可以ping通R3的回环口3.3.3.3 目标在R1 上过滤掉3.3.3.3路由 我们在R2 上作distribute list R2(config)#access-list 10 deny 3.3.3.0 0.0.0.255 R2(config)#acces

actor binary tree lab4

forward 与 ! (tell) 的差异,举个例子: Main(当前actor): topNode ! Insert(requester, id=1, ele = 2) topNode: root ! Insert(requester, id=1, ele = 2) 对于root而言,Insert消息的来源是topNode 假如第二行替换为 topNode: root forward Insert(requester, id=1, ele = 2) 此时,对于root而言,Insert消息的

关于lab4实验git+近期出国手续办理

1.下载mit jos lab4时遇到问题(关于git操作,使用,还需进一步理解) 遇到的问题 出现未合并(merge)完全的问题,操作:git add kern/init.c 之后在确认提交 方法二(未实现) 删除该本地分支 但由于有问题(merge 冲突)无法删除或者更换分支 关于git待补充 ----------------------------------------------------------------------------------------------------

JOS lab4 用户程序分析

faultread.c faultdie.c: 这两个用户程序一起分析 左右两个用户程序,都试图对非法地址写入数据,但是左边的就会导致 page fault ,触发的是内核trap 而后边的就会进入打印trap frame,导致系统挂掉,而右边的不会,右边的由于有用户空间的处理机制, 还是建议自己一步步跟踪去看. faultalloc.c faultallocbad.c : 这里还是用户空间直接调用系统调用处理函数,还是通过用户程序触发page fault 之后再用用户空间page fault

CSAPP 六个重要实验 lab4

CSAPP && lab4 实验材料: http://download.csdn.net/detail/u011368821/7926305 实验指导书: http://download.csdn.net/detail/u011368821/7926323 实验环境: Linux 3.13.11 Ubuntu 14.0 Part I: An Experiment in C and Java Q&A Answer these questions: 1.  What are the s

[操作系统实验lab4]实验报告

昨天跟老师建议了OS实验改革的事情,感觉助教老师给的指导书挺坑哈,代码注释也不全.我也算沦落到看别人家的源码了... 我参考的源码注释是:https://github.com/benwei/MIT-JOS/ 这个源码质量暂且不评价,但这个注释质量真心不错!!!良心注释啊!!! 本不想去找源码注释啥来看的,毕竟可能一不小心就抄袭了源码的思想?不知道HT和WLM是怎么写的,他们做的都好快啊=.=难道只有我一个人做OS实验的周期是1~2周吗... 哎,不吐槽了,这篇文章留着慢慢更,不着急.感觉在鸣神的

操作系统ucore lab4实验报告

操作系统lab4实验报告 本次实验将接触的是内核线程的管理.内核线程是一种特殊的进程,内核线程与用户进程的区别有两个:内核线程只运行在内核态而用户进程会在在用户态和内核态交替运行:所有内核线程直接使用共同的ucore内核内存空间,不需为每个内核线程维护单独的内存空间而用户进程需要维护各自的用户内存空间. 在本次实验完成之后,为了加深理解,我这里简单将之前的所有代码又重新阅读并梳理了一遍,简单作了下总结. 这里主要是从kern_init函数的物理内存管理初始化开始的,截图如下: 按照函数的次序我进

MIT 6.828 学习笔记6 Lab4实验报告

Lab4实验报告 开始之前,为了弄懂 mpconfig 与 lapic,可能需要阅读 Intel processor manual 和 MP Specification,相关资源可以在课程中找到 Execrise 1 Implement mmio_map_region in kern/pmap.c. // mmio_map_region() uintptr_t ret = base; size = ROUNDUP(size, PGSIZE); base = base + size; if (ba

lab4打卡

由于pj1工程量有点大一时半会做不完,就先趁热打铁把双向链表的lab4做完了..PART1: 1 public void insertFront(int i) { 2 DListNode1 front1=new DListNode1(i); 3 if(size==0) { 4 head=front1; 5 tail=head; 6 } 7 else { 8 front1.next=head; 9 head.prev=front1; 10 head=front1; 11 } 12 size++;

MIT 操作系统实验 MIT JOS lab4

MIT JOS lab4 写在前面的碎碎念~ : 经历了LAB 3的洗礼,死磕到了lab 4. 这里还是首先向各位为JOS 实验做过笔记,写过博客,把自己实验代码托管到JOS上面的先行者们致敬! 如果没有这么好的开源环境, 这么好的东西学不来. 珍惜, 不用嘴. Doing is better than saying! -----------------------------------------------------------------------------------------