Inspect a Queue to print all elements - java

I am currently revising for my programming exam and I have came across a question from a past paper that has me rather confused.
I have two classes, Queue and Node, shown below.
The question states that I have to extend the behaviour of the Queue class by adding the necessary code to the inspectQueue method that prints to the console all the data stored within the queue.
The only solution I can think of, and it is very weak, is to have a simple ArrayList and every time an element is enqueued/dequeued then add/remove the node to/from the list.
Is there a better solution that I am glossing over? I'd really appreciate some guidance.
I've commented the code where I have implemented my "solution" the rest of the code is how it appears in the exam paper.
Thanks for your time.
Queue.java
public class Queue {
protected Node head;
protected Node last;
//added by me
private ArrayList<Node> nodes = new ArrayList<Node>();
//end my add
public boolean isEmpty() {
return (this.head == null);
}
public void enqueue(Object d) {
Node n = new Node();
n.setData(d);
nodes.add(n); //added by me
if (this.isEmpty()) {
head = n;
last = n;
}
else {
last.setNext(n);
last = n;
}
}
public Object dequeue() {
if(this.isEmpty()) {
this.last = null;
return null;
}
else {
Node h = this.head;
nodes.remove(h); //added by me
head = h.getNext();
return h.getData();
}
}
public Object peek() {
if(this.isEmpty()) {
return null;
}
else {
Node t = this.head;
return t.getData();
}
}
public void clearQueue() {
this.head = null;
this.last = null;
}
public void inspectQueue() {
//added by me (all below)
System.out.println("Inspecting Queue: (contains " + nodes.size() + " nodes)");
for(Node n : nodes) {
System.out.println(n.getData());
}
}
}
Node.java
public class Node {
protected Object data;
protected Node next;
public void setNext(Node e) {
this.next = e;
}
public Node getNext() {
return this.next;
}
public void setData(Object d) {
this.data = d;
}
public Object getData() {
return this.data;
}
}

Your nodes form a linked list, so just do
public void inspectQueue() {
Node n = head;
while (n != null) {
System.out.println(n.getData());
n = n.getNext();
}
}

This is a very basic data structure, called a LinkedList. In the your code for the Node class you can see the following:
protected Node next;
This means that every Node also holds a reference to the next Node in the list. If this Node is null, there are no more elements in the list. Knowing this, you can loop somewhat like this:
Node currentNode = this.head;
while(currentNode != null) {
System.out.println(currentNode.getData().toString());
currentNode = currentNode.getNext();
}
This eliminates the need for an ArrayList to store your references.
The LinkedList is a VERY frequently used data structure and very important to understand. If you have any questions, just go ahead and ask!
If you also want to have the size, keep a counter along, increment it each time you call getNext(), and print the size after the for loop.

You don't need the array, you have that information stored within the Node next property:
public void inspectQueue() {
Node current = head;
while(current != null) {
System.out.println(n.getData());
current = current.getNext();
}
}
That data structure is called linked list.

The simpler solution is to start with queue.head and traverse the linked list of nodes using node.next, printing the data as you go along.

Related

How can I get my next pointer of my LinkedList class without using built in function?

I'm newbie in programming and I'm practicing a Java Programming Language. I was having a rough day in finding the solution of my program because I cannot get my "next" pointer and I really want to print my last value. Could someone help me to fix this and explain to me? Thank you in advance. Here's my code.
Note: The output of my program is 5.
public class Node {
private int data;
private Node next;
public Node (int data){
this.data = data;
}
public int getData() {
return this.data;
}
public void setNext(Node n) {
this.next = n;
}
public Node getNext() {
return this.next;
}
}
public class LinkedList {
private static Node head, next;
public LinkedList (int data) {
head = new Node (data);
}
public void addLast(int data) {
Node n = new Node (data);
if (head == null) {
head = n;
}
else {
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
Node t = temp.getNext();
t = n;
}
}
public void printList() {
head.setNext(next);
while (head.getNext() != null) {
System.out.println(head.getData());
head = head.getNext();
}
System.out.println(head.getData());
}
public static void main(String[] args) {
LinkedList l = new LinkedList(5);
l.addLast(7);
l.printList();
}
}
I suggest following two amendment to your code.
with following else block in addLast method.
Node temp = head;
temp.setNext(next); // this line causing the next object to be set to null all the time. commenting this line will help in making sure the follwing loop reaches to end of the list, otherwise the while loop will always exit without any iteration.
while (temp.getNext() != null) {
temp = temp.getNext();
}
Node t = temp.getNext();
t = n; // this will also not change the linking. Its basically assigned a new value to t.
use following suggestion
Node temp = head;
while (temp.getNext() != null) {
temp = temp.getNext();
}
// now we reached end of list and temp.next is null.
// assign newly createdd node to temp.next
temp.setNext(n);
While iterating the element in printList same problem exist as mentioned in point 1. try to use following suggestion for printList method.
// head.setNext(next); // This line will always set head.next to null and whole list will be lost. Instead of this use following line
Node temp = head;
while (temp.getNext() != null) { // here if you use head its position will move to end. So use temp variable for iteration
System.out.println(temp.getData());
temp= temp.getNext();
}
System.out.println(temp.getData());
You may also need to study list iteration algorithm to have better understanding.
I've made some amendments to make your code work. The If statement in your addLast method:
if (head == null) {
is redundant since your LinkedList can only be initialized by passing some data, hence head will never be null, it will always point to the Node containing data
Also the line
head.setNext(next);
in your printList() was problematic, it was always pointing to null
public class LinkedList {
private static Node head, next;
public LinkedList(int data) {
head = new Node(data);
}
public void addLast(int data) {
Node n = new Node(data);
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(n);
}
public void printList() {
while (head.getNext() != null) {
System.out.println(head.getData());
head = head.getNext();
}
System.out.println(head.getData());
}
public static void main(String[] args) {
LinkedList l = new LinkedList(5);
l.addLast(7);
l.printList();
}
}
TL;DR:
You set null as the next node in your printList() method;
Your addLast does not work either (you do not set the next node (see details below);
You should never set the node (or do any logical alteration whatsoever) in your print method. Print must just print, as the name suggests, and it should not contain any side-effect, amending your data structure. That is: you have to clearly separate your concerns.
In your current addLast, you do:
public void addLast(int data) {
Node n = new Node (data);
if (head == null) {
head = n;
}
else {
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
Node t = temp.getNext();
t = n;
}
}
which means, that when your temp's next node is null, you never add the Node you instantiate with your int argument.
Change the else block as follows:
else {
Node temp = head;
temp.setNext(next);
while (temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(n);
//two redundant lines removed
}
Correspondingly, remove head.setNext(next); (and possibly unnecessary System.out.println() statement) from your printList() method.
P. S. I would really recommend you to spend some time on the Linked List Data Structure (Data Structure, and not the Java code), as your current design, shows that you need to have a better grasp of it.

Java Simple Linked List

I am taking a course in Data Structures at University and I am having troubles understanding why my Singly Linked List is not following FIFO algorithm.
Here is my Node/PSVM class:
public class Node {
protected int data;
protected Node next;
Node(int element){
this.data = element;
next = null;
}
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.addElement(300);
ll.addElement(600);
ll.addElement(900);
ll.addElement(1200);
ll.printList();
}
}
This is my Linked List Class:
public class LinkedList {
// create a reference of type node to point to head
Node head;
// keep track of the size of ll
int size = 0;
void printList() {
Node n = head;
for (int i = 0; i < llSize(); i++) {
System.out.print(n.data + " ");
n = n.next;
}
System.out.println("");
}
int llSize() {
return this.size;
}
boolean isEmpty() {
return size == 0;
}
void addElement(int element) {
if (isEmpty()) {
head = new Node(element);
} else {
Node nNode = new Node(element);
Node current = head;
while(current.next != null){
current = current.next;
}
current.next = nNode;
}
this.size++;
}
}
Sorry in advance if this is a basic question/problem. I have asked my professor and she sent me a YouTube link which really didn't help.
Thank you for your time.
The code has no bugs.
For the list to behave as FIFO, nodes will be added to one end and deleted from the opposite end.
Therefore, you will have to implement a delete operation. You can maintain separate reference to the head and tail node.

Multiple the contents of the Nodes Linked List

I have a programming challenge that is to recursively multiple the data in the nodes of the list following it. For example
2 - 4 - 6 - 8
will be
384 - 192- 48 - 8
This is what I have done so far in the void product method. I keep getting a null pointer exception. What is wrong with my product method
class Node
{
private int data;
private Node next;
private Node prev;
public Node(int newData,Node newNext,Node newPrev)
{
data = newData;
next = newNext;
prev = newPrev;
}
public int getData()
{
return data;
}
public void setData(int otherData)
{
this.data = otherData;
}
public Node getNext()
{
return next;
}
public Node getPrev()
{ return prev;
}
public void setNext(Node newNext)
{
next = newNext;
}
public void setPrev(Node newPrev)
{
prev = newPrev;
}
}
class LinkedList
{
private Node head;
private Node start;
private Node end;
public LinkedList()
{
head = null;
start = null;
end = null;
}
public void insert(int data)
{
Node newNode = new Node(data,null,null);
if(start == null)
{
start = newNode;
end = start;
}
else
{
newNode.setPrev(end);
end.setNext(newNode);
end = newNode;
}
}
public void product()
{
product(head);
}
public void product(Node head)
{
Node next = head.getNext();
if(head == null)
{
return;
}
else
{
int data = head.getData() * next.getData();
head.setData(data);
product(head.getNext());
}
}
}
You are calling head.getNext() and next.getData() without checking if either of head or next is null, so the program will crash when processing the last node. Even so, you are only multiplying two consecutive items and not accumulating the product.
You can make use of the function's return value to accumulate the right answer:
public void product()
{
product(head);
}
public int product(Node head)
{
if(head == null)
{
return 1;
}
else
{
int data = head.getData() * product(head.getNext());
head.setData(data);
return data;
}
}
I didn't check the logic of your entire code, but the first thing that comes to mind is that you assign:
Node next = head.getNext();
And then you check if(head == null) but what about if(next == null)?
If it is, then you have your error right here:
next.getData()
Because head can be non-null, but its next can certainly be null.
The correct course of action would be to first check if(head == null), then assign Node next = head.getNext();, and then check if(next == null).
First, you call getNext() method on head and then you check if head is null? That is clearly wrong.
You should first check if head is null. Then you should check if next is null.
Also I don't think your recursion will compute the product correctly, since you multiply the data in your current head with what's currently in next - you could achieve that with a simple loop.
Instead you should call product(next) first and compute product later. Like this (didn't test it though)
public void product(Node head)
{
if (head == null)
return;
Node next = head.getNext();
product(next);
if (next != null)
{
int data = head.getData() * next.getData();
head.setData(data);
}
}

Java Using Nodes with LinkedList

I've been working through some standard coding interview questions from a book I recently bought, and I came across the following question and answer:
Implement an algorithm to find the nth to last element in a linked list.
Here's the provided answer:
public static LinkedListNode findNtoLast(LinkedListNode head, int n) { //changing LinkedListNode to ListNode<String>
if(head == null || n < 1) {
return null;
}
LinkedListNode p1 = head;
LinkedListNode p2 = head;
for(int j = 0; j < n-1; ++j) {
if(p2 == null) {
return null;
}
p2 = p2.next;
}
if(p2 == null) {
return null;
}
while(p2.next != null) {
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
I understand the algorithm, how it works, and why the book lists this as its answer, but I'm confused about how to access the LinkedListNodes to send as an argument to the method. I know that I'd have to create a LinkedListNode class (since Java doesn't already have one), but I can't seem to figure out how to do that. It's frustrating because I feel like I should know how to do this. Here's something that I've been working on. I'd greatly appreciate any clarification. You can expand/comment on my code or offer your own alternatives. Thanks.
class ListNode<E> {
ListNode<E> next;
E data;
public ListNode(E value) {
data = value;
next = null;
}
public ListNode(E value, ListNode<E> n) {
data = value;
next = n;
}
public void setNext(ListNode<E> n) {
next = n;
}
}
public class MyLinkedList<E> extends LinkedList {
LinkedList<ListNode<E>> list;
ListNode<E> head;
ListNode<E> tail;
ListNode<E> current;
ListNode<E> prev;
public MyLinkedList() {
list = null;
head = null;
tail = null;
current = null;
prev = null;
}
public MyLinkedList(LinkedList<E> paramList) {
list = (LinkedList<ListNode<E>>) paramList; //or maybe create a loop assigning each ListNode a value and next ptr
head = list.getFirst();
tail = list.getLast(); //will need to update tail every time add new node
current = null;
prev = null;
}
public void addNode(E value) {
super.add(value);
//ListNode<E> temp = tail;
current = new ListNode<E>(value);
tail.setNext(current);
tail = current;
}
public LinkedList<ListNode<E>> getList() {
return list;
}
public ListNode<E> getHead() {
return head;
}
public ListNode<E> getTail() {
return tail;
}
public ListNode<E> getCurrent() {
return current;
}
public ListNode<E> getPrev() {
return prev;
}
}
How can the LinkedListNode head from a LinkedList?
Update: I think part of my confusion comes from what to put in the main method. Do I need to create a LinkedList of ListNode? If I do that, how would I connect the ListNodes to each other? How would I connect them without using a LinkedList collection object? If someone could show me how they would code the main method, I think that would put things into enough perspective for me to solve my issues. Here's my latest attempt at the main method:
public static void main(String args[]) {
LinkedList<ListNode<String>> list = new LinkedList<ListNode<String>>();
//MyLinkedList<ListNode<String>> list = new MyLinkedList(linkedList);
list.add(new ListNode<String>("Jeff"));
list.add(new ListNode<String>("Brian"));
list.add(new ListNode<String>("Negin"));
list.add(new ListNode<String>("Alex"));
list.add(new ListNode<String>("Alaina"));
int n = 3;
//ListIterator<String> itr1 = list.listIterator();
//ListIterator<String> itr2 = list.listIterator();
LinkedListNode<String> head = new LinkedListNode(list.getFirst(), null);
//String result = findNtoLast(itr1, itr2, n);
//System.out.println("The " + n + "th to the last value: " + result);
//LinkedListNode<String> nth = findNtoLast(list.getFirst(), n);
ListNode<String> nth = findNtoLast(list.getFirst(), n);
System.out.println("The " + n + "th to the last value: " + nth);
}
In an attempt to connect the nodes without using a custom linked list class, I have edited my ListNode class to the following:
class ListNode<E> {
ListNode<E> next;
ListNode<E> prev; //only used for linking nodes in singly linked list
ListNode<E> current; //also only used for linking nodes in singly linked list
E data;
private static int size = 0;
public ListNode() {
data = null;
next = null;
current = null;
if(size > 0) { //changed from prev != null because no code to make prev not null
prev.setNext(this);
}
size++;
}
public ListNode(E value) {
data = value;
next = null;
current = this;
System.out.println("current is " + current);
if(size > 0) {
prev.setNext(current);//this line causing npe
}
else
{
prev = current;
System.out.println("prev now set to " + prev);
}
size++;
System.out.println("after constructor, size is " + size);
}
public ListNode(E value, ListNode<E> n) {
data = value;
next = n;
current = this;
if(size > 0) {
prev.setNext(this);
}
size++;
}
public void setNext(ListNode<E> n) {
next = n;
}
}
As is right now, the program will run until it reaches prev.setNext(current); in the single argument constructor for ListNode. Neither current nor prev are null at the time this line is reached. Any advice would be greatly appreciated. Thanks.
You don't actually need a separate LinkedList class; the ListNode class is a linked list. Or, to state it differently, a reference to the head of the list is a reference to the list.
The use of head, tail, current, prev in the sample code you posted has come from a double-linked list which is a data type that has links in both directions. This is more efficient for certain types of applications (such as finding the nth last item).
So I would recommend renaming your ListNode class to LinkedList and renaming next to tail.
To add a new item to the list you need a method that creates a new list with the new item at it's head. Here is an example:
class LinkedList<E> {
...
private LinkedList(E value, LinkedList<E> tail) {
this.data = value;
this.tail = tail;
}
public LinkedList<E> prependItem(E item) {
return new LinkedList(item, this);
}
}
Then to add a new item i to list you use list = list.prependItem(i);
If for some reason you need to always add the items to the end, then:
private LinkedList(E value) {
this.data = value;
this.tail = null;
}
public void appendItem(E item) {
LinkedList<E> list = this;
while (list.tail != null)
list = list.tail;
list.tail = new LinkedList<>(item);
}
However this is obviously pretty inefficient for long lists. If you need to do this then either use a different data structure or just reverse the list when you have finished adding to it.
Incidentally, an interesting side effect of this is that a reference to any item in the list is a reference to a linked list. This makes recursion very easy. For example, here's a recursive solution for finding the length of a list:
public int getLength(LinkedList list) {
if (list == null) {
return 0;
} else {
return 1 + getLength(list.getTail());
}
}
And using this a simple (but very inefficient!) solution to the problem you provided - I've renamed the method to make its function more obvious:
public LinkedList getTailOfListOfLengthN(LinkedList list, int n) {
int length = getLength(list);
if (length < n) {
return null;
} else if (length == n) {
return list;
} else {
return getTailOfLengthN(list.getTail(), n);
}
}
And to reverse the list:
public LinkedList<E> reverse() {
if (tail == null) {
return this;
} else {
LinkedList<E> list = reverse(tail);
tail.tail = this;
tail = null;
return list;
}
}
As I hope you can see this makes the methods a lot more elegant than separating the node list classes.
Actually you have created a linked list with you class ListNode.
A linked list is made of a node and a reference to another linked list (see the recursion?).

Homework - Java - Selection Sort on my created double-linked list

My assigment is to create my own linked list class (I can't use Java's LinkedList class) and implement a selection sort on it by swapping pointers rather than data.
I've created a double-linked MyLinkedList class, but I'm having trouble with the sort method. I've tried a number of things but nothing has worked - not even anything that would make sense to post here for correction. (I do know that I need to use at least one temp Node.) It has to be a selection sort.
I'm not looking for someone to code it for me, necessarily; I'm hoping someone can help me with an algorithm that I can then turn into code myself. Any help is appreciated greatly.
Here's how I've implemented the MyLinkedList class and the associated Node class:
public class MyLinkedList
{
private Node head;
private int count;
public MyLinkedList()
{
head = new Node(null);
count = 0;
}
public void add(String line)
{
Node temp = new Node(line);
Node current = head;
while (current.getNext() != null)
{
current = current.getNext();
}
temp.setLine (line); // not sure this is how to do it
current.setNext(temp);
temp.setPrev(current);
count++;
}
public void displayList()
{
Node current = head;
for (int i = 0; i < count; i++)
{
current = current.getNext();
System.out.println(current.getLine());
}
}
public void sortList()
{
Node start = head;
Node index = start;
Node min = start;
Node temp1, temp2;
while (start.getNext() != null)
{
index = index.getNext();
if (index.getLine().compareTo(min.getLine()) < 0)
{
min = index;
}
//swap - HELP, PLEASE :-)
{
// Algorithm???
}
}
}
public int size()
{
return count;
}
private class Node
{
String textLine;
Node next;
Node prev;
public Node()
{
textLine = null;
next = null;
prev = null;
}
public Node (String line)
{
textLine = (line);
next = null;
prev = null;
}
public Node (String line, Node node1, Node node2)
{
textLine = line;
prev = node1;
next = node2;
}
public String getLine()
{
return textLine;
}
public Node getNext()
{
return next;
}
public Node getPrev()
{
return prev;
}
public void setLine(String line)
{
textLine = line;
}
public void setNext(Node nextNode)
{
next = nextNode;
}
public void setPrev(Node prevNode)
{
prev = prevNode;
}
}
}
It may get confusing if an empty MyLinked List has a node in it even if it's just one with null prev, next and data, so you need to be careful of that MyLinkedList constructor - it would probably be much easier if it read simply head = null;.
Also it would be useful if a MyLinked List had a tail node as well to save you following the chain to the end to find where add should put a new Node.
After that, I think the problem is that you haven't noticed you need two loops: one to work your way through the list to keep track of where the unsorted nodes start, and one to find the smallest node from thereon. You also need to write a swap method for Node so that you can write something like this untested pseudocode that just happens to look a lot like Java
for (index = head; index != null; index = index.getNext()) {
min = index;
for (test = min.getNext(); test != null; test = test.getNext) {
if (test.getLine().compareTo(min.getLine()) < 0)
min = test;
}
if (min != index) {
swap(index, min);
index = min;
}
}
and swap would look roughly like
public void swap(Node other)
{
Node temp;
temp = next;
next = other.getNext();
other.setNext(temp);
temp = prev;
prev = other.getPrev();
other.setPrev(temp);
other.getNext().setPrev(this);
other.getPrev().setNext(this);
this.getNext().setPrev(other);
this.getPrev().setNext(other);
}
Note again this is completely untested and hasn't even seen a compiler.
Make sure to think about special cases like when the list is empty or has only one element in it, and when there is only one node left unsorted in the list.
I couldn't leave this without pointing out that swap is actually a lot more complex than that. I've added a few lines to correct the pointers in the nodes before and after the nodes to be swapped. You also need to consider:
Whether either of the nodes that are swapped are at the end of the list, in which case the head (and tail if you have one) of the list will need to be updated instead of the pointers in the adjacent nodes. That's fairly obvious.
Whether the nodes to be swapped are next to each other in the list, when if you apply the normal algorithm you get nodes pointing to themselves. That's less obvious.

Categories

Resources