/* DListNode.java */
package cs61b.homework4;
/**
* A DListNode is a node in a DList (doubly-linked list).
*/
public class DListNode {
/**
* item references the item stored in the current node. prev references the
* previous node in the DList. next references the next node in the DList.
*
* DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
*/
public Object item;
private DListNode prev;
private DListNode next;
/**
* DListNode() constructor.
*
* @param i
* the item to store in the node.
* @param p
* the node previous to this node.
* @param n
* the node following this node.
*/
protected DListNode(Object i, DListNode p, DListNode n) {
item = i;
setPrev(p);
setNext(n);
}
public DListNode getNext() {
return next;
}
public void setNext(DListNode next) {
this.next = next;
}
public DListNode getPrev() {
return prev;
}
public void setPrev(DListNode prev) {
this.prev = prev;
}
}
/* LockDListNode.java */
package cs61b.homework4.Lockable;
homework4;
import cs61b.homework4.DListNode;
public class LockDListNode extends DListNode{
protected boolean lock;
protected LockDListNode(Object i, DListNode p, DListNode n){
super(i, p, n);
this.lock = false;
}
}
/* LockDList.java */
package cs61b.homework4.Lockable;homework4;
import cs61b.homework4.DList;
import cs61b.homework4.DListNode;
public class LockDList extends DList {
/**
* newNode() calls the LockDListNode constructor. Use this method to
* allocate new LockDListNodes rather than calling the LockDListNode
* constructor directly.
*
* @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 LockDListNode newNode(Object item, DListNode prev, DListNode next) {
return new LockDListNode(item, prev, next);
}
/**
* LockDList() constructor for an empty LockDList.
*/
public LockDList() {
super();
}
/**
* remove() removes "node" from this DList. If "node" is null, do nothing.
* Performance: runs in O(1) time.
*/
public void remove(DListNode node) {
if (node == null) {
return;
} else if (((LockDListNode)node).lock == true) {
return;
} else {
node.item = null;
node.getPrev().setNext(node.getNext());
node.getNext().setPrev(node.getPrev());
this.size--;
}
}
public void lockNode(DListNode node) {
if(node == null){
return;
}else{
((LockDListNode)node).lock = true;
}
}
}