Showing posts with label Stack implementation using Linked List in Java. Show all posts
Showing posts with label Stack implementation using Linked List in Java. Show all posts

Sunday, 16 December 2012

Stack implementation using Linked List in Java


import java.io.*;
class Node
{
public int item;
public Node next;
public Node(int val)
{
item = val;
}
}
class LinkedList
{
private Node first;
public LinkedList()
{
first = null;
}
public void push(int val)
{
Node newNode = new Node(val);
newNode.next = first;
first = newNode;
}
public int pop()
{
if(first==null)
{
System.out.println("Stack is Empty");
return 0;
}
else
{
int temp = first.item;
first = first.next;
return temp;
}
}
public void display()
{
if(first==null)
{
System.out.println("Stack is Empty");
}
else
{
System.out.println("Elements from top to bottom");
Node current = first;
while(current != null)
{
System.out.println("[" + current.item + "] ");
current = current.next;
}
System.out.println("");
}
}

}

Best geography books for UPSC prelims, mains

This post is intended to clear the confusion that prevails among the aspirants over how to prepare for UPSC geography and the best books f...