活跃农民
- 积分
- 521
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-8-17
- 最后登录
- 1970-1-1
|
本帖最后由 melissami 于 2014-6-11 21:03 编辑
这个写了好久,还是调不出来,LockDList依然有错 - package list;
- public class LockDList extends DList {
- protected LockDListNode head;
- protected LockDListNode tailnode;
- //protected LockDListNode temp;
- protected DListNode newNode(Object item, DListNode prev, DListNode next) {
- DListNode nnn = new LockDListNode(item,prev, next);
- return nnn;
- }
- public LockDList() {
- //super class constructor called by default
- }
-
- public void insertFront(Object item) {
- super.insertFront(item);
- }
-
-
- public void remove(DListNode node) {
- LockDListNode curr = this.head;
- while(curr.next != head){
- if(curr.item == node.item && curr.prev == node.prev && curr.next == node.next){
- if(curr.lockstatus == false){
- super.remove(node);
- }
- }
- curr = curr.next;
- }
- }
复制代码 在这一行会出错
while(curr.next != head){
java.lang.NullPointerException
明明现在curr不是空,这个list已经有5个item了,为什么curr.next会说是空指针呢?
其他的function,比如insertFront, insertBack之类的都是直接继承super class,没有修改,不知道这样可行咩~
输出如下(借用的lz的test case):
Now we are testing LockDList.
Here is a list after insertBack 6, 9, 12: [ 6 9 12 ]
Here is the same list after insertBack(15) and insertFront(3): [ 3 6 9 12 15 ]
front() should be 3. It is: 3
front's next should be 6. It is: 6
front's next's prev should be 3. It is: 3
lockNode: size is: 5
Exception in thread "main" java.lang.NullPointerException
at list.LockDList.lockNode(LockDList.java:45)
感觉还是我的继承有问题,但是不知道怎么改:/
我把DList代码也贴上来吧,方便挑错 实在不行,我过两天再删掉~- /* DList.java */
- package list;
- /**
- * A DList is a mutable doubly-linked list ADT. Its implementation is
- * circularly-linked and employs a sentinel (dummy) node at the head
- * of the list.
- *
- * DO NOT CHANGE ANY METHOD PROTOTYPES IN THIS FILE.
- */
- public class DList {
- /**
- * head references the sentinel node.
- * size is the number of items in the list. (The sentinel node does not
- * store an item.)
- *
- * DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
- */
- protected DListNode head;
- protected DListNode tailnode;
- protected int size;
-
- protected DListNode temp;
- /* DList invariants:
- * 1) head != null.
- * 2) For any DListNode x in a DList, x.next != null.
- * 3) For any DListNode x in a DList, x.prev != null.
- * 4) For any DListNode x in a DList, if x.next == y, then y.prev == x.
- * 5) For any DListNode x in a DList, if x.prev == y, then y.next == x.
- * 6) size is the number of DListNodes, NOT COUNTING the sentinel,
- * that can be accessed from the sentinel (head) by a sequence of
- * "next" references.
- */
- /**
- * newNode() calls the DListNode constructor. Use this class to allocate
- * new DListNodes rather than calling the DListNode constructor directly.
- * That way, only this method needs to be overridden if a subclass of DList
- * wants to use a different kind of node.
- * @param item the item to store in the node.
- * @param prev the node previous to this node.
- * @param next the node following this node.
- */
- protected DListNode newNode(Object item, DListNode prev, DListNode next) {
- return new DListNode(item, prev, next);
- }
- /**
- * DList() constructor for an empty DList.
- */
- public DList() {
- // Your solution here.
- head = newNode(null, null, null);
- head.next = head.prev = head;
- //tailnode = head;
- size = 0;
- }
- /*
- public DList returnDList(){
- return this;
- }
- */
- /**
- * isEmpty() returns true if this DList is empty, false otherwise.
- * @return true if this DList is empty, false otherwise.
- * Performance: runs in O(1) time.
- */
- public boolean isEmpty() {
- return size == 0;
- }
- /**
- * length() returns the length of this DList.
- * @return the length of this DList.
- * Performance: runs in O(1) time.
- */
- public int length() {
- return size;
- }
- /**
- * insertFront() inserts an item at the front of this DList.
- * @param item is the item to be inserted.
- * Performance: runs in O(1) time.
- */
- public void insertFront(Object item) {
- // Your solution here.
- //DListNode temp;
- if(size == 0){ // if list is empty
- tailnode = newNode(item, null, null);
- tailnode.prev = tailnode.next = head;
- head.prev = head.next = tailnode;
- //tailnode = temp;
- size++;
- }else{
- temp = newNode(item, null, null);
- temp.prev = head;
- temp.next = head.next;
- head.next = temp;
- //(head.next).prev = temp;
- (temp.next).prev = temp;
- size++;
- }
- }
- /**
- * insertBack() inserts an item at the back of this DList.
- * @param item is the item to be inserted.
- * Performance: runs in O(1) time.
- */
- public void insertBack(Object item) {
- // Your solution here.
- //DListNode temp;
- if(size == 0){ // if list is empty
- tailnode = newNode(item, null, null);
- tailnode.prev = tailnode.next = head;
- head.prev = head.next = tailnode;
- size++;
- }else{
- temp = newNode(item, null, null);
- temp.next = head;
- temp.prev = tailnode;
- tailnode.next = temp;
- tailnode = temp;
- head.prev = tailnode;
- size++;
- }
- }
- /**
- * front() returns the node at the front of this DList. If the DList is
- * empty, return null.
- *
- * Do NOT return the sentinel under any circumstances!
- *
- * @return the node at the front of this DList.
- * Performance: runs in O(1) time.
- */
- public DListNode front() {
- // Your solution here.
- if(isEmpty() == true){
- return null;
- }else{
- return head.next;
- }
- }
- /**
- * back() returns the node at the back of this DList. If the DList is
- * empty, return null.
- *
- * Do NOT return the sentinel under any circumstances!
- *
- * @return the node at the back of this DList.
- * Performance: runs in O(1) time.
- */
- public DListNode back() {
- // Your solution here.
- if(head.next == head){
- return null;
- }else{
- return tailnode;
- }
- }
- /**
- * next() returns the node following "node" in this DList. If "node" is
- * null, or "node" is the last node in this DList, return null.
- *
- * Do NOT return the sentinel under any circumstances!
- *
- * @param node the node whose successor is sought.
- * @return the node following "node".
- * Performance: runs in O(1) time.
- */
- public DListNode next(DListNode node) {
- // Your solution here.
- if(isEmpty() == true){
- return null;
- }else{
- return node.next;
- }
- }
- /**
- * prev() returns the node prior to "node" in this DList. If "node" is
- * null, or "node" is the first node in this DList, return null.
- *
- * Do NOT return the sentinel under any circumstances!
- *
- * @param node the node whose predecessor is sought.
- * @return the node prior to "node".
- * Performance: runs in O(1) time.
- */
- public DListNode prev(DListNode node) {
- // Your solution here.
- if(isEmpty() == true){
- return null;
- }else{
- return node.prev;
- }
- }
- /**
- * insertAfter() inserts an item in this DList immediately following "node".
- * If "node" is null, do nothing.
- * @param item the item to be inserted.
- * @param node the node to insert the item after.
- * Performance: runs in O(1) time.
- */
- public void insertAfter(Object item, DListNode node) {
- // Your solution here.
- //DListNode temp;
- if(node.item != null){
- if(node == tailnode){
- temp = newNode(item, null, null);
- temp.prev = node;
- temp.next = head;
- node.next = temp;
- tailnode = temp;
- size++;
- }else{ // not the end of DList
- temp = newNode(item, null, null);
- temp.prev = node;
- temp.next = node.next;
- node.next = temp;
- size++;
- }
- }
- }
- /**
- * insertBefore() inserts an item in this DList immediately before "node".
- * If "node" is null, do nothing.
- * @param item the item to be inserted.
- * @param node the node to insert the item before.
- * Performance: runs in O(1) time.
- */
- public void insertBefore(Object item, DListNode node) {
- // Your solution here.
- //DListNode temp;
- if(node.item != null){
- if(node == head.next){
- temp = newNode(item, null, null);
- temp.prev = head;
- temp.next = node;
- node.prev = temp;
- head.next = temp;
- size++;
- }else{ // not the beginning of DList
- temp = newNode(item, null, null);
- temp.prev = node.prev;
- temp.next = node;
- (node.prev).next = temp;
- node.prev = temp;
- size++;
- }
- }
- }
- /**
- * remove() removes "node" from this DList. If "node" is null, do nothing.
- * Performance: runs in O(1) time.
- */
- public void remove(DListNode node) {
- // Your solution here.
- if(node.item == tailnode.item){
- // && node.prev == tailnode.prev && node.next == tailnode.next
- //System.out.println("*******************remove: The node that's going to remove is tailnode");
- tailnode = node.prev;
- }
- if(node.item != null){
- //System.out.println("*******************remove");
- (node.prev).next = node.next;
- (node.next).prev = node.prev;
- }
- }
- /**
- * toString() returns a String representation of this DList.
- *
- * DO NOT CHANGE THIS METHOD.
- *
- * @return a String representation of this DList.
- * Performance: runs in O(n) time, where n is the length of the list.
- */
- public String toString() {
- String result = "[ ";
- DListNode current = head.next;
- while (current != head) {
- //System.out.println("while starts!!!! current is" );
- result = result + current.item + " ";
- current = current.next;
- }
- return result + "]";
- }
-
-
- }
复制代码 |
|