File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How the insertAtBack sets the next for head? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How the insertAtBack sets the next for head?" Watch "How the insertAtBack sets the next for head?" New topic

How the insertAtBack sets the next for head?

swagata roy

Joined: Dec 16, 2008
Posts: 1
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) { = data; = 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());

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());

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.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38075
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.
I agree. Here's the link:
subject: How the insertAtBack sets the next for head?
Similar Threads
Linked List Sorting Problem
Linked List Sorting Problems
what is the code trying to say between inheritance and aggregation?
Linked List help
Saving to a txt file