I am following Coursera Algorithm 1 course, and right now implementing Queues using linked list, but getting a NullPointerException. Please help me out.
package algo_packages;
public class QueueLinkedList {
private Node first, last;
public class Node{
String item;
Node next;
}
public QueueLinkedList(){
first = null;
last = null;
}
public boolean isEmpty(){
return first == last;
}
public void enqueue(String item){
Node oldLast = last;
last = new Node();
last.item = item;
last.next = null;
if(isEmpty()){
first = last;
}
else {
oldLast.next = last;
}
}
public String dequeue(){
String item = first.item;
first = first.next;
if (isEmpty()) last = null;
return item;
}
}
I am getting the exception at:
oldLast.next = last;
oldLast.next caused the NullPointerException when I tried to debug the program.
The first time you enqueue an item isEmpty() returns false, since it checks if first==last, but first is still null and last is no longer null (since you already assigned the new Node to it). This brings you to access oldLast.next when oldLast is null, hence the NullPointerException.
A possible fix :
public void enqueue(String item)
{
Node oldLast = last;
Node newNode = new Node();
newNode.item = item;
newNode.next = null;
if(isEmpty()) { // last is not assigned yet, so isEmpty returns correct result
last = newNode;
first = last;
} else {
last = newNode;
oldLast.next = last;
}
}
When you check isEmpty() it always returns false for enqueue because you are setting last to a new Node() which will never equal first. You don't need to check if list isEmpty() because if list is empty then first == last so you don't need to assign first = last because they are already equal. Try this:
public void enqueue(String item){
Node oldLast = last;
last = new Node();
last.item = item;
last.next = null;
if(oldLast != null)
{
oldLast.next = last;
}
else
{
first = last;
}
}
Related
I want to emulate a queue by using linked lists in Java. My general schema is to have a list of nodes, with two nodes that points to the first and last element of my queue. When I perform the dequeue() I want to get ride of the first element. So far what I have done is the following:
public class Node {
public Object e;
Node next;
public Node(Object e) {
this.e = e;
}
}
public class Queue {
Node queueList;
Node first, last;
int count;
public void enQueue(Object n) {
Node temp = new Node(n);
temp.next = last;
last = temp;
if (queueList == null) {
first = temp;
}
count++;
queueList=temp;
}
public Object deQueue() {
Node previous = last;
Node current = last.next;
Object num = null;
if (count == 0)
System.out.println("empty queue");
else {
while (current.next != null) {
previous = previous.next;
current = current.next;
}
num = first.e;
first = previous;
count--;
}
return num;
}
public void print() {
Node current = last;
while (current != null) {
System.out.println(current.e);
current = current.next;
}
}
}
I do not want to use double linked lists, so for the dequeue() operation what I do is to traverse my list with two pointers like this:
So when the current.next points to a null, I want that previous to be the first node. The problem that I got is when I print the elements of my queue it stills prints me: 10,15,5,18, but the 18 value is not deleted. Any help?
Thanks
There are basically two things I would correct in your code.
First of all, the Queue field was not assigned any value, and I also doubt its utility, basically its logic can be applied using first
public void enQueue(Object n) {
Node temp = new Node(n);
temp.next = last;
last = temp;
if (first == null) { <--- HERE
first = temp;
}
count++;
}
The second one is that you never put the value of the new head of the queue next to null which is needed in that case
public Object deQueue() {
Node previous = last;
Node current = last.next;
Object num = null;
if (count == 0) System.out.println("empty queue");
else {
while (current.next != null) {
previous = previous.next;
current = current.next;
}
num = first.e;
first = previous;
first.next = null; <--- HERE
count--;
}
return num;
}
recently I read an implementation of enqueue in java, and become really confused at the meaning of couple lines in it.
public void enqueue(String item) {
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if (isEmpty())
first = last;
else
oldlast.next = last;
}
I'm confused that since oldlast is created only in this method, why would we state "oldlast.next= last" after else? it would be destroyed after the method, right?
Also, if we only add last to first when first is empty, would it be buggy if I add two nodes then delete two? Since only one node is added to first and I want to delete two would there be exception?
public void enqueue(String item)
{
Node oldlast = last; // last points to some Node_A, oldlast also will point at the same object
last = new Node(); // Now last points to new object (Node_B), old last still points to Node_A
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else oldlast.next = last;
}
oldLast is not created, it simply is a reference to the same object as last.
public void enqueue(String item)
{
Node oldlast = last; // oldLast -> nodeX, last -> nodeX
last = new Node(); // oldLast -> nodeX, last -> nodeY
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else oldlast.next = last; // change the nodeX
}
The following code ensures that elements are inserted in a linked list in a sorted manner.
After understanding the logic behind this i decided to test it on my own. However when i wrote my version of the code it as follows.
public class SortedList {
private Node first;
public SortedList() {
first = null;
}
public boolean isEmpty() {
return first == null;
}
public void insert(int j) {
Node newNode = new Node(j);
Node previous = null;
Node current = first;
while (current != null && j > current.iData) {
previous = current;
current = current.next;
}
if (previous == null)
first = newNode;
else
newNode.next = current;
previous.next = newNode;
}
public Node remove() {
Node temp = first;
first = first.next;
return temp;
}
public void displayList() {
System.out.println("First to -----> Last");
Node current = first;
while (current != null) {
current.display();
current = current.next;
}
}
}
NODE CLASS
public class Node {
public int iData;
public Node next;
public Node(int id) {
iData = id;
}
public void display() {
System.out.println(iData + " ");
}
}
TEST CLASS
public class SortedListApp {
public static void main(String[] args) {
SortedList list = new SortedList();
list.insert(20);
list.insert(40);
list.displayList();
list.insert(10);
list.insert(30);
list.insert(50);
list.displayList();
list.remove();
list.displayList();
}
}
The only difference between the two is that in my version when the while loop terminates. I first set the next value of the newNode to the current and then i set the next value of previous to the new Node. In the original code posted they have it reversed. For some reason this throws Null Pointer Exception. I am wondering why?
From what i understand, once the new node finds its place for insertion. We have reference to the previous node and the current node and we are trying to insert the new node in the middle of the previous and the current. Therefore what i do is set the next of the new Node to the current and then i set the next of previous node to the new node.
Please tell me where i am wrong.
Trace through this code when previous is null:
if (previous == null)
first = newNode;
else
newNode.next = current;
previous.next = newNode;
Notice that you never actually changed previous in the if statement, so this will try to write the next field of null, causing the crash.
If previous is null, then you need to prepend the node to the list, and in that case all you need to do is set first to newNode and make newNode's next pointer point to the old list. In the case where previous isn't null, then you need to do two rewirings: you need to make previous.next point to the new node and for the new node's next pointer to point to the current node. You can fix this by writing
if (previous == null) {
newNode.next = current;
first = newNode;
}
else {
newNode.next = current;
previous.next = newNode;
}
Equivalently:
newNode.next = current;
if (previous == null)
first = newNode;
else
previous.next = newNode;
Hope this helps!
I Know this topic has been beat to death but I'm really struggling with implementing these two add methods to a linked list. addFirst and addLast both work when called by themselves but when I call addFirst("foo") and addLast("bar") the add last removes anything previously added to the list. add first is supposed to add an item to the beginning of the list, and add last is supposed to append it to the end.
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item> {
private int N;
private Node first;
private Node last;
//create linked list
private class Node
{
String item;
Node next;
Node previous;
}
public Deque() // construct an empty deque
{
N = 2;
first = new Node();
last = new Node();
//link together first and last node;
first.next = last;
last.previous = first;
last.item = "Last";
first.item = "First";
}
public boolean isEmpty() // is the deque empty?
{
return first == null;
}
public int size() // return the number of items on the deque
{
return N;
}
public void addFirst(Item item) // insert the item at the front
{
Node nextElement = new Node();
nextElement.item = (String)item;
nextElement.next = first.next;
nextElement.previous = first;
first.next = nextElement;
N++;
}
public void addLast(Item item) // insert the item at the end
{
Node newLast = new Node();
newLast.item = (String)item;
newLast.next = last;
newLast.previous = last.previous;
last.previous.next = newLast;
last.previous = newLast;
N++;
}
public void printList()
{
Node print = first;
for (int i = 0; i < N; i++)
{
System.out.print(print.item);
print = print.next;
}
System.out.println("");
}
Seems like you're getting yourself confused. Generally, if your doing something.next.next or similar, a warning should go off in your head. You'd also be well served to provide a constructor that could take the item instead of the addition statement in the method.
public void addLast(Item item) // insert the item at the end
{
Node newLast = new Node();
newLast.item = (String)item;
if (isEmpty()) {
first = newLast;
} else {
last.next = newLast;
newLast.previous = last;
}
last = newLast;
N++;
}
As far as addFirst is concerned, so you don't inadvertently get bad advice, it would go something like this...
public void addFirst(Item item) {
Node newFirst = new Node();
newFirst.item = (String)item;
if (isEmpty()) {
last = newFirst;
} else {
first.previous = newFirst;
}
newFirst.next = first;
first = newFirst;
N++;
}
The addfirst method is missing updating one of the pointers
public void addFirst(Item item) // insert the item at the front
{
Node nextElement = new Node();
nextElement.item = (String)item;
nextElement.next = first.next;
nextElement.previous = first;
first.next.previous = nextElement; //ADDED HERE
first.next = nextElement;
N++;
}
I think this question is answered with one simple link - you're re-inventing the wheel which is always a bad idea, no matter what educational purposes your goals serve.
Use the Deque interface.
In the following code, I am trying to understand one thing in the insertFirst() method that
Why is the last statement first =newLink; and not first.next=new Link;
Will it be wrong? Isn't there a "next" in first?
I know this code is right and I know that a node needs to be inserted at the beginning and all, but I need help understanding just one statement.
Is first =newLink; and first.next=new Link; not the same thing?
public class LinkedList {
private Link first;
public LinkedList()
{
first = null;
}
public boolean isEmtpy()
{
return(first==null);
}
public void insertFirst(int id, int dd)
{
Link newLink=new Link(id,dd);
newLink.next=first;
first =newLink;
}
}
No, it's right: the list inserts new links at the beginning. The old "first" becomes the new link's "next", and the new link is the new "first".
Why is the last statement first =newLink; and not first.next=new Link;
Because you're inserting a new first element and the "next" element is the old first element, which was set on the previous line.
Is first =newLink; and first.next=new Link; not the same thing?
No. first is the first and first.next is the second.
This is because you want to put new element to the beginning, so you must set new element to the head of list and this element should point on "old-head", and then you have:
new_elemnt->old_head->...
LinkedList::first is not a guard element. It really points to the first element of the list. If LinkedList::first == null, then the list is empty. If Link::next == null, then it's the last element (the null is called a guard element in this case).
Simple example of SingleLinkedList in Java
package com.ds;
public class SingleLinkedList {
private Node head;
public static void main(String[] args) {
SingleLinkedList linkedList = new SingleLinkedList();
linkedList.insert(5);
linkedList.insert(15);
linkedList.insert(45);
linkedList.insert(55);
linkedList.insert(58);
linkedList.insert(25);
// Print value of Single Linked list.
linkedList.print();
// delete node from tail side.
linkedList.delete();
linkedList.delete();
linkedList.delete();
linkedList.delete();
linkedList.delete();
/*linkedList.delete();
linkedList.delete();
linkedList.delete();
linkedList.delete();*/
linkedList.print();
}
SingleLinkedList() {
head = null;
}
void insert(int val) {
Node temp = new Node();
temp.data = val;
temp.next = null;
if (head == null) {
head = temp;
} else {
Node k = head;
while (k.next != null) {
k = k.next;
}
k.next = temp;
}
}
// delete from tail.
void delete() {
// if it's first node
if (head == null || head.next == null) {
head = null;
} else {
Node n = head;
Node t = head;
while (n.next != null) {
t = n;
n = n.next;
}
t.next = null;
}
}
void print() {
Node k = head;
while (k != null) {
System.out.println(k.data);
k = k.next;
}
}
Node reverse() {
Node h = head;
Node p = null;
Node t = null;
while (h != null) {
t = h.next;
h.next = p;
p = h;
h = t;
}
return p;
}
class Node {
private int data;
private Node next;
}
}