Tutorial: Linked List III


Authored by
Ronald S. Holland
Total Application Works
RonHolland@sumtotalz.com






To visit my site <

HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript ]


To contact us

2002 - 2011 All Rights Reserved Total Application Works

  • Tell a friend about this site (copy and paste the following HTML page that links to this tutorial.)
  • <html>
    <title>Example of a link </title>
    <body>
    <table border>
    <a href="http://sumtotalz.com/TotalAppsWorks/Linkedlist/LL_Tutorial_3.html"> Tutorial: Linked List III </a>
    </table>
    </body>
    </html>
    Download Source
    and display a Screenshot
  • On-site classes can be arranged in Java and C++. Send all inquiries to:
  • RonHolland@sumtotalz.com

  1. Java Table of contents
  2. 2nd Java Cup
  3. Pie Chart I
  4. Pie/Bar Chart IB
  5. Pie/Bar Chart II
  6. Pie/Bar Chart III
  7. A Basic Calculator
  8. Linked list Introduction
  9. Linked Lists I
  10. Linked List II
  11. Linked List III
  12. Linked List IV
  13. Hardware Store I
  14. Hardware Store II
  15. Hardware Store III
  16. Client/Server
  17. Client/Server II
  18. Client/Server III
  19. Client/Server IV
  20. Multithreaded Client/Server
  21. Multithreaded Client/Server II
  22. Multithreaded Client/Server III
  23. Basic Calculator II
  24. Basic Calculator III

Introduction

This tutorial assumes that you have already mastered the preceding tutorials on this topic:

  1. Linked Lists I
  2. Linked List II
In this tutorial, we will continue to develop our Linked List application by adding a GUI front end to our application. We will not be changing our Linked List class in this tutorial. In the preceding two tutorials, we used the JOptionPane class as a front end to our Linked List application. In this tutorial, we will use the JApplet and JFrame APIs to build the GUI for our application. If needed, you can review A Second cup of coffee for an introduction to applets and applications. As a review, we will redefine a Linked List, which is a main topic in our tutorial, in the next section.

What is a Linked List?

What is a Linked List? It is a self-referential class that contains a reference member and that member contains the address to a class object of the same class type. In real life, there are many instances where we use addresses. For example, we use addresses

  • To designate where we live.
  • To designate where mail is to be sent.
  • To designate our email address.
We make use of this knowledge when we use a telephone book, a distribution/mailing list, and/or the firing order of a combustion engine. In the case of a telephone book, the information is organized by sections and sub-sections. For example, all last names starting with an "A" are found in the major section pointing to an ordered list of last names that begin with "A".
  
+----+    +----+     +----+      +----+    +----+    +----+ 
| A  |    | B  |     | C  | ...  | X  |    | Y  |    | Z  |   <-- major nodes
+-+--+    +-+--+     +-+--+      +-+--+    +-+--+    +-+--+     
  |         |          |           |         |         |   
  |         V          V           V         V         |
  |                                                    |
  V                                                    V 
+-+---------+                                       +--+--------+   
| Last      |                                       | Last      |
| Names     |                                       | Names     | 
| Starting  |  <------- ordered list ---------->    | Starting  | 
| With      |                                       | With      | 
| "A"       |                                       | "Z"       |  
+-----------+                                       +-----------+
 
Figure 1: Telephone book using a linked list


A Linked List is like a chain where each link in the chain contains the address to the next link in the chain. A class of this type might look like:

  
class node {
   private int data;
   private next refA;

   public node() {}
   void setData( int ) {}
   int getData(  int ) {}
   void setNextRef( node ref ) {}
   node getNextRef( ) {} 
   void init(  ) {}
   void AppendNode( int ) {}
   void InsertNode( int ) {}
   void DeleteNode( int ) {}
   void DisplayList( int ) {}
}
  
Figure 2: Linked list class Node


After the above class is run and data is inserted into the linked list, the chain might look something like the following:

  
    HEAD
+-----------+
| reference | <-- contains the address of the start
+-----+-----+     of the linked list
      | 
      V   
+-----+-----+      +-----------+      +-----------+
|   data    |  +-->|   data    |  +-->|   data    |
+-----------+  |   +-----------+  |   +-----------+
| reference |--+   | reference |  |   | reference |
+-----------+      +-----+-----+  |   +-----+-----+
                         |        |
      +------------------+        |
      |                           |
      V                           |
+-----+-----+      +-----------+  |
|   data    |  +-->|   data    |  |
+-----------+  |   +-----------+  | 
| reference |--+   | reference |--+
+-----------+      +-----------+
Figure 3: Linked list chain


In an array, data is stored in contiguous memory and can be accessed by using a reference and an index. For example,

  
int ar[] = new int[ 12] ;
 
for ( int i = 0 ; i < ar.length ; i++ )
   ar[ i ] = i ;
Figure 4: Initializing an array


The above code segment loads the value of i in each succeeding location of the array named ar.

An array is different than a linked list in many ways. In an array,

  • The data is stored in contiguous physical memory.
  • The contiguous memory is addressed using a reference to the first location.
  • By using the name of the array and an index, the data is accessed. For example,
      
    ar[ 1 ] = 5 ;
    
  • The location of the array is resolved at compile time.
  • In addition, the size of an array cannot be changed at run time.

The organization of data in an array is physical. A linked list

  • Stores data based on the logical order of the data and not the physical order.
    • In other words, a linked list gives the appearance that the data in the list is stored in contiguous memory.
  • Instead of an index, all stored data records are assigned a physical address in memory that the link list algorithm/logic uses to locate the information.
  • A linked list logically organizes the data, rather than by physical address.
  • The memory for each node in the linked list is dynamically allocated at run time.

What is a Java Applet?

An applet is a Java program that is transported over the Internet and executed in browsers (like Netscape, MS Internet Explorer, etc.). The applet API also has an initialization method called init(). Therefore, our applet template would look like:

  
import javax.swing.*;
import java.awt.*;
 
/**
 * The MyApplet class implements an applet.
 */
public class MyApplet extends JApplet {

    public void init() {
        // initialization code
        initialization code...
        ...
    }

    public void paint(Graphics g) {
        // Display "something!"
        g.drawString("something!", 50, 25);
    }
}
  
Figure 5: Applet Template


where,

  • public void init()
    • The init() method is called by the browser or applet viewer to inform this applet that it has been loaded into the system prior to the start() method being called.
      • The init() method is always called before the start() method is called.
    • A subclass of JApplet should override he init() method if it has initialization to perform.
    • The implementation of this method provided by the Applet class does nothing.

What is a Java Application?

A Java Application contains the following main method.

  
public static void main( String args[] ) 
  
A template for an application looks like the following code template.

   
/** 
 * The MyApp class implements an application.
 */
class MyApp {

    // MyApp constructor
    public MyApp() {
       initialization code
       ...
    } 

    public static void main(String[] args) {
        minimal code
        ...
    }
}
  
Figure 6: Application Template


Requirements for a Linked List

This program is intended to be an introduction to linked lists. As such, the requirements are:

  1. Requirements for tutorial I
    1. Write this program as an application.
    2. The linked list node should be added to the linked list in a LIFO manner
      • All new nodes should be added at the head of the linked list
    3. Create an Add method to add new nodes to the linked list
    4. Create a Remove method to delete/remove nodes from the linked list
    5. Create a DisplayLL method to show the contents of the nodes
  2. Requirements for tutorial II
    1. Modify the add method so that elements are added in ascending sorted order.
    2. Create an addLast method to add new elements to the tail/end of the list.
    3. Create an addFirst method to add new elements to the head of the list.
    4. Create a Remove method to delete an element from the Linked List.
    5. Create an isEmpty method to determine if the Linked List has any entries.
    6. Create a contains method that finds out if a particular value is in the Linked List.
    7. Create a toArray method that retrieves the elements in the Linked List and stores them in an array.
    8. Create a getLast method that returns the last element in the Linked List.
    9. Create a size method that discovers the number of elements in the Linked List.
  3. Requirements for tutorial III
    1. Write the code so that it can be run as either an applet or an application.
      • Use the JApplet class to run the code as an applet.
      • Use the JFrame class to run the code as an application.
    2. Remove the main() method from the Linked List application.
    3. Add a test application that creates a Linked List object.
    4. Add a GUI front end to the test application.
      • The test application should be a subclass of JFrame i.e., extends JFrame
      • Use the JTable class to display the entries in the list.
      • Use the JButton class to allow the user to access various Linked List functions.

A Possible Approach to Building an Application

When we look at the requirements (clues), a few items jump out at us.

  1. Write the code so that it can be run as either an applet or an application.
    • Use the JApplet class to run the code as an applet.
    • Use the JFrame class to run the code as an application.

Over the years, I have seen various approaches to building an application. I will use the following simple top-down approach of using templates. Templates allow us to solve this problem from a top down view.

   
/** this is a template for a program that can
    be run as an application or an applet*/

public class TestLinkedList extends JApplet 
          implements ActionListener  {

   private int width = 500, height = 400  ;
   private LinkedList ll  = new LinkedList() ;

   public void init() {
      /** ************************************************
       * initialization code...
       * ...
       ***************************************************/
   }


   public void actionPerformed( ActionEvent e )  {

      /** ************************************************
       * This is the event handler code that responds to
       * events e.g., buttons being pressed
       ***************************************************/
   }

    
   public static void main(String[] args) {
        
      // create window in which applet will execute
      JFrame appMain =
         new JFrame( "Linked List Application" );

        // create the rest of the minimal code
        TestLinkedList appBar = new TestLinkedList() 


      /** call applet's init and start methods */
      appBar.init();
      appBar.start();

      /** attach applet to center of window */
      appMain.getContentPane().add( appBar );

      /** set the window's size */
      appMain.setSize( appBar.width, appBar.height );

      /**
       * showing the window causes all GUI components
       * attached to the window to be painted
       */

      appMain.show();
   }
}
  
Figure 7: Template for an application or an applet


Logic for init() Method

The init() is used primarily to initialize the application. In our example, we will:

  1. Use getContentPane() method to get the context for the application/applet.
  2. Call our setupText() method to set up labels and textfields.
  3. Call our setupButtons() method to set up our buttons.
  4. Call our InitLL() method to set up initialize the Linked List.
  5. Call the Linked List toArray() method to load the Linked List entries into an array.
  6. Call the JTable API with the array just loaded and the column names.
  7. Using a Border Layout manager;
    1. Load the table in the center part of the frame.
    2. Load the label and text fields in the upper part of the frame.
    3. Load the buttons in the bottom part of the frame.
An example looks like:

  
   /** Initialization method */
   public void init()   {

      c = getContentPane();

      setupText() ; // set up labels and textfields

      setupButtons() ; // set up buttons

      InitLL() ; // set up the linked list

      ll.toArray(  pData ) ;
      table = new JTable( pData, columnNames ) ;
      table.setEnabled( true );
      c.add( table , BorderLayout.CENTER) ;
      c.add( new JScrollPane ( table ) );
      c.validate();

      c.add( textPanel,  BorderLayout.NORTH );
      c.add( buttonPanel, BorderLayout.SOUTH );
   }
  
Figure 8: Logic for init() Method


Logic for setupText() Method

To set the label and text field in our application/applet, we will do the following:

  1. Create an instructional label.
  2. Create a JPanel to contain the label and text entry field.
  3. Create a text entry field to capture input.
  4. Add the label and text entry field to the above panel.
  5. Attach an event listener to the text entry field.

Logic for setupButtons() Method

The logic for the add Method is as follows:

  1. Add the following JButtons:
    • addBut
    • addLastBut
    • addFirstBut
    • debugBut
    • removeBut
    • sizeBut
    • exitButton
  2. Add the buttons to a JPanel
  3. Add an event listener to the buttons.

Logic for InitLL() Method

In the InitLL() Method, we will initialize the Linked List with some initial values. Since this version of the code does not have persistent data capability, our Linked List must be initialized at the start of every execution.

Logic for Redisplay() Method

The Redisplay() method is called to redisplay the JTable when the value(s) have changed. The logic looks like:

  1. Call the Linked List toArray() method to get the updated entries
  2. Delete the old table
  3. Create a new table
  4. Add the new table to the frame context
  5. Add a scroll pane to this context
  6. Cause the updated table to show on the screen

Logic for actionPerformed() Method

The actionPerformed() Method is the method that is called when a GUI event, such as a button being pressed, occurs.

Logic for sysPrint() Method

The sysPrint() Method is used for debugging.

  • set myDebug = true to turn on debugging

Logic for checkDigit() Method

The checkDigit() Method is used to make sure that the data entered is a digit.

Logic for checkFields() Method

The purpose of the checkFields() Method is to ensure that the text entry field is not null.

  1. It issues an error message if the text enry field is null.
  2. It then checks to ensure that the data entered was a digit.
  3. If the data entered was a digit, this code ensures that a button was pressed.

Putting It All Together

When we put it all together we get code that looks like:

  
/** *******************************************************************
 * File: TestLinkedList.java
 * This program takes user input and creates a Linked List.
 *
 * Methods:
 *  1- init()  
 *  2- setupText()   
 *  3- setupButtons()  
 *  4- InitLL()  
 *  5- Redisplay() 
 *  6- actionPerformed()  
 *  7- sysPrint()
 *  8- setWidth()
 *  9- setHeight()
 * 10- checkDigit() 
 * 11- getTextValues() 
 * 12- checkDigit() 
 * 13- checkFields()
 * 14- sysExit() 
 * 15- main( String args[] ) 
 * 
 *********************************************************************************/


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

public class TestLinkedList extends JApplet
          implements ActionListener  {
   private JButton addBut, addLastBut, addFirstBut,
                 debugBut, removeBut, sizeBut, exitButton;
   private JPanel buttonPanel;
   private JPanel textPanel;
   private JTable table;
   private String pData[] []  = new String [ 100 ] [ 2 ];
   private String columnNames[] = {"Record ID", "Node Value" } ;
   private JTextField text1 ;
   private JLabel label1 , label2 ;
   private Container c ;
   private int width = 500, height = 400 , intTextValue ;
   private JFrame app ;
   private String strVal;
   private LinkedList ll  = new LinkedList() ;
   public  Graphics gVal;
   boolean buttonPressed = false ,
            myDebug = false ;

   /** Initialization method */
   public void init()   {

      c = getContentPane();

      setupText() ;

      setupButtons();

      InitLL() ;

      ll.toArray(  pData ) ;
      table = new JTable( pData, columnNames ) ;
      table.setEnabled( true );
      c.add( table , BorderLayout.CENTER) ;
      c.add( new JScrollPane ( table ) );
      c.validate();

      c.add( textPanel,  BorderLayout.NORTH );
      c.add( buttonPanel, BorderLayout.SOUTH );
      //c.add( drawingArea, BorderLayout.CENTER );
   }

   /** *****************************************************
    * Allocate and set up the text entry fields
    * 1- Create an instructional label. 
    * 2- Create a JPanel to contain the label and 
    *    text entry field.
    * 3- Create a text entry field to capture input.
    * 4- Add the label and text entry field to the panel.
    * 5- Attach an event listner to the text entry field.
    **********************************************************/
   public void setupText() {

      label1      = new JLabel("                              " +
                        "       Enter 1 number.");
      label2      = new JLabel("                              " +
                               "                             "   );

      textPanel   = new JPanel();
      text1       = new JTextField(5);

      textPanel.setLayout(
         new GridLayout( 1, 3 ) );

      textPanel.add(label1);
      textPanel.add(text1);
      textPanel.add(label2);
      buttonPanel = new JPanel();
      buttonPanel.setLayout(
         new GridLayout( 1, 7 ) );

      text1.addActionListener( this );
   }

   /** ****************************************************
    * Allocate and set up the JButtons 
    *  1- Add the following JButtons:
    *    - addBut
    *    - addLastBut
    *    - addFirstBut
    *    - displayBut
    *    - removeBut
    *    - sizeBut
    *    - exitButton
    *  2- Add the buttons to a JPanel
    *  3- Add an event listner to the buttons
    ********************************************************/
   public void setupButtons() {

      addBut        = new JButton( "Add" );
      addLastBut    = new JButton( "Add Last" );
      addFirstBut   = new JButton( "Add First" );
      debugBut    = new JButton( "Debug" );
      debugBut.setBackground( Color.blue);
      debugBut.setForeground( Color.white);
      removeBut     = new JButton( "Delete" );
      removeBut.setBackground( Color.red);
      removeBut.setForeground( Color.white);
      sizeBut       = new JButton( "Get Size" );
      exitButton    = new JButton( "Exit" );
      exitButton.setBackground( Color.red);
      exitButton.setForeground( Color.white);

      buttonPanel.add( addBut );
      buttonPanel.add( addLastBut );
      buttonPanel.add( addFirstBut );
      buttonPanel.add( debugBut );
      buttonPanel.add( removeBut );
      buttonPanel.add( sizeBut );
      buttonPanel.add( exitButton );

      addBut.addActionListener( this );
      addLastBut.addActionListener( this );
      addFirstBut.addActionListener( this );
      debugBut.addActionListener( this );
      removeBut.addActionListener( this );
      sizeBut.addActionListener( this );
      exitButton.addActionListener( this );
   }


   /** ****************************************************
    * Initialize the list with some initial values.
    ********************************************************/
   public void InitLL() {

      ll.add(110);
      ll.add(93);
      ll.add(250);
      ll.add(77);
      ll.add(11);
      ll.add(150);
      ll.add(215);
      ll.add(34);
      ll.add(81);
      ll.add(47);
      ll.add(55);
      ll.add(62);
      ll.add(211);
   }


   /** ********************************************************
    * The Redisplay() method is called to redisplay the JTable
    * when the value(s) have changed.
    *  1- Call the Linked List toArray() method to get the updated
    *       entries
    *  2- Delete the old table 
    *  3- Create a new table 
    *  4- Add the new table to the frame context 
    *  5- Add a scroll pane to this context 
    *  6- Cause the updated table to show on the screen 
    * 
    ***********************************************************/
   public void Redisplay() {

      ll.toArray(  pData ) ;
      c.remove( table ) ;
      table = new JTable( pData, columnNames ) ;
      table.setEnabled( true );
      c.add( table , BorderLayout.CENTER) ;
      c.add( new JScrollPane ( table ) );
      c.validate();
   }

   /** ********************************************************
    * The actionPerformed( ActionEvent e ) s called when a 
    * GUI event, such as a button being pressed, occurs.
    * 
    ***********************************************************/
   public void actionPerformed( ActionEvent e )  {

      if ( e.getSource() == text1 ) {
         if ( checkFields( e,  addBut, 0 ) )
             checkFieldsChange( e );
            //drawingArea.setCurrentChoice( 0 );
         sysPrint("Textfield processing started.") ;
      }
      else  if ( e.getSource() == addBut ) {
         sysPrint("Add Button processing started.") ;
         buttonPressed = true ;
         if ( checkFields( e,  addBut, 1 ) )
             checkFieldsChange( e );
         ll.add( intTextValue );
         Redisplay() ;
      }
      else if ( e.getSource() == addLastBut ) {
         sysPrint("addLast Button processing started.") ;
         buttonPressed = true ;
         if ( checkFields( e,  addLastBut, 2 ) )
            checkFieldsChange( e );
         ll.addLast( intTextValue );
         Redisplay() ;
      }
      else if ( e.getSource() == addFirstBut ) {
         buttonPressed = true ;
         sysPrint("addFirst Button processing started.") ;
         if ( checkFields( e,  addFirstBut, 3 ) )
                    checkFieldsChange( e );
         ll.addFirst( intTextValue );
         Redisplay() ;
      }
      else if ( e.getSource() == debugBut ) {
         buttonPressed = true ;
         sysPrint("Debug Button processing started.") ;
         myDebug = true ;
      }
      else if ( e.getSource() == removeBut ) {
         sysPrint("remove Button processing started.") ;
         buttonPressed = true ;
         if ( checkFields( e,  removeBut, 4 ) )
                    checkFieldsChange( e ) ;
         ll.Remove( intTextValue );
         Redisplay() ;
      }
      else if ( e.getSource() == sizeBut ) {
         sysPrint("size Button processing started.") ;
         buttonPressed = true ;

         int sizeOfLL = ll.size() - 1 ;
         JOptionPane.showMessageDialog(null,
                 "The size of the linked list is " + sizeOfLL ,
                 "Size of linked list", JOptionPane.INFORMATION_MESSAGE ) ;
      }
      else if ( e.getSource() == exitButton ) {
         buttonPressed = true ;
         sysPrint("exitButton processing started.") ;
         sysExit( 0 );
      }
   }


   /** *************************************************
    *  sysPrint() - is used for debugging
    *               - set myDebug = true to turn on debugging
    *
    ************************************************ */
   public void sysPrint( String str  )   {
      if ( myDebug ) {
         System.out.println( str );
      }
   }

   /** Set the width of frame to 600 if w less than 0 */
   public void setWidth( int w )
      { width = ( w >= 0 ? w : 600 ); }

   /** Set the height to 600 if h less than 0 */
   public void setHeight( int h )
      { height = ( h >= 0 ? h : 400 ); }

   /** Make sure that the data entered is a digit */
   public boolean  checkDigit(String strVal) {

       int strLength = 0;
       boolean notDig = true;

       strLength = strVal.length();

       for (int ii = 0; ii < strLength; ii++) {
          if (!Character.isDigit(strVal.charAt(ii)) ) {
             notDig = false;
             break;
          }
       }

       return notDig;
   }

   /** Check if data in the text fields has changed */
   public void checkFieldsChange( ActionEvent e ) {
/*       if (!text1.getText().equals(drawingArea.strField1) ||
                  !text2.getText().equals(drawingArea.strField2) ||
                  !text3.getText().equals(drawingArea.strField3) ||
                  !text4.getText().equals(drawingArea.strField4) ) {
          getTextValues();
       }  */

       return;
   }

   /** Retrieve data from the text fields */
   public int getTextValues() {
      return Integer.parseInt( text1.getText() );
   }

   /** Ensure that the data is not null */
   public boolean checkFields(
                       ActionEvent e, JButton choice, int i ) {
      boolean checkedOK = false;

      if ( text1.getText().equals("")     ) {
         JOptionPane.showMessageDialog(null,
                 "Please enter a valid whole number.\n",
                 "Invalid Number", JOptionPane.ERROR_MESSAGE ) ;
      }
      else if( !checkDigit( text1.getText())  ) {
         JOptionPane.showMessageDialog(null,
                 "Please enter a valid whole number.\n" +
                 "\"" + text1.getText() + "\" is not a valid number.",
                 "Invalid Number", JOptionPane.ERROR_MESSAGE ) ;
      }
      else {
         intTextValue  = getTextValues() ;
         checkedOK = true;
         if ( !buttonPressed ) {
             buttonPressed = false ;
             JOptionPane.showMessageDialog(null,
                 "Please press a button.\n" ,
                 "No Button", JOptionPane.INFORMATION_MESSAGE ) ;
         }
      }

      return checkedOK;
   }

   /** */
   public void sysExit( int a ) {
      System.exit( a );
   }

   /** ************************************************
    *  Main entry point
    ***************************************************/
   public static void main( String args[] )   {
      int width = 790, height = 550;
      Graphics gVal2;

      // create window in which applet will execute
      JFrame appMain =
         new JFrame( "Linked List Application" );

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

      /** create one applet instance */
      TestLinkedList appBar = new TestLinkedList();
      appBar.setWidth( width );
      appBar.setHeight( height );

      /** call applet's init and start methods */
      appBar.init();
      appBar.start();

      /** attach applet to center of window */
      appMain.getContentPane().add( appBar );

      /** set the window's size */
      appMain.setSize( appBar.width, appBar.height );

      /**
       * showing the window causes all GUI components
       * attached to the window to be painted
       */

      appMain.show();
   }
}
  
Figure 8: Logic for init() Method


Our TestLinkedList API

Methods: Description
 void actionPerformed(java.awt.event.ActionEvent e)
          The actionPerformed( ActionEvent e ) s called when a GUI event, such as a button being pressed, occurs.
 boolean checkDigit(java.lang.String strVal)
          Make sure that the data entered is a digit
 boolean checkFields(java.awt.event.ActionEvent e, javax.swing.JButton choice, int i)
          Ensure that the data is not null
 void checkFieldsChange(java.awt.event.ActionEvent e)
          Check if data in the text fields has changed
 int getTextValues()
          Retrieve data from the text fields
 void init()
          Initialization method
 void InitLL()
          Initialize the list with some initial values.
static void main(java.lang.String[] args)
          Main entry point
 void Redisplay()
          The Redisplay() method is called to redisplay the JTable when the value(s) have changed.
 void setHeight(int h)
          Set the height to 600 if h less than 0
 void setupButtons()
          Allocate and set up the JButtons 1- Add the following JButtons: - addBut - addLastBut - addFirstBut - displayBut - removeBut - sizeBut - exitButton 2- Add the buttons to a JPanel 3- Add an event listener to the buttons
 void setupText()
          Allocate and set up the text entry fields 1- Create an instructional label.
 void setWidth(int w)
          Set the width of frame to 600 if w less than 0
 void sysExit(int a)
           
 void sysPrint(java.lang.String str)
          sysPrint() - is used for debugging - set myDebug = true to turn on debugging
 
Methods inherited from class javax.swing.JApplet
addImpl, createRootPane, getAccessibleContext, getContentPane, getGlassPane, getJMenuBar, getLayeredPane, getRootPane, isRootPaneCheckingEnabled, paramString, processKeyEvent, remove, setContentPane, setGlassPane, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, update
 
Methods inherited from class java.applet.Applet
destroy, getAppletContext, getAppletInfo, getAudioClip, getAudioClip, getCodeBase, getDocumentBase, getImage, getImage, getLocale, getParameter, getParameterInfo, isActive, newAudioClip, play, play, resize, resize, setStub, showStatus, start, stop

Our Linked List API

Methods: Description
public LinkedList() Constructs an empty list.
public LinkedList(int n, LinkedList ln) This Constructor is used to create a node containing the value passed.
public void add( int num ) Inserts the specified element to this list in ascending sorted order.
void addLast( int num) Appends the given element to the end of this list.
public void addFirst( int num ) Inserts the given element at the beginning of this list.
public void DisplayLL() Displays the elements in the list to the screen
void Remove(int num) Deletes the specified element from the list.
public boolean isEmpty() Determines if the list is empty.
  • Returns true if not empty
  • Returns false if empty
public boolean contains( int num ) Determines if the specified value is in the list.
  • Returns true if the value is in the list
  • Returns false if the value is not in the list
public void toArray(int a[]) Add each element in the list to the referenced array.
public int getLast() Returns the last element in the list.
public int size() Returns the size of the list i.e., the number of elements.
public static void main(String args[]) This is the entry point of the application.

DBMS - DataBase Management System.

inheritance - when a class is defined, any subclass that is defined can inherit the definitions (public and/or protected) of the parent/base classes.

Java API: Linked List - The following are the methods in the Java Linked List.

requirement - In this instance, a specification that documents the functions, performance, design constraints and attributes of a program to be delivered.

Template - is a unique class or pattern that can be used to build other software code.

Method Summary
 void add(int index, Object element)
          Inserts the specified element at the specified position in this list.
 boolean add(Object o)
          Appends the specified element to the end of this list.
 boolean addAll(Collection c)
          Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
 boolean addAll(int index, Collection c)
          Inserts all of the elements in the specified collection into this list, starting at the specified position.
 void addFirst(Object o)
          Inserts the given element at the beginning of this list.
 void addLast(Object o)
          Appends the given element to the end of this list.
 void clear()
          Removes all of the elements from this list.
 Object clone()
          Returns a shallow copy of this LinkedList.
 boolean contains(Object o)
          Returns true if this list contains the specified element.
 Object get(int index)
          Returns the element at the specified position in this list.
 Object getFirst()
          Returns the first element in this list.
 Object getLast()
          Returns the last element in this list.
 int indexOf(Object o)
          Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
 int lastIndexOf(Object o)
          Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
 ListIterator listIterator(int index)
          Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.
 Object remove(int index)
          Removes the element at the specified position in this list.
 boolean remove(Object o)
          Removes the first occurrence of the specified element in this list.
 Object removeFirst()
          Removes and returns the first element from this list.
 Object removeLast()
          Removes and returns the last element from this list.
 Object set(int index, Object element)
          Replaces the element at the specified position in this list with the specified element.
 int size()
          Returns the number of elements in this list.
 Object[] toArray()
          Returns an array containing all of the elements in this list in the correct order.
 Object[] toArray(Object[] a)
          Returns an array containing all of the elements in this list in the correct order.




[
HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript ]

2002 - 2011 All Rights Reserved Total Application Works