Before all, here is a Minimal Working Example of my code on GitHub:
https://github.com/rmwesley/DancingLinks_MWE
I've been trying to implement the Dancing Links' algorithm by D. Knuth to solve the Exact Cover problem.
The code works. Problem is, I want to implement an Iterator.
In fact, the Iterator works for Node.java.
But not for Column.java, as I will further detail.
I've tried completely refactoring the code and doing some crazy modifications, but to no avail.
I left some of my best trials as commented lines of code.
These were the least garbagey ones.
My current design is as follows:
Given a problem matrix, I aimed at constructing the main data structure with 4-way nodes.
So first I implemented Node.java, a 4-way circularly linked data structure.
Then I extend Node.java in Column.java, which is the "backbone" of the structure.
Column elements then make up the main row.
Rows of Nodes are then linked with the rest of the structure with .addRow().
That is, new rows come below the last added row and above the column. Remember, circular.
See the schematics in D. Knuth's paper: https://arxiv.org/abs/cs/0011047.
With this, the full structure can be initialized from a given problem matrix.
"this" in Column serves as the head itself, so no elements are added above or below it.
Here is my source code:
Node.java
public class Node implements Iterable<Node> {
private Node upNode;
private Node downNode;
private Node leftNode;
private Node rightNode;
private Column column;
public Node() {
upNode = this;
downNode = this;
leftNode = this;
rightNode = this;
column = null;
}
#Override
public String toString() {
String str = this.column.getSize() + " ";
for (Node node : this){
str += node.column.getSize() + " ";
}
return str;
}
#Override
public java.util.Iterator<Node> iterator(){
Node currNode = this;
return new NodeIter(this);
}
public Column getColumn(){
return this.column;
}
public void setColumn(Column column){
this.column = column;
}
public Node getR(){
return this.rightNode;
}
public Node getD(){
return this.downNode;
}
public Node getL(){
return this.leftNode;
}
public Node getU(){
return this.upNode;
}
void removeHoriz() {
this.rightNode.leftNode = this.leftNode;
this.leftNode.rightNode = this.rightNode;
}
void removeVert() {
this.downNode.upNode = this.upNode;
this.upNode.downNode = this.downNode;
}
void restoreVert() {
this.downNode.upNode = this;
this.upNode.downNode = this;
}
void restoreHoriz() {
this.rightNode.leftNode = this;
this.leftNode.rightNode = this;
}
//Create an horizontal link between nodes
public void linkD(Node other) {
this.downNode = other;
other.upNode = this;
}
//Create a vertical link between nodes
public void linkR(Node other) {
this.rightNode = other;
other.leftNode = this;
}
void addHoriz(Node other) {
other.rightNode = this.rightNode;
other.leftNode = this;
}
void addVert(Node other) {
other.downNode = this.downNode;
other.upNode = this;
}
}
Column.java
import java.util.HashSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
//public class Column extends Node implements Iterable<Column>{
public class Column extends Node {
private int size;
private String name;
public Column() {
super();
this.setColumn(this);
size = 0;
name = new String();
}
public Column(int length) {
this();
Column currColumn = this;
for(int i = 0; i < length; i++){
currColumn.setName("" + i);
Column nextColumn = new Column();
currColumn.linkR(nextColumn);
currColumn = nextColumn;
}
currColumn.linkR(this);
}
public void addRow(int[] vector) throws Exception {
Column currColumn = this;
Node firstNode = new Node();
Node currNode = firstNode;
Node prevNode = currNode;
for(int index=0; index < vector.length; index++){
currColumn = currColumn.getR();
if(vector[index] == 0) continue;
currColumn.increment();
currColumn.getU().linkD(currNode);
currNode.linkD(currColumn);
currNode.setColumn(currColumn);
prevNode = currNode;
currNode = new Node();
prevNode.linkR(currNode);
}
currColumn = currColumn.getR();
prevNode.linkR(firstNode);
if(currColumn != this){
throw new Exception("Differ in length");
}
}
public Column(int[][] matrix) throws Exception {
this(matrix[0].length);
for(int i = 0; i < matrix.length; i++){
this.addRow(matrix[i]);
}
}
#Override
public Column getR(){
return (Column) super.getR();
}
#Override
public Column getL(){
return (Column) super.getL();
}
#Override
public String toString(){
String str = "";
//for (Column currColumn : this) str += currColumn.getSize() + " ";
for (Column currColumn = this.getR();
currColumn != this;
currColumn = currColumn.getR()){
str += currColumn.getSize() + " ";
}
return str;
}
public String getName(){
return this.name;
}
public int getSize(){
return this.size;
}
public void setSize(int size){
this.size = size;
}
public void setName(String name){
this.name = name;
}
public void increment(){
this.size++;
}
public void decrement(){
this.size--;
}
/*
#Override
public Iterator<Column> iterator(){
return new Iterator<Column>(){
private Column currNode = Column.this;
#Override
public boolean hasNext(){
return currNode.getR() != Column.this;
}
#Override
public Column next(){
if (!hasNext()) throw new NoSuchElementException();
currNode = currNode.getR();
return currNode;
}
};
}
*/
}
NodeIter.java
public class NodeIter implements java.util.Iterator<Node>{
private Node head;
private Node current;
public NodeIter(Node node){
this.head = this.current = node;
}
#Override
public boolean hasNext(){
return current.getR() != head;
}
#Override
public Node next(){
if (!hasNext()) throw new java.util.NoSuchElementException();
current = current.getR();
return current;
}
}
Commented lines give these errors when uncommented:
src/Column.java:5: error: Iterable cannot be inherited with different arguments: <Column> and <Node>
public class Column extends Node implements Iterable<Column>{
^
src/Column.java:111: error: iterator() in Column cannot implement iterator() in Iterable
public Iterator<Column> iterator(){
^
return type Iterator<Column> is not compatible with Iterator<Node>
where T is a type-variable:
T extends Object declared in interface Iterable
src/Column.java:76: error: incompatible types: Node cannot be converted to Column
for (Column currColumn : this) str += currColumn.getSize() + " ";
How do I make Column.java iterable?
I've been coding in Java recently, but without carefully considering design patterns.
So I fully believe I am suffering the consequences of bad code design.
Should I make some abstract class or make use of some Generic Type?
Like Node and Column, just so I can implement Iterable.
Am I wrong?
Does anyone have any pointers?
Tried using generics and overriding .iterator() method with different return types in Column.java.
Even tried using completely different class structures.
The Node class has an implementation of the Iterable interface in the form of one method:
#Override
public java.util.Iterator<Node> iterator(){
Node currNode = this;
return new NodeIter(this);
}
(BTW the first line of this method is not doing anything useful)
You are trying to make Node's subclass Column implement Iterable, meaning you want to add an overriding method like this:
#Override
public Iterator<Column> iterator()
Such an override which only differs in return type is not allowed in Java, hence the compilation error.
The fundamental problem is that, since Node is an Iterable, all its subclasses will also be an Iterable due to inheritance.
I guess you would like to write code like this:
for(Node n : node) {
for(Column c : n.getColumn()) {
c.increment();
}
}
Currently I think you could do this:
for(Node n : node) {
for(Node c : n.getColumn()) {
((Column) c).increment();
}
}
Where you are casting the iterand to Column in order to access Column methods.
I do think the design is weird when I read this for instance:
public Column() {
super();
this.setColumn(this);
eh? So a Column is a Node which has a column field? Seems like the design is conflicted about whether a Column is-a Node, or a Node has-a Column... I feel like your iterable problem will magically disappear once you figure that out.
EDIT: I don't fully grasp the algorithm and data structure yet (although I read a bit about it). From what I've understood I think you should create something like the following structure:
class Matrix {
Column[] columns;
Matrix(int[][] input) {
// init Columns
}
}
class Column {
String name;
int size;
Node firstNode;
}
class Node {
Node up;
Node down;
Node left;
Node right;
}
And avoid sub classing, it's usually not needed. Better to work with interfaces and collaborators.
Related
for uni we are supposed to implement an iterator for a String linked list by ourselves. But the infos how to do that was pretty small. So we tried it by ourselves and googled a lot but all the explanations we found dont include the whole code and I dont get how to implement the iterator correct. I think everything works fine as long as we use the for each loop to use the iterator but as soon as we are trying to use the "while (iterator.hasnext) { next }" thing it stays in the first element of the linked list. I think I know this problem is based on that we are always instantiating a new iterator but I dont get how to implement it else. Hopefully someone can help, I really don't know what to do, I tried everything..
import java.util.Iterator;
import java.util.NoSuchElementException;
public class LinkedList implements Iterable<String> {
// ---------- Attributes ----------
private int size = 0;
private Node head = null;
// private Iterator<String> linkedListIterator = this.iterator(); // ??
static class Node {
// ---------- Attributes ----------
private String object;
private Node next;
// ---------- Constructors ----------
public Node(String object, Node node) {
this.object = object;
this.next = node;
}
public Node() {
this(null, null);
}
// ---------- Getter, Setter ----------
public String getElement() {
return this.object;
}
public void setElement(String object) {
this.object = object;
}
public Node getNext() {
return this.next;
}
public void setNext(Node node) {
this.next = node;
}
}
class LinkedListIterator implements Iterator<String> {
// ---------- Attributes ----------
private Node currentNode = null;
private int counter = 0;
// ---------- Constructor ----------
public LinkedListIterator(LinkedList linkedList) {
this.currentNode = linkedList.head;
}
// ---------- Getter, Setter, Methods ----------
public boolean hasNext() {
return this.currentNode != null;
}
public String next() {
if (!this.hasNext()) {
System.out.println("Fehler: ");
throw new NoSuchElementException();
}
String object = this.currentNode.getElement(); // ?
this.currentNode = this.currentNode.getNext();
this.counter++;
return object;
}
public int getCounter() {
return this.counter;
}
}
// ---------- Getter, Setter, Methods ----------
public Node getHead() {
return this.head;
}
public void addFirst(String object) {
// new node as head
Node newNode = new Node(object, this.head);
this.head = newNode;
this.size++;
}
public String getFirst() { //throws ListEmptyException {
if (isEmpty()) {
// throw new ListEmptyException();
}
return this.head.getElement();
}
public String removeFirst() { //throws ListEmptyException {
if (isEmpty()) {
// throw new ListEmptyException();
}
String object = this.head.getElement();
this.head = this.head.getNext();
return object;
}
public boolean isEmpty() {
return this.head == null;
}
public int getSize() {
return this.size;
}
#Override
public Iterator<String> iterator() {
System.out.println("helo");
return new LinkedListIterator(this);
}
public String toString() {
String output = "";
// this is working:
// for (String element: this) {
// output += element + "\n";
// }
while (this.iterator().hasNext()) {
System.out.println(this.iterator().hasNext());
output += this.iterator().next() + "\n";
}
return output;
}
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.addFirst("a");
ll.addFirst("b");
ll.addFirst("c");
ll.addFirst("d");
ll.addFirst("e");
System.out.println(ll.toString());
}
}
Problem solved by this
But new question: Why is this working
public String toString() {
String output = "";
Iterator<String> iterator = this.iterator();
while (iterator.hasNext()) {
output += it.next() + "\n";
}
return output;
}
But this not
public class LinkedList implements Iterable<String> {
private Iterator<String> linkedListIterator = this.iterator();
public String toString() {
String output = "";
while (this.linkedListIterator.hasNext()) {
output += this.linkedListIterator.next() + "\n";
}
return output;
}
}
Your implementation of LinkedListIterator is correct, the problem is in the toString() method.
You are calling this.iterator() 3 times, so each time you return a new instance of LinkedListIterator.
Instead you have to call this.interator() only once and use the instance you get.
Like this:
Iterator<String> it=this.iterator();
while (it.hasNext()) {
System.out.println(it.hasNext());
output += it.next() + "\n";
}
Regarding the new question.
If you instantiate the private Iterator<String> linkedListIterator attribute in the body of the class, (Something that should never be done), every time you refer to it you will make a call to the public Iterator<String> iterator() method and you will get a new instance of LinkedListIterator.
You are making the same mistake as in the beginning.
This is an example of why attributes should be instantiated only within a method declaration.
Remember that an iterator can only move forward, if you want to restart it you must create a new instance. That's what you do by calling this.iterator().
I recommend you to use some debugging tools so you can see the instructions that are executed
Also, there is a design pattern that deals iterators.
https://en.wikipedia.org/wiki/Iterator_pattern
Implementing LinkedList in a recursive approach was a bit challenging to me, which I get stuck in implementing of its remove method and wonder how to keep reference to previous item in recursive?
MyLinkedList class
package linkedlist;
public class MyLinkedList {
private Integer value;
private MyLinkedList next;
public MyLinkedList() {
}
public MyLinkedList(Integer value) {
this.value = value;
}
public void add(Integer value) {
if (this.value == null) {
this.value = value;
} else if (this.next == null) {
this.next = new MyLinkedList(value);
} else {
this.next.add(value);
}
}
public MyLinkedList remove(Integer index) {
//
// if (index < 0) {
// return this;
// }
// if (index == 0) {
// return this.next;
// }
// this.next = remove(index - 1);
return this;
}
public Integer indexOf(Integer value) {
if (this.value.equals(value)) {
return 0;
} else if (this.next == null) {
return null;
} else {
return 1 + this.next.indexOf(value);
}
}
}
MyLinkedListTester class
package linkedlist;
public class MyLinkedListTester {
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.add(1);
myLinkedList.add(2);
myLinkedList.add(3);
myLinkedList.add(4);
System.out.println("Index Of Array: " + myLinkedList.indexOf(3));
MyLinkedList linkedList = myLinkedList.remove(3);
}
}
As mentioned in the comments the iterative approach is easier and more efficient most of the time. Anyway I think you do this as an exercise because in Java you already have a LinkedList.
So first you have a kind of error in your thinking (as far as I'm aware of it). It's also a kind of bad design choice. You create your MyLinkedList and save the data right into it and the next is also of the class MyLinkedList but it's not a list, it's a Node. There should only be one List, and 0 - many nodes.
For example I can't figure out how to do a remove function that will return the removed Node (in your case MyLinkedList) and as well let you keep the list in case you remove the first element in your list.
If you are looking in the implementation that's why they use Nodes and it's also more logical (a list doesn't contain "List elements") and so on...
Some other remark: your indexOf funtion will return an error if you try to get a element that does not exist (1 + null => error).
So anyway. What you have to do is to create a Node. (btw if you really want a real LinkedList you can use generic instead of int/Integer).
Below I post my solution how to do it (may be better out there but that is how I would do it). I also wrote a toString method to see how the List looks like (and it works as far as I can say). In case you want to still use your code without the Node it should give you an idea how to solve your problem with remove. You can also put some of the logic into the Node class but for me Node is only a container and doesn't really contain any logic.
public class MyLinkedList {
private Node head;
public MyLinkedList() {
}
public class Node{
private int value;
private Node next = null;
public Node(int value){
this.value = value;
}
public int getValue(){
return value;
}
public Node getNext(){
return next;
}
public void setNext(Node next){
this.next = next;
}
}
public void add(int value) {
Node next = new Node(value);
if(head == null){
head = next;
} else {
addRecursive(head,next);
}
}
private void addRecursive(Node node, Node next) {
if(node.next == null){
node.setNext(next);
} else {
addRecursive(node.getNext(),next);
}
}
public Node remove(int index){
Node removeNode = head;
if(index == 0){
head = head.getNext();
} else {
removeNode = removeRecursive(head,index-1);
}
return removeNode;
}
private Node removeRecursive(Node node, int index){
Node removeNode = node.getNext();
if(index == 0){
node.setNext(removeNode.getNext());
} else {
removeNode = removeRecursive(node.getNext(),index-1);
}
return removeNode;
}
public int indexOf(int value) {
if (head == null){
return -1;
} else if (head.getValue() == value){
return 0;
} else {
return indexOfRecursive(head,value,0);
}
}
private int indexOfRecursive(Node node, int value, int index) {
if(node.getNext() == null){
return -1;
} else if(node.getNext().getValue() == value){
return index + 1;
} else {
return indexOfRecursive(node.getNext(),value,index+1);
}
}
#Override
public String toString(){
if(head == null){
return "";
} else {
return toStringRecursive(head,"["+head.getValue());
}
}
private String toStringRecursive(Node node, String output){
if(node.getNext() == null){
return output + "]";
} else {
return toStringRecursive(node.getNext(),output + ", " + node.getNext().getValue());
}
}
}
This question already has answers here:
How to implement a tree data-structure in Java?
(27 answers)
Closed 7 years ago.
I've always wondered what is the best practice to create objects which have lists within lists within lists etc. Let's say I have this sort of object :
root = new CarrierTreeNode(null,
new CarrierTreeNode[] {
new CarrierTreeNode(new CarrierTreeItem("item1"), new CarrierTreeNode[] {
new CarrierTreeNode(new CarrierTreeItem("item1.1"))
}),
new CarrierTreeNode(new CarrierTreeItem("item2"), new CarrierTreeNode[] {
new CarrierTreeNode(new CarrierTreeItem("item2.1"))
})
});
and I want to generate this dynamically and also access and modify the lists/arrays within it dynamically. Is there a design pattern for this?
Thank you.
To make it more clear, the constructor used here is like this : node (item, node[])
You might want to have a look at the composite pattern.
In short, this is used when Object A holds a collection of Object A.
This pattern / data structure allows easy recursive behavior.
There are many sources, so if this doesn't suffice, simple go to google for more information, but here's a start:
https://en.wikipedia.org/wiki/Composite_pattern
As for the creation part, I'd typically use a factory or builder in that case, but the exact implementation vary. Say you had a 2d array if items, and you wanted to create these nodes according to this array.
public class NodeBuilder{
public CarrierTreeNode build(String[][] items){
CarrierTreeNode node = new CarrierTreeNode(null);
for(int i = 1; i < items.length; i++){
CarrierTreeNode nextNode = new CarrierTreeNode(new CarrierTreeItem(items[i][0]));
node.addNextNode(nextNode);
for(int j = 1; j < items[i].length; j++)
nextNode.addNextNode(new CarrierTreeItem(items[i][j]));
}
return node;
}
}
This would obviously only work for a structure of 3 layers. A recursive approach is preferable. You could create a system where build calls build n times, to create the depth you want. The problem is in acquiring the data, for that to work, the data must already be in the right structure, but just as strings.
If your strings are dynamically generated, so that the builder could figure out the data, it would be possible to make it work.
I propose this design hoping it will help you :
TreeElement iterface : the generic tree element type.
Item class that implements the TreeElement iterface :so it can be a
key for a tree node or a value of a tree node.
Node class that implements also the TreeElement interface so it can
be a base node or a value of a key.
This is the implementation :
interface TreeElement {
enum ElementType {
NODE, ITEM
};
public TreeElement getElement(Item item);
public Node addElement(Item item, TreeElement element);
public ElementType getType();
}
class Item implements TreeElement {
String name;
public Item(String name) {
super();
this.name = name;
}
#Override
public TreeElement getElement(Item item) {
return null;
}
#Override
public ElementType getType() {
return ElementType.ITEM;
}
#Override
public Node addElement(Item item, TreeElement element) {
return null;
}
#Override
public String toString() {
return "Item [" + name + "]";
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Item other = (Item) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
class Node implements TreeElement {
Map<Item, TreeElement> map;
{
map = new HashMap<>();
}
#Override
public TreeElement getElement(Item item) {
return map.get(item);
}
#Override
public ElementType getType() {
return ElementType.NODE;
}
public Node addElement(Item item, TreeElement element) {
this.map.put(item, element);
return this;
}
#Override
public String toString() {
return "Node " + map + " ";
}
}
Test the code :
Node node = new Node();
Item item = new Item("Item 1");
node.addElement(item, new Node().addElement(new Item("Level 1"), new Item("Item 1")));
System.out.println(node);
//Print : Node {Item [Item 1]=Node {Item [Level 1]=Item [Item 1]} }
TreeElement element = node.getElement(item);
if (element.getType().equals(ElementType.NODE)) {
element.addElement(new Item("Level 2"), new Node().addElement(new Item("Item 2.1"), new Item("Item 2.2")));
}
System.out.println(node);
//Print : Node {Item [Item 1]=Node {Item [Level 1]=Item [Item 1], Item [Level 2]=Node {Item [Item 2.1]=Item [Item 2.2]} } }
Important : equals() and hashcode() are critical for the behavior of the example especially equals(). They are used in the Map collections to determine if a collection contains a given element.
Most simple tree node implementation must contain one field for store data and list of own children.
public class Node<T> {
private T data;
private List<Node> list = new LinkedList<>();
public Node(){}
public Node(T data){
this.data = data;
}
public List<Node> getChildList() {
return list;
}
public void setData(T data) {
this.data = data;
}
public T getData() {
return data;
}
}
This class is ready to build simple tree structure:
Node<String> root = new Node<String>("Stark");
root.getChildList().add(new Node<String>("Benjen"));
Node<String> eddard = new Node<String>("Eddard");
root.getChildList().add(eddard);
eddard.getChildList().add(new Node<String>("Arya"));
This implementation is simplest but of cause not the best. Classically tree node provides information about parent and sibling nodes, methods for internal recursive search , control of cycle references and many other things (wiki describes it very detailed).
I am currently running into problems with java generics, linked list/ques structures and methods that should operate on them. Currently, I am trying to write generic methods that should manipulate a linked list of jobs for my school project. I have to implement basic methods, such as enque, de-que, sort-by-priority, get number of elements and so on. The element is, say, a printing job with a priority. A print que shall be implemented as a linked list of jobs. I am not allowed to use any pre-defined collection classes.
This being said, I am not getting something obvious. In the java code shown below, there are 3 classes (Job, MyPrintQue and LinkNode) and one generic interface (PrintQue). I am not importing any other classes from java.util. In the line 85 I use a curr.data.getPriority() method, but curr.data is taken here as the type Object, instead of the type Job, and therefore does have getPriority() method defined. Not sure why is that and how to fix it.
I've gone through a couple of related posts here, but have not found any remedy to my problem. Would be grateful for any input.
Here's the code:
Class Job
public class Job {
private int priority;
public Job(int i) {this.priority=i;}
public int getPriority(){return priority;}
public String toString () {return String.format("This job has priority %d", priority);}
}
Class MyPrintQue
public class ListNode<Job> {
public Job head;
public ListNode<Job> tail;
ListNode (Job j) {this.head=j;}
public Job getHead(){return head;}
public void setHead(Job j){}
}
Interface PrintQue
public interface PrintQue<Job> {
public void enque(Job j);
public void deque(ListNode<Job> n);
public void printQue();
public boolean isEmpty();
public ListNode<Job> hasTheHighestPriority();
public void sortByPriority();
}
and Class MyPrintQue
public class MyPrintQue<Job> implements PrintQue<Job>
{
//Setting up front and end elements of a print que.
private ListNode<Job> front;
private ListNode<Job> end;
private static int queLength;
//Accessors for head and tail.
public ListNode<Job> getFront(){return front;}
public ListNode<Job> getEnd(){return end;}
public void enque(Job j)
{
if (front == null && end == null)
{
front = new ListNode<Job>(j);
queLength++;
}
else if (front !=null & end == null)
{
end = new ListNode<Job>(j);
front.tail =end;
queLength++;
}
else
{
ListNode<Job> temp = new ListNode<Job>(j);
end.tail = temp;
end = temp;
queLength++;
}
}
public boolean find(ListNode<Job> n)
{
for (ListNode<Job> curr = front; curr !=null; curr = curr.tail)
{
if (curr == n) return true;
}
return false;
}
public void deque(ListNode<Job> n)
{
if (find(n))
{
for (ListNode<Job> curr = front; curr !=null; curr = curr.tail)
{
if (front == n) {front = n.tail;}
else if (curr.tail == n) {curr.tail=n.tail;}
}
n = null;
queLength--;
}
}
public void printQue()
{
int length=0;
for (ListNode<Job> curr = front; curr !=null; curr = curr.tail)
{
System.out.println(curr.head);
length++;
}
System.out.println(length);
}
public boolean isEmpty(){if (front == null) return true; else return false;}
public ListNode<Job> hasTheHighestPriority()
{
ListNode<Job> temp = new ListNode<Job>(null);
int prior = 0;
for (ListNode<Job> curr = front; curr.head !=null; curr = curr.tail)
{
if (prior <= ((curr.head).getPriority()))
{
System.out.printf("Current priority is %d, top priority is %d%n", curr.head.getPriority(), prior);
temp = curr;
prior = (int)curr.head.getPriority();
}
}
return temp;
}
public void sortByPriority()
{
MyPrintQue<Job> temp = new MyPrintQue<Job>();
while(!isEmpty())
{
temp.enque(hasTheHighestPriority().head);
deque(hasTheHighestPriority());
}
front = temp.front;
}
}
The difference between your
public class MyPrintQue<Job> implements PrintQue<Job>
and
public class MyJobPrintQue implements PrintQue<Job>
is that in the first case Job is a generic type parameter, nothing to do with the class Job.
And the rewrite, there is a PrintQue of the class Job.
Instead of parameters <Job> better use <J> or whatever.
For good order "queue" is the spelling in English (for an explanation "few" also has double u).
The NullPointerException can be removed by:
if (front == n) {
front = n.tail;
} else {
for (ListNode<Job> curr = front; curr !=null; curr = curr.tail) {
if (curr.tail == n) {
curr.tail = n.tail;
break;
}
}
}
I have an assignment that I am terribly lost on involving doubly linked lists (note, we are supposed to create it from scratch, not using built-in API's). The program is supposed to keep track of credit cards basically. My professor wants us to use doubly-linked lists to accomplish this. The problem is, the book does not go into detail on the subject (doesn't even show pseudo code involving doubly linked lists), it merely describes what a doubly linked list is and then talks with pictures and no code in a small paragraph. But anyway, I'm done complaining. I understand perfectly well how to create a node class and how it works. The problem is how do I use the nodes to create the list? Here is what I have so far.
public class CardInfo
{
private String name;
private String cardVendor;
private String dateOpened;
private double lastBalance;
private int accountStatus;
private final int MAX_NAME_LENGTH = 25;
private final int MAX_VENDOR_LENGTH = 15;
CardInfo()
{
}
CardInfo(String n, String v, String d, double b, int s)
{
setName(n);
setCardVendor(v);
setDateOpened(d);
setLastBalance(b);
setAccountStatus(s);
}
public String getName()
{
return name;
}
public String getCardVendor()
{
return cardVendor;
}
public String getDateOpened()
{
return dateOpened;
}
public double getLastBalance()
{
return lastBalance;
}
public int getAccountStatus()
{
return accountStatus;
}
public void setName(String n)
{
if (n.length() > MAX_NAME_LENGTH)
throw new IllegalArgumentException("Too Many Characters");
else
name = n;
}
public void setCardVendor(String v)
{
if (v.length() > MAX_VENDOR_LENGTH)
throw new IllegalArgumentException("Too Many Characters");
else
cardVendor = v;
}
public void setDateOpened(String d)
{
dateOpened = d;
}
public void setLastBalance(double b)
{
lastBalance = b;
}
public void setAccountStatus(int s)
{
accountStatus = s;
}
public String toString()
{
return String.format("%-25s %-15s $%-s %-s %-s",
name, cardVendor, lastBalance, dateOpened, accountStatus);
}
}
public class CardInfoNode
{
CardInfo thisCard;
CardInfoNode next;
CardInfoNode prev;
CardInfoNode()
{
}
public void setCardInfo(CardInfo info)
{
thisCard.setName(info.getName());
thisCard.setCardVendor(info.getCardVendor());
thisCard.setLastBalance(info.getLastBalance());
thisCard.setDateOpened(info.getDateOpened());
thisCard.setAccountStatus(info.getAccountStatus());
}
public CardInfo getInfo()
{
return thisCard;
}
public void setNext(CardInfoNode node)
{
next = node;
}
public void setPrev(CardInfoNode node)
{
prev = node;
}
public CardInfoNode getNext()
{
return next;
}
public CardInfoNode getPrev()
{
return prev;
}
}
public class CardList
{
CardInfoNode head;
CardInfoNode current;
CardInfoNode tail;
CardList()
{
head = current = tail = null;
}
public void insertCardInfo(CardInfo info)
{
if(head == null)
{
head = new CardInfoNode();
head.setCardInfo(info);
head.setNext(tail);
tail.setPrev(node) // here lies the problem. tail must be set to something
// to make it doubly-linked. but tail is null since it's
// and end point of the list.
}
}
}
Here is the assignment itself if it helps to clarify what is required and more importantly, the parts I'm not understanding. Thanks
https://docs.google.com/open?id=0B3vVwsO0eQRaQlRSZG95eXlPcVE
if(head == null)
{
head = new CardInfoNode();
head.setCardInfo(info);
head.setNext(tail);
tail.setPrev(node) // here lies the problem. tail must be set to something
// to make it doubly-linked. but tail is null since it's
// and end point of the list.
}
the above code is for when u not have any nodes in list, here u r going to add nodes to ur list.I.e. ist node to list
here u r pointing head & tail to same node
I assume CardList is meant to encapsulate the actual doubly-linked-list implementation.
Consider the base case of a DLL with only a single node: the node's prev and next references will be null (or itself). The list's encapsulation's head and tail references will both be the single node (as the node is both the start and end of the list). What's so difficult to understand about that?
NB: Assuming that CardList is an encapsulation of the DLL structure (rather than an operation) there's no reason for it to have a CardInfoNode current field, as that kind of state information is only useful to algorithms that work on the structure, which would be maintaining that themselves (it also makes your class thread-unsafe).