Skip to content

Instantly share code, notes, and snippets.

@jthomp
Created April 14, 2009 20:26

Revisions

  1. jthomp created this gist Apr 14, 2009.
    270 changes: 270 additions & 0 deletions JustinThompsonHw9
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,270 @@
    import java.util.*;
    import java.io.*;

    public class JustinThompsonHw9
    {
    static LinkedList list;
    static final int MAX = 10; // using this for count.

    public static void main(String[] args) throws Exception
    {
    list = new LinkedList();
    Scanner reader = new Scanner(System.in);
    displayMenu();
    int resp = reader.nextInt();
    while (resp != 0)
    { switch (resp)
    { case 1: enter1(reader); holdScreen(); break;
    case 2: showFirst2(); holdScreen(); break;
    case 3: removeFirst3(); holdScreen(); break;
    case 4: length4(); holdScreen(); break;
    case 5: isFull5(); holdScreen(); break;
    case 6: isEmpty6(); holdScreen(); break;
    case 7: deleteAll7(); holdScreen(); break;
    case 8: displayAll8(); holdScreen(); break;
    case 9: quit9(); holdScreen(); break;
    case 10: deleteAllInSequence(); holdScreen(); break;
    default: System.out.println("Invalid entry. Please try again."); holdScreen(); break;
    }
    displayMenu();
    resp = reader.nextInt();
    }
    }

    public static void holdScreen() throws Exception
    {
    Scanner reader = new Scanner(System.in);
    System.out.println("Press ENTER to continue . . .");
    String dummy = reader.nextLine();
    }

    public static void displayMenu()
    {
    System.out.println("\n\n\n\n\n\n\tThings-To-Do List for Justin Thompson");
    System.out.println("--------------------------------------------------------------");
    System.out.println("\t1. Enter an item");
    System.out.println("\t2. Show the first entry");
    System.out.println("\t3. Remove the first entry ");
    System.out.println("\t4. Length of queue ");
    System.out.println("\t5. Is it full ");
    System.out.println("\t6. Is it empty ");
    System.out.println("\t7. Empty the queue");

    System.out.println("\t8. Display queue contents ");
    System.out.println("\t9. Quit");
    System.out.println("--------------------------------------------------------------\n");

    System.out.println("\tExtra Options");

    System.out.println("--------------------------------------------------------------\n");
    System.out.println("\t10. Empty the queue sequentially");

    System.out.println("--------------------------------------------------------------\n");

    System.out.print("\tEnter your option: ");
    }

    public static void enter1(Scanner scan)
    {
    ListIterator iterator = list.listIterator();
    int count = 0;
    while(iterator.hasNext())
    {
    count++;
    iterator.next();
    }

    if (count != MAX)
    {
    addNode();
    }
    else
    {
    System.out.println("Your list is full. Please clear some items first.");
    }
    }

    public static void addNode()
    {
    Scanner reader = new Scanner(System.in);
    System.out.println("Enter the message:");
    String m = reader.nextLine();
    System.out.println("Enter the priority:");
    int p = reader.nextInt();
    Node newNode = new Node(m,p); // create the node
    ListIterator iterator = list.listIterator();
    boolean inserted = false;

    if (!(iterator.hasNext())) // empty list
    {
    iterator.add(newNode);
    }
    else
    {
    while (iterator.hasNext() && ! inserted) // insert in place
    {
    Node temp = (Node)iterator.next();
    if (p < temp.getPriority())
    { iterator.previous();
    iterator.add(newNode);
    inserted = true;
    }
    }
    if (!inserted) iterator.add(newNode); // insert at end
    }
    }

    public static void showFirst2()
    {
    ListIterator iterator = list.listIterator();
    if (iterator.hasNext())
    {
    System.out.println(iterator.next());
    }
    else
    {
    System.out.println("Your list is empty.");
    }
    }

    public static void removeFirst3()
    {
    ListIterator iterator = list.listIterator();
    if (iterator.hasNext())
    {
    Object i = iterator.next(); // assign the pointer to a var for ref.
    System.out.println(i + " has been removed.");
    iterator.remove();
    }
    else
    {
    // List must be empty.
    System.out.println("Nothing to remove.");
    }
    }

    public static void length4()
    {
    ListIterator iterator = list.listIterator();
    int count=0;
    while (iterator.hasNext())
    {
    count++;
    iterator.next();
    }
    System.out.println("Your list contains: " + count + " items.");
    }

    public static void isFull5()
    {
    ListIterator iterator = list.listIterator();
    int count=0;
    while (iterator.hasNext())
    {
    count++;
    iterator.next();
    }

    if (count == MAX)
    {
    System.out.println("Your list is full.");
    }
    else if (count == 0)
    {
    System.out.println("Your list is empty.");
    }
    else if (count != MAX)
    {
    System.out.println("Your list is not full. List contains "
    + count + " items.");
    }
    }

    public static void isEmpty6()
    {
    ListIterator iterator = list.listIterator();
    int count=0;
    while (iterator.hasNext())
    {
    count++;
    iterator.next();
    }

    if (count == 0)
    {
    System.out.println("Your list is empty.");
    }
    else
    {
    System.out.println("Your list is not empty. List contains "
    + count + " items.");
    }
    }

    public static void deleteAll7()
    {
    list = null;
    System.out.println("Your list has been emptied.");
    }

    public static void deleteAllInSequence()
    {
    ListIterator iterator = list.listIterator();
    int count=0;
    while (iterator.hasNext())
    {
    count++;
    Object i = iterator.next(); // assign the pointer to a var for ref.
    System.out.println(i + " has been removed.");
    iterator.remove();
    }

    if (count == 0)
    {
    System.out.println("Your list is empty.");
    }
    }

    public static void displayAll8()
    {
    ListIterator iterator = list.listIterator();
    int count=0;
    while (iterator.hasNext())
    {
    count++;
    System.out.println(iterator.next());
    }

    if (count == 0)
    {
    System.out.println("Your list is empty.");
    }
    }

    public static void quit9()
    {
    System.exit(1);
    }
    }

    class Node
    {
    private String message;
    private int priority;

    public Node(String m, int p)
    {
    message = m;
    priority = p;
    }

    public int getPriority()
    {
    return priority;
    }

    public String toString()
    {
    return "Message: " + message + ", Priority: " + priority;
    }
    }