• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How the insertAtBack sets the next for head?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package oops;

class Node { // (1)
private Object data; // Data
private Node next; // Next node

// Constructor for initializing data and reference to the next node.
Node(Object data, Node next) {
this.data = data;
this.next = next;
}

// Methods
public void setData(Object obj) { data = obj; }
public Object getData() { return data; }
public void setNext(Node node) { next = node; }
public Node getNext() { return next; }
}

class LinkedList { // (2)
protected Node head = null;
protected Node tail = null;

// Methods
public boolean isEmpty() { return head == null; }
public void insertInFront(Object dataObj) {
if (isEmpty()) head = tail = new Node(dataObj, null);
else head = new Node(dataObj, head);
}
public void insertAtBack(Object dataObj) {
if (isEmpty())
head = tail = new Node(dataObj, null);
else {
tail.setNext(new Node(dataObj, null));
tail = tail.getNext();
}
}
public Object deleteFromFront() {
if (isEmpty()) return null;
Node removed = head;
if (head == tail) head = tail = null;
else head = head.getNext();
return removed.getData();
}
}

class QueueByAggregation { // (3)
private LinkedList qList;

// Constructor
QueueByAggregation() {
qList = new LinkedList();
}

// Methods
public boolean isEmpty() { return qList.isEmpty(); }
public void enqueue(Object item) { qList.insertAtBack(item); }
public Object dequeue() {
if (qList.isEmpty()) return null;
else return qList.deleteFromFront();
}
public Object peek() {
return (qList.isEmpty() ? null : qList.head.getData());
}
}

class StackByInheritance extends LinkedList { // (4)
public void push(Object item) { insertInFront(item); }
public Object pop() {
if (isEmpty()) return null;
else return deleteFromFront();
}
public Object peek() {
return (isEmpty() ? null : head.getData());
}
}

public class Client1 { // (5)
public static void main(String[] args) {
String string1 = "Queues are boring to stand in!";
int length1 = string1.length();
QueueByAggregation queue = new QueueByAggregation();
for (int i = 0; i<length1; i++)
queue.enqueue(new Character(string1.charAt(i)));
while (!queue.isEmpty())
System.out.print((Character) queue.dequeue());
System.out.println();

String string2 = "!no tis ot nuf era skcatS";
int length2 = string2.length();
StackByInheritance stack = new StackByInheritance();
for (int i = 0; i<length2; i++)
stack.push(new Character(string2.charAt(i)));
stack.insertAtBack(new Character('!')); // (6)
while (!stack.isEmpty())
System.out.print((Character) stack.pop());
System.out.println();
}
}



Could someone tell me as in how in the 'insertAtBack' method in class 'LinkedList' when we call tail.setNext(new Node(dataObj, null)), the next of head is also getting set. I have tried debugging but could not get thru.

Thanks
 
Marshal
Posts: 80063
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please go back to your post and edit it with the pencil-and-paper icon, then put CODE tags round all the ode. That will preserve indentation. Otherwise nobody can read your code.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic