I wrote a method that adds an item to the front of a doubly linked list. Every time I call the function, it should add the item passed through it. The function works as expected, except for when I iterate through the doubly linked list and print each item, it always prints null twice at the end. My code is below.
public Deque() {
first = new Node();
last = new Node();
first.next = last;
last.prev = first;
}
public void addFirst(E item) {
if (item.equals(null)) {
throw new NullPointerException();
} else {
if (first.equals(null) && last.equals(null)) {
first = new Node();
first.next.item = item;
first.next.next = null;
last = first;
} else {
Node node = new Node();
node.item = item;
node.next = first;
first = node;
}
}
N++;
}
public static void main(String[] args) {
Deque<Integer> lst = new Deque<Integer>(); // empty list
lst.addFirst(1);
lst.addFirst(5);
lst.addFirst(7);
lst.addFirst(9);
Iterator<Integer> it = lst.iterator(); // tests iterator method
while (it.hasNext()) {
Integer val = it.next();
System.out.println(val);
}
}
That code prints: 9, 5, 7, 1, null, null. I cannot figure out the two extra null values are being printed. Can anybody tell me how I can fix my code so that it does not print null two times at the end?
You're creating two nodes called first and last as part of the constructor of the Deque class.
public Deque() {
first = new Node();
last = new Node();
first.next = last;
last.prev = first;
}
So, your items get added in front of them. When you print them out, it prints the ones you added and then first and last, and since they don't have an item set, it prints null.
Related
I am attempting to create an Deque class (Stack/Queue that can be added to and referenced at both ends) by implementing a doubly linked-list format.
import java.util.Iterator;
public class Deque implements Iterable {
Node first;
Node last;
int size;
public Deque()
{
first = null;
last = null;
size = 2;
first.next = last;
last.prev = first;
}
private class Node
{
Node next;
Node prev;
Item item;
}
private class ListIterator implements Iterator<Item>
{
private Node current = first;
public boolean hasNext()
{
return current.next != null;
}
public Item next()
{
Item item = current.item;
current = current.next;
return item;
}
public void remove()
{
/* not supported */
}
}
public boolean isEmpty()
{
if(first == null&&last == null)
return true;
return false;
}
public int size()
{
return size;
}
public void addFirst(Item item)
{
Node oldfirst = first;
first = new Node();
first.item = item;
first.next = oldfirst;
oldfirst.prev = first;
size++;
}
public void addLast(Item item)
{
Node oldlast = last;
last = new Node();
last.item = item;
last.prev = oldlast;
oldlast.next = last;
size++;
}
public Item removeFirst()
{
Item item = first.item;
first = first.next;
size--;
return item;
}
public Item removeLast()
{
Item item = last.item;
last = last.next;
size--;
return item;
}
#Override
public Iterator<Item> iterator()
{
return (new ListIterator());
}
public static void main(String[] args)
{
Deque<Integer> deque = new Deque<Integer>();
for(int i=0; i<5; i++)
{
deque.addFirst(i);
deque.addLast(9-i);
}
for(Integer i : deque)
{
StdOut.println(i);
}
}
}
When I run the code, I get a NullPointerException when it tries to do first.next = last; I can understand why, but I'm not sure how to fix it without breaking the list. Any solutions? Is it perhaps unnecessary to use a doubly linked format (i.e. remove the prev reference Node altogether)?
You avoid NullPointerException by avoiding access to uninitialized variables.
In that particular example, leave out the:
first.next = last;
last.prev = first;
in your constructor and use defensive programming and check for null if it could be uninitialized, before accessing a variable.
For example in your addFirst method:
public void addFirst(Item item)
{
Node oldfirst;
if (first != null){
oldfirst = first;
}
first = new Node();
first.item = item;
if (oldfirst != null){
first.next = oldfirst;
oldfirst.prev = first;
}
size++;
}
etc.
By the way, is this a learning exercise? If not, Java library does have Deques ready to use, including linked list:
http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
How is your size beginning at 2? It should be 0 until you add an Item.
You initial conditions should be that both prev and next are null. When you add a single item, then size should be 1 and both prev and next should point to that object.
When the Deque is empty, there is no "next" and "previous". It is completely empty. There would be "next" and "previous" only as soon as there is data.
So when you initialize the Deque, you should not attempt to assign a prev and next to null references. The fact that they are null indicates that there's nothing there, so of course there is nothing that comes previously or after it.
And of course, the size should be zero.
Then, in your addFirst and addLast methods, you should handle the case in which your first and last are null. In that case, you have to initialize them both to the same value, where its next and prev are both null. And set the size to 1.
Only proceed as you did (add item, correct the linkage) if the item in first or last respectively is not null.
And remember to check for null in your removeFirst and removeLast methods as well.
Short version: the case of an empty list is special. You should start with an empty list. You should check for this special case in your add and remove methods.
Having a bit of trouble adding a node to the end of my linked list. It only seems to display the very last one I added before I call my addFirst method. To me it looks like on the addLast method I'm trying to first create the node to assign it 5, then for the following numbers use a while loop to assign them to the last node on the linked list. Little stuck on why I can't get my output to display 5 and 6.
class LinkedList
{
private class Node
{
private Node link;
private int x;
}
//----------------------------------
private Node first = null;
//----------------------------------
public void addFirst(int d)
{
Node newNode = new Node();
newNode.x = d;
newNode.link = first;
first = newNode;
}
//----------------------------------
public void addLast(int d)
{
first = new Node();
if (first == null)
{
first = first.link;
}
Node newLast = new Node();
while (first.link != null)
{
first = first.link;
}
newLast.x = d;
first.link = newLast;
first = newLast;
}
//----------------------------------
public void traverse()
{
Node p = first;
while (p != null)
{
System.out.println(p.x);
p = p.link;
}
}
}
//==============================================
class test123
{
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.addLast(5);
list.addLast(6);
list.addLast(7);
list.addFirst(1);
list.addFirst(2);
list.addFirst(3);
System.out.println("Numbers on list");
list.traverse();
}
}
I've also tried creating a last Node and in the traverse method using a separate loop to traverse the last node. I end up with the same output!
public void addLast(int d)
{
Node newLast = new Node();
while (last.link != null)
{
last = newLast.link;
}
newLast.x = d;
newLast.link = last;
last = newLast;
}
The logic of your addLast method was wrong. Your method was reassigning first with every call the logic falls apart from that point forward. This method will create the Node for last and if the list is empty simply assign first to the new node last. If first is not null it will traverse the list until it finds a Node with a null link and make the assignment for that Nodes link.
public void addLast(int d) {
Node last = new Node();
last.x = d;
Node node = first;
if (first == null) {
first = last;
} else {
while (node.link != null) {
node = node.link;
}
node.link = last;
}
}
Your addLast() method displays the value of the last node because every time you append a node to the end of your list, you are overwriting the reference to "first". You are also doing this when you assign a new reference to first in the following line:
first = new Node();
Try the following:
public void addLast(int d)
{
Node newLast = new Node();
if (first == null)
{
newLast.x = d;
first = newLast;
return;
}
Node curr = first;
while (curr.link != null)
{
curr = curr.link;
}
newLast.x = d;
curr.link = newLast;
}
The method creates a new node and it is added to the end of the list after checking two conditions:
1.) If first is null: in this case the list is empty and the first node should be
initialized to the new node you are creating, then it will return (or you could do
an if-else).
2.) If first is not null: If the list is not empty you'll loop through your list until you
end up with a reference to the last node, after which you will set its next node to the
one you just created.
If you wanted to keep track of the tail of your list called "last", like you mentioned above, then add:
last = newLast
at the end of your method. Hope this helps!
I'm trying to add a new Node to the end of my linked list but it doesn't seem to be doing anything. It adds the first element because it's a special case but then ignores all of the other assignments when I step through the debugging.
Here's the test I'm running:
#Test
public void testInsertElement()
{
PriorityList<String> list = new LinkedPriorityList<String>();
list.insertElementAt(0, "first");
list.insertElementAt(1, "second");
list.insertElementAt(2, "third");
assertEquals("first" , list.getElementAt(0));
assertEquals("second", list.getElementAt(1));
assertEquals("third" , list.getElementAt(2));
}
It fails on the second assertion because nothing is added after the first.
Here's the constructor for the Node Objects:
public class LinkedPriorityList<E> implements PriorityList<E> {
private class Node
{
private E data;
private Node next;
public Node(E element)
{
data = element;
next = null;
}
}
And finally the code that is failing on me:
public void insertElementAt(int index, E element) throws IllegalArgumentException
{
if(index>size() || index<0) //can only be between 0 and size()
throw new IllegalArgumentException();
if(size()==0)
first = new Node(element); //adding the first element. This works
else
{
if(index == size()) //if an element is being added to the end
{
Node ref = first; //assigning ref to the first element of the list
for(;ref!=null; ref = ref.next); //stepping through the list until ref is null
ref = new Node(element); //assigning the null reference a new Node. Doesn't assign
}
else //if an element is being inserted in the list. untested...
{
Node ref = first;
Node temp = new Node(element);
for(int i=1; i<index; i++)
ref = ref.next;
temp = ref.next;
ref = temp;
}
}
size++; //keeping track of how many elements in list
}
I think this works but if you want the get method too, here it is:
public E getElementAt(int index) throws IllegalArgumentException
{
if(index>=size() || index<0)
throw new IllegalArgumentException();
Node ref = first;
for(int i=0; i<index; i++)
ref = ref.next;
return ref.data;
}
When index == size, you want to create a new node, find the last node in the list, and assign the new node to its next pointer.
The last node is the one whose next pointer is null.
This should be enough to let you implement the algorithm by yourself.
This is probably what you meant to do:
for(; ref.next != null; ref = ref.next) {
/* intentionally empty */
}
ref.next = new Node(element);
Note that I'm both testing and assigning ref.next, not ref itself.
You need a temp node when adding at the end too (to keep track of the last element)
if (index == size())
{
Node ref = first, temp = first;
for (; ref != null; temp = ref, ref = ref.next);
temp.next = new Node(element);
}
By just assigning the new Node to ref; it doesn't link it to the current last node's next.
I have been working on a project where I must implement a java class that implements the use of doubly linked lists. I have the LinkedList class finished with all my methods. I'm just unsure how to actually add node objects into the list. Here is my code so far with test at the bottom. Any help would be appreciated. Thanks
public class LinkedList {
private Node first;
private Node current;
private Node last;
private int currentIndex;
private int numElements;
public LinkedList() {
this.first = null;
this.last = null;
this.numElements = 0;
this.current = null;
this.currentIndex = -1;
}
private class Node {
Node next;
Node previous;
Object data;
}
public boolean hasNext() {
return (current != null && current.next != null);
}
public Object next() {
if (!this.hasNext()) {
throw new IllegalStateException("No next");
}
current = current.next;
return current.data;
}
public boolean hasPrevious() {
return (current != null && current.previous != null);
}
public Object previous() {
if (!this.hasPrevious()) {
throw new IllegalStateException("No previous");
}
current = current.previous;
return current.data;
}
int nextIndex() {
int index = numElements;
if (hasNext()) {
index = this.currentIndex + 1;
}
System.out.println(index + "The current index is " + current);
return index;
}
int previousIndex() {
int index = -1;
if (hasPrevious()) {
index = this.currentIndex - 1;
}
System.out.println(index + "The current index is " + current);
return index;
}
public void set(Object o) {
if (this.current == null) {
throw new IllegalStateException("No node found, cannot set.");
}
current.data = o;
}
public int size() {
return numElements;
}
public void add(Object o) {
Node newNode = new Node();
newNode.data = o;
if (first == null) {
first = newNode;
last = newNode;
newNode.next = null;
} else if (first != null) {
if (current == null) {
newNode.previous = null;
newNode.next = first;
first.previous = newNode;
first = newNode;
} else if (current == last) {
newNode.previous = current;
newNode.next = null;
current.next = newNode;
last = newNode;
} else {
newNode.previous = current;
newNode.next = current.next;
current.next.previous = newNode;
current.next = newNode;
}
}
current = newNode;
numElements++;
currentIndex++;
}
public void remove() {
if (current != null) {
if (current == first && current == last) {
first = null;
last = null;
} else if (current == last) {
current.previous = null;
last = current.previous;
} else if (current == last) {
current.previous.next = null;
last = current.previous;
} else {
current.previous.next = current.next;
current.next.previous = current.previous;
}
current = current.next;
numElements--;
}
}
}
import java.util.Scanner;
public class LinkedListTest {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String name;
int index;
LinkedList<Object> listOne = new LinkedList<Object>();
listOne.add(object o);
}
}
The posted class LinkedList looks functional to me.
Make sure that your test code does not confuse this class and java.util.LinkedList, which Java provides for you (It's a part of the existing Collections framework).
For clarity, I would recommend renaming your class to something like MyLinkedList
The following code works and the output is "0","2":
public class MyLinkedListTest {
public static final void main(String[] args) {
MyLinkedList list = new MyLinkedList();
System.out.println("Number of items in the list: " + list.size());
String item1 = "foo";
String item2 = "bar";
list.add(item1);
list.add(item2);
System.out.println("Number of items in the list: " + list.size());
// and so on...
}
}
I'd be surprised if your code compiled, since your class isn't actually generic. Just initialize it as LinkedList listOne = new LinkedList(); (no angle brackets).
As to actually adding elements, you just need an instance of some Object to add; anything will do (assuming your internal code works properly). Try this down at the end there:
Object objectToAdd = "Strings are Objects";
listOne.add(objectToAdd);
objectToAdd = new File("C:\\foo.bar"); // Or use any other Objects!
listOne.add(objectToAdd);
Think of numbered list and look at the relations between the elements
Say I have the list:
A
B
C
What do I have to do to the relations get the list:
A
B
NewNode
C
The new next node of B is NewNode
The new previous node of C is NewNode. So an insert function would want to know the immediate previous node or the immediate next node and then adjust the relationships.
Your LinkedList doesn't have generic types so you can't declare it as
LinkedList<Object> listOne = new LinkedList<Object>();
but rather as
LinkedList listOne = new LinkedList();
And now to add elements just use your add method
listOne.add("something");
listOne.add(1);//int will be autoboxed to Integer objects
Also if you want to add data from keyboard you can use something like
String line="";
do{
System.out.println("type what you want to add to list:");
line = keyboard.nextLine();
listOne.add(line);
}while(!line.equals("exit"));
The line
LinkedList<Object> listOne = new LinkedList<Object>();
won't compile unless you change your class declaration to
class LinkedList<T>
or alternately you can just write
LinkedList listOne = new LinkedLis();
After that you should be able to add objects to your list. However, you'll need to create an Object to add to it, listOne.add(object o); won't do--at the very least you'll have to write listOne.add(new Object()). (Your code does not instantiate an Object, there is no Object that you already have called o, and besides, object o does not mean anything in Java and would not compile.
As people have mentioned your list is not generic. However as they advise you to get rid of the parameter, you can also just add <Object> or <E> to your linked list implementation and leave your initialization of the list as it is.
So in your linked list class you should do something like:
public class LinkedList<E>
This will make sure when you're using LinkedList<Object> listOne = new LinkedList<Object>();, E will be covnerted to Object
Let's improve your test a little bit so that it becomes apparent where your problems are (if any) I commented out the call to the current() method since you have not included one. (I would leave this alone as it may confuse you.) The general idea would be to add items to the linked list and walk forward and backward through it checking the items with each step.
public class LinkedListTest {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String name;
int index;
LinkedList listOne = new LinkedList();
//Initially we should be empty so we are positioned
// at both the beginning and end of the list
assert listOne.size() == 0 :"List should be empty";
assert listOne.hasPrevious()==false: "Should be at the beginning of the list";
assert listOne.hasNext()==false : "Should be at the end of the list";
Object firstNode = "I am the first node";
listOne.add(firstNode); //we've added something
//I left this commented out since you don't have a current() method.
// assert firstNode == listOne.current() : "Our current item should be what we just added";
assert listOne.hasPrevious()==false : "Should not have moved forward in our list yet";
assert listOne.hasNext()==true : "should have an item after our current";
assert listOne.size() == 1 : "Should only have one item in the list";
Object secondNode = "I am the second node";
listOne.add(secondNode);
assert listOne.size() == 2 : "Should only have two items in the list";
assert firstNode == listOne.next() : "1st call to next should return the 1st node";
assert listOne.hasPrevious()==true : "We should be positioned after the 1st node";
assert listOne.hasNext()==true : "We should be positioned before the 2nd node";
}
}
I need to create a delete method inside of a doubly Linked List. I am having trouble, as I think that I need to have 4 cases.
if the list is empty
if the node being deleted is the head
if the node is the tail
if the node is somewhere in the middle of the list
This is the code that I have so far.
public void delete(Node n) {
if (head == null) {
System.out.println("the list is empty");
} else if (head != null) {
head = n;
Node newHead = n.next;
newHead = head;
} else if (n.next == null) {
Node beforeTail = n.previous;
beforeTail.next = null;
} else if (n.next != null || n.previous != null) {
Node inFront = n.previous;
Node inBack = n.next;
inFront.next = inBack;
inBack.previous = inFront;
} else {
System.out.println("error");
}
}
Here is the test program:
public class TestLL {
public static void main(String[] args){
/*Create a bunch of free standing nodes */
Node n1= new Node(new Integer(11));
Node n2= new Node(new Integer(12));
Node n3= new Node(new Integer(13));
Node n4= new Node(new Integer(14));
Node n5= new Node(new Integer(15));
Node n6= new Node(new Integer(16));
Node n7= new Node(new Integer(17));
/* link them */
LL myLL =new LL();
myLL.printList(); // prints "empty list"
myLL.add(n1); //11
myLL.add(n3); //13
myLL.add(n5); //15
myLL.add(n2); //12
myLL.add(n7); //17
myLL.printList(); //should print 11, 13, 15, 12, 17; one per line
System.out.println();
myLL.delete(n3);
myLL.addAfter(n4,n1);
myLL.printList(); //should print 11,14,15,12,17 one per line
System.out.println();
myLL.delete(n7);
myLL.delete(n2);
myLL.printList();//should print 11,14,15 one per line
}
}
I am not really sure what to do at all. Also I cannot use any methods already in Java.
Hard to say but the LL approach looks wrong. Generally working with an LL will not require any awareness of "nodes" as it is up to the implementation to handle all the linking details. The code that calls should be passing in objects of it's choice.
I would expect usage of LL to look like this.
LL myLL = new LL();
myLL.add(new Integer(1));
myLL.add(new Integer(2));
// etc
myLL.remove(new Integer(1));
During a call to add a new Node (internal class to LL) would get created and appended to the end. Remove/delete would search the LL and remove the first instance that matches the passed in object.
e.g. A rough sketch of an LL implementation.
class LL
{
private Node headNode = null;
public void add(Object item)
{
// construct a new node
// iterate to the last node and add new node to the end
}
public boolean remove(Object item)
{
// starting at the head node search the list until a node with the matching item is found
// update the node pointers to "remove" the node
}
class Node
{
Node nextNode;
Node prevNode;
Object item;
}
}
I won't fill in the implementations as this is homework ;) but you are right in there are a few cases to handle
List is empty
Item is not present
Item is in the head node
Item is in another node
Good luck!