Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Exercise 4:
  3.  * write a Java application to insert THREE(3) integers 8, 10, 66 using insertAtFront().
  4.  * Print and observe the output.
  5.  * note: the output should be 66,10,8
  6.  *
  7.  * @author MUHAMMAD AZRI BIN JASNI
  8.  * @version 10 OCTOBER 2012
  9.  */
  10. import java.util.*;
  11. public class exercise4
  12. {
  13.     public static void main(String [] args)
  14.     {
  15.         LinkedList list = new LinkedList();
  16.         Scanner sc = new Scanner(System.in);
  17.         int input;
  18.        
  19.         System.out.println("Enter the 3 integers[8,10,66] at the front.");
  20.         for (int i=0;i<3;i++)
  21.         {
  22.             input = sc.nextInt();
  23.             list.insertAtFront(input);
  24.         }
  25.         list.display();
  26.        
  27.         /* Exercise 5
  28.          * Write the complete definition for method insertAtBack() and insert
  29.          * another THREE (3) integers 68, 3, 1. Print and observe the output.
  30.          * *Note: The output should be 66, 19, 8, 68, 3, 1*/
  31.         System.out.println("Enter the 3 integers[68,3,1] at the back.");
  32.         for (int i=0;i<3;i++)
  33.         {
  34.             input = sc.nextInt();
  35.             list.insertAtBack(input);
  36.         }
  37.         list.display();
  38.            
  39.         /* Exercise 6
  40.          * Write the complete definition for method removeFromFront() and remove a node.
  41.          * Print and observe the output.
  42.          *
  43.          * *Note: The output should be 10, 8, 68, 3, 1*/
  44.          System.out.println("Removing an element from the front...");
  45.          list.removeFromFront();
  46.          list.display();
  47.         /* Exercise 7
  48.          * Write the complete definition for method removeFromBack() and remove
  49.          * TWO(2) nodes. Print and observe the output.
  50.          * *Note: The output should be 10, 8, 68*/
  51.          System.out.println("Removing 2 element from the back...");
  52.          list.removeFromBack();
  53.          list.display();
  54.          list.removeFromBack();
  55.          list.display();
  56.         sc.close();
  57.     }
  58. }