Showing posts with label Collection-Data Structure. Show all posts
Showing posts with label Collection-Data Structure. Show all posts

Thursday, June 30, 2011

How to Implement Stack

package org.best.example;

import java.util.*;
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;

 public class StackTest extends JFrame
 {

     public StackTest()
     {
         super( "Stacks" );

         Container c = getContentPane();

         final JLabel status = new JLabel();
         final Stack s = new Stack();

         c.setLayout( new FlowLayout() );
         c.add( new JLabel( "Enter a string" ) );
         final JTextField input = new JTextField( 10 );
         c.add( input );

         JButton pushBtn = new JButton( "Push" );
         pushBtn.addActionListener( new ActionListener()
         {
             public void actionPerformed( ActionEvent e )
             {
                 status.setText( "Pushed: " + s.push( input.getText() ) );
             }
         } );
         c.add( pushBtn );

         JButton popBtn = new JButton( "Pop" );
         popBtn.addActionListener( new ActionListener()
         {
             public void actionPerformed( ActionEvent e )
             {
                 try
                 {
                     status.setText( "Popped: " + s.pop() );
                 }
                 catch ( EmptyStackException exception )
                 {
                     status.setText( exception.toString() );
                 }
             }
         } );
         c.add( popBtn );

         JButton peekBtn = new JButton( "Peek" );
         peekBtn.addActionListener( new ActionListener()
         {
             public void actionPerformed( ActionEvent e )
             {
                 try
                 {
                     status.setText( "Top: " + s.peek() );
                 }
                 catch ( EmptyStackException exception )
                 {
                     status.setText( exception.toString() );
                 }
             }
         } );
         c.add( peekBtn );

         JButton emptyBtn = new JButton( "Is Empty?" );
         emptyBtn.addActionListener( new ActionListener()
         {
             public void actionPerformed( ActionEvent e )
             {
                 status.setText( s.empty() ? "Stack is empty" : "Stack is not empty" );
             }
         } );
         c.add( emptyBtn );

         JButton searchBtn = new JButton( "Search" );
         searchBtn.addActionListener( new ActionListener()
         {
             public void actionPerformed( ActionEvent e )
             {
                 String searchKey = input.getText();
                 int result = s.search( searchKey );

                 if ( result == -1 )
                     status.setText( searchKey + " not found" );
                 else
                     status.setText( searchKey + " found at element " + result );
             }
        } );
        c.add( searchBtn );

         JButton displayBtn = new JButton( "Display" );
         displayBtn.addActionListener( new ActionListener()
         {
             public void actionPerformed( ActionEvent e )
             {
                 Enumeration enum = s.elements();
                 StringBuffer buf = new StringBuffer();

                 while ( enum.hasMoreElements() )
                     buf.append( enum.nextElement() ).append( " " );

                 JOptionPane.showMessageDialog( null, buf.toString(), "Display", JOptionPane.PLAIN_MESSAGE );
             }
         } );
         c.add( displayBtn );
         c.add( status );

         setSize( 675, 100 );
         show();
     }

     public static void main( String args[] )
     {
         StackTest app = new StackTest();

         app.addWindowListener( new WindowAdapter()
         {
             public void windowClosing( WindowEvent e )
             {
                 System.exit( 0 );
             }
         } );
     }
 }

Tuesday, June 21, 2011

How to LinkedList in Java

In this example, you will see the use of java.util.LinkedList class. We will be creating an object of link list class and performing various operation like adding and removing object.
This class extends AbstractSequentialList and implements List, Cloneable, Serializable. It permits all elements including null. LinkedList class provides methods get, insert and remove an element at the beginning and end of the list.
In this example six methods of LinkedList class is demonstrated.
add(Object o): Appends the specified element to the end of this list. It returns a boolean value.
size(): Returns the number of elements in this list.
addFirst(Object o): Inserts the given element at the beginning of this list. addLast(Object o): Inserts the given element at the last of this list.
add(int index,Object o): Insert the specified element at the specified position in this list. It throws IndexOutOfBoundsException if index is out of range.
remove(int index): Remove the element at the specified position in this list. It returns the element that was removed from the list. It throws IndexOutOfBoundsException if index is out of range. 


import java.util.*;

public class LinkedListDemo{
 public static void main(String[] args){
  LinkedList link=new LinkedList();
  link.add("a");
  link.add("b");
  link.add(new Integer(10));
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());
  
  link.addFirst(new Integer(20));
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());

  link.addLast("c");
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());

  link.add(2,"j");
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());

  link.add(1,"t");
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());

  link.remove(3);
  System.out.println("The contents of array is" + link);
  System.out.println("The size of an linkedlist is" + link.size());
 }
}