Home » Stacks & Queues(Use Me !!) » Stack Implemention

Stack Implemention


import java.util.NoSuchElementException;

/** Singly-Linked List Stack Implementation */
public class Stack {

 /** Reference to the top element */
 private Node top;

 /** Singly-linked list data structure for holding a stack element */
 private class Node {
 public Object data;
 public Node next;

 public Node( Object data, Node next ) {
 this.data = data;
 this.next = next;
 }
 }


 /** Pushes new element to the top of the stack. */
 public void push( Object data ) {
 if( isEmpty( ) ) {
 top = new Node( data, null );
 } else {
 top = new Node( data, top );
 }
 }


 /** Returns and removes the top element from the stack. */
 public Object pop( ) throws NoSuchElementException {
 if( isEmpty( ) ) {
 throw new NoSuchElementException( );
 } else {
 // Get top element
 Object data = top.data;
 // Nullify reference to top element
 top = top.next;
 // Return top element
 return data;
 }
 }

 /** Returns the element from the top of the stack without removing it. */
 public Object peek( ) throws NoSuchElementException {
 if( isEmpty( ) ) {
 throw new NoSuchElementException( );
 } else {
 return top.data;
 }
 }

 /** Checks if the stack is empty. */
 public boolean isEmpty( ) {
 return top == null;
 }

 /** Test Method */
 public static void main( String[ ] args ) {

 Stack stack = new Stack( );
 for( int i = 0; i < 10; i++ ) {
 stack.push( i );
 }

 // Peek at the top element
 System.out.println( stack.peek( ) ); // return top elemnt

 // Pop all elements off the list
 while( ! stack.isEmpty( ) ) {
 System.out.print( stack.pop( ) + " " );
 }


 }
}

&nbsp;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Website

arpit tak

arpit tak

I like JAVA . I code. I chill. I blog.I eat. I sleep. I dream.

Personal Links

View Full Profile →

Followers

%d bloggers like this: