stack singly linked list example

Hi,

I ran below example

http://www.enterjava.com/search?updated-min=2015-01-01T00:00:00%2B05:30&updated-max=2016-01-01T00:00:00%2B05:30&max-results=6

public class MainClass { 
 public static void main(String[] args) {
  MyStack stack = new MyStack();
  stack.push(55);
  stack.push(12);
  stack.push(5);
  stack.push(18);
  System.out.println(stack);
  System.out.println("Top is :" + stack.top().getData());
  System.out.println("Pop :" + stack.pop().getData());
  System.out.println("Top after one pop :" + stack.top().getData());
 }
}

Open in new window

class MyStack implements Stack {
 private Node headNode;
 public void push(int data) {
  if (headNode == null) {
   headNode = new Node(data);
  } else {
   Node node = new Node(data);
   node.setNext(headNode);
   headNode = node;
  }
 }
 
 
 public Node top() {
  if (headNode == null) {
   return null;
  } else {
   return headNode;
  }
 }
 
 
 public Node pop() {
  Node node = null;
  if (headNode == null) {
   System.out.println("stack empty");
  } else {
   node = headNode;
   headNode = headNode.getNext();
  }
  
  return node;
 }
   
@Override
public boolean isEmpty() {
	// TODO Auto-generated method stub
	return false;
}


@Override
public void deleteStack() {
	// TODO Auto-generated method stub
	
}


}

Open in new window


class Node {
 private Integer data;
 private Node next;
 public Node(Integer data) {
  super();
  this.data = data;
 }
 public Integer getData() {
  return data;
 }
 public void setData(int data) {
  this.data = data;
 }
 public Node getNext() {
  return next;
 }
 public void setNext(Node next) {
  this.next = next;
 }
 @Override
 public String toString() {
  return "Node [data=" + data + ", next=" + next + "]";
 }
}

Open in new window


interface Stack {
 public void push(int data);

 public Node pop();

 public Node top();

 public boolean isEmpty();

 public void deleteStack();
}

Open in new window

I got output as
MyStack@276af2
Top is :18
Pop :18
Top after one pop :5


I see author saying
As we are using the linked list implementation , we have the Node class which is  representation of singly linked list. It has data and next attribute.

i do not see any 'singly linked list' in the code. What is 'singly linked list'. where and how and why it is used.  Is there is somethong like doubly linked list or triply linked lists also. when we use each.Can you please elaborate more on these concepts.
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
It's a singly linked list implementation because each Node points to the next Node in the sequence.  You have a class that is holding a reference to another instance of the the same type of class:
class Node { // we are defining a class called "Node"

    private Node next; // <-- this points to another instance of "Node"!

    // ... other code ...

}

Open in new window

When traversing a singly linked list, you can only move forward, as there is only a reference to the node that is next in the sequence.

As well as the next node, however, a double linked list also points to the previous node:
class Node { // we are defining a class called "Node"

    private Node previous;

    private Node next;

    // ... other code ...

}

Open in new window

This allows you to move forwards and/or backwards when traversing a doubly linked list.

A List is preferable to an Array, since you do not need a contiguous block of memory to hold the elements.  You can insert  and delete items in a List without needing to physically shift all items like in an array.  Instead, you simply update the references in the affected Nodes; everything else remains unchanged.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSr. Software engineerCommented:
gudii9Author Commented:
let me read
zzynxSr. Software engineerCommented:
Thanx 4 axxepting
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.