Question: implement Stack three ways each: o simple linked list o using Java class LinkedList o using the Java Stack implement a Queue two ways each:

implement Stack three ways each:

o simple linked list

o using Java class LinkedList<>

o using the Java Stack<>

implement a Queue two ways each:

o simple linked list o using Java class LinkedList<>

implement a Doubly linked list

I'm trying to search a word in my stack from a txt file but when ever I run the program it seem as if it's not searching at all. JAVA import java.util.*; import java.nio.file.*; import java.io.*; import java.text.*;

public class P4Program{ private DecimalFormat formatter; // Formation for the numbers private String fileName; private String word; private String outputFile; // File name for the Output file private String outputString; private final String LS = System.lineSeparator(); private int count; private double startTime; // Wall clock starting time for int private double endTime; // Wall clock ending time for int private double startSystemTime; // CPU clock startindg time for int private double endSystemTime; // CPU clock starting time for float private double lapsedTime_SLL; private double lapsedTime_LL; private double lapsedTime_SC; private double lapsedTime_SLL_Que; private double lapsedTime_LL_Que; private double lapsedTime_SLL_ForCpu; private double lapsedTime_LL_ForCpu; private double lapsed_SC_ForCpu; private double lapsed_SLL_Que_ForCpu; private double lapsedTime_LL_Que_ForCpu; private double lapsed_DL; private double lapsed_DL_ForCpu; private final double NON_SECONDS = 1_000_000_000.0; private final double MILLI_SECONDS = 1_000.0;

/** * Instantiate all the data structures we'll be testing. */ public P4Program(){ formatter = new DecimalFormat("0.0000"); fileName = null; word = null; outputFile = "P4Output.txt"; count = 1; }// end of constructor public void mainMenu(){ Scanner scan = new Scanner(System.in); SimpleLinkedListStack list = new SimpleLinkedListStack (); P4Program loadList = new P4Program(); //System.out.println("Search word is..." + searchWord()); //System.out.println("Please enter the Input file name... "); //fileName = scan.nextLine(); //outputToFile(); //loadList.linkedStackFromFile(); //loadList.stackFromFile(); //loadList.linkedListFromFile_Queue(); //loadList.simpleLinklistFromFile(); //loadList.simpleLinkedlistFromFile_Queue(); //loadList.doublyLinkedFromFile(); } public String searchWord(){ Scanner scan = new Scanner(System.in); SimpleLinkedListStack list = new SimpleLinkedListStack (); LinkedStack listOne = new LinkedStack(); Stack listTwo = new Stack(); DoublyLinkedList listThree = new DoublyLinkedList(); SimpleLinkedListQueue listFour = new SimpleLinkedListQueue(); LinkedQueue listFive = new LinkedQueue(); System.out.println("Please enter the search word... "); word = scan.nextLine(); LinkedStack temp = listOne; SimpleLinkedListStack tempOne = list; Stack tempTwo = listTwo; DoublyLinkedList tempThree = listThree; LinkedQueue tempFour = listFive; SimpleLinkedListQueue tempFive = listFour; while (!temp.isEmpty()) { if (temp.pop() == word){ System.out.print("The word from linked stack is " + word); } } /**else if(tempOne.pop() == word){ System.out.print("The word from simple linked list is " + word); } else if(tempTwo.pop() == word){ System.out.print("The word from stack class is " + word); } else if(tempThree.printList() == word){ System.out.print("The word from stack class is " + word); } else if(tempFour.dequeue() == word){ System.out.print("The word from stack class is " + word); } else if(tempFive.dequeue() == word){ System.out.print("The word from stack class is " + word); } else{ System.out.println("No such word from the list"); } }*/ System.out.println(" search word... " + word); return word; } public int searchKey(){ Scanner scan = new Scanner(System.in); SimpleLinkedListStack list = new SimpleLinkedListStack (); LinkedStack listOne = new LinkedStack(); Stack listTwo = new Stack(); DoublyLinkedList listThree = new DoublyLinkedList(); SimpleLinkedListQueue listFour = new SimpleLinkedListQueue(); LinkedQueue listFive = new LinkedQueue(); int key = 0; System.out.println("Please enter the search word... "); key = scan.nextInt(); LinkedStack temp = listOne; SimpleLinkedListStack tempOne = list; Stack tempTwo = listTwo; DoublyLinkedList tempThree = listThree; LinkedQueue tempFour = listFive; SimpleLinkedListQueue tempFive = listFour; while (!temp.isEmpty()) { if (count == key){ System.out.print("The key from linked stack is " + key); } else if(count == key){ System.out.print("The key from simple linked list is " + key); } else if(count == key){ System.out.print("The key from stack class is " + key); } else if(count == key){ System.out.print("The key from stack class is " + key); } else if(count == key){ System.out.print("The key from stack class is " + key); } else if(count == key){ System.out.print("The key from stack class is " + key); } else{ System.out.println("No such word from the list"); } } return key; } public void simpleLinklistFromFile(){ String content = new String(); int count = 1; File file = new File("P4Input.txt"); SimpleLinkedListStack list = new SimpleLinkedListStack (); try { Scanner sc = new Scanner(new FileInputStream(file)); while (sc.hasNextLine()){ content = sc.nextLine(); list.push(content); } sc.close(); }catch(FileNotFoundException fnf){ fnf.printStackTrace(); } catch (Exception e) { e.printStackTrace(); System.out.println(" Program terminated Safely..."); } SimpleLinkedListStack temp = list; while (!temp.isEmpty()) { System.out.print("Node " + (count++) + " : "); System.out.println(temp.pop()); } endTime = System.currentTimeMillis(); endSystemTime = System.nanoTime(); lapsedTime_SLL = (endTime - startTime)/MILLI_SECONDS; lapsedTime_SLL_ForCpu = (endSystemTime - startSystemTime)/NON_SECONDS; } public void stackFromFile(){ String content = new String(); int count = 1; File file = new File("P4Input.txt"); Stack list = new Stack(); lapsedTime_SC = 0.0; lapsed_SC_ForCpu = 0.0; startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); try { Scanner sc = new Scanner(new FileInputStream(file)); while (sc.hasNextLine()){ content = sc.nextLine(); list.push(content); } sc.close(); }catch(FileNotFoundException fnf){ fnf.printStackTrace(); } catch (Exception e) { e.printStackTrace(); System.out.println(" Program terminated Safely..."); } Stack temp = list; while (!temp.isEmpty()) { System.out.print("Node " + (count++) + " : "); System.out.println(temp.pop()); } endTime = System.currentTimeMillis(); endSystemTime = System.nanoTime(); lapsedTime_SC = (endTime - startTime)/MILLI_SECONDS; lapsed_SC_ForCpu = (endSystemTime- startSystemTime)/NON_SECONDS; } public void doublyLinkedFromFile(){ lapsed_DL = 0.0; lapsed_DL_ForCpu = 0.0; String content = new String(); int count = 1; File file = new File(fileName); DoublyLinkedList list = new DoublyLinkedList(); startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); try { Scanner sc = new Scanner(new FileInputStream(file)); while (sc.hasNextLine()){ content = sc.nextLine(); list.addToFront(content); } sc.close(); }catch(FileNotFoundException fnf){ fnf.printStackTrace(); } catch (Exception e) { e.printStackTrace(); System.out.println(" Program terminated Safely..."); } DoublyLinkedList temp = list; while (!temp.isEmpty()) { System.out.print("Node " + (count++) + " : "); System.out.println(temp.printList()); } endTime = System.currentTimeMillis(); endSystemTime = System.nanoTime(); lapsed_DL = (endTime - startTime)/MILLI_SECONDS; lapsed_DL_ForCpu = (endSystemTime - startSystemTime)/NON_SECONDS; } public void linkedStackFromFile(){ lapsedTime_LL = 0.0; lapsedTime_LL_ForCpu = 0.0; String content = new String(); int count = 1; File file = new File("P4Input.txt"); LinkedStack list = new LinkedStack(); startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); try { Scanner sc = new Scanner(new FileInputStream(file)); while (sc.hasNextLine()){ content = sc.nextLine(); list.push(content); } sc.close(); }catch(FileNotFoundException fnf){ fnf.printStackTrace(); } catch (Exception e) { e.printStackTrace(); System.out.println(" Program terminated Safely..."); } LinkedStack temp = list; while (!temp.isEmpty()) { System.out.print("Node " + (count++) + " : "); System.out.println(temp.pop()); } /**while (!temp.isEmpty()) { String found = temp.pop(); if ( found == word){ System.out.println("The word from linked stack is " + word); } }*/ endTime= System.currentTimeMillis(); endSystemTime = System.nanoTime(); lapsedTime_LL = (endTime - startTime)/MILLI_SECONDS; lapsedTime_LL_ForCpu = (endSystemTime - startSystemTime)/NON_SECONDS; }

public void linkedListFromFile_Queue(){ lapsedTime_LL_Que = 0; lapsedTime_LL_Que_ForCpu = 0; String content = new String(); int count = 1; File file = new File(fileName); LinkedQueue list = new LinkedQueue(); startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); try { Scanner sc = new Scanner(new FileInputStream(file)); while (sc.hasNextLine()){ content = sc.nextLine(); list.enqueue(content); } sc.close(); }catch(FileNotFoundException fnf){ fnf.printStackTrace(); } catch (Exception e) { e.printStackTrace(); System.out.println(" Program terminated Safely..."); } LinkedQueue temp = list; while (!temp.isEmpty()) { System.out.print("Node " + (count++) + " : "); System.out.println(temp.dequeue()); } endTime = System.currentTimeMillis(); endSystemTime = System.nanoTime(); lapsedTime_LL_Que = (endTime - startTime)/MILLI_SECONDS; lapsedTime_LL_Que_ForCpu = (endSystemTime - startSystemTime)/NON_SECONDS; } public void simpleLinkedlistFromFile_Queue(){ lapsed_SLL_Que_ForCpu = 0.0; lapsedTime_SLL_Que = 0.0; String key = ""; String content = new String(); int count = 1; File file = new File(fileName); SimpleLinkedListQueue list = new SimpleLinkedListQueue(); startTime = System.currentTimeMillis(); startSystemTime = System.nanoTime(); try { Scanner sc = new Scanner(new FileInputStream(file)); while (sc.hasNextLine()){ content = sc.nextLine(); list.enqueue(content); } sc.close(); }catch(FileNotFoundException fnf){ fnf.printStackTrace(); } catch (Exception e) { e.printStackTrace(); System.out.println(" Program terminated Safely..."); } SimpleLinkedListQueue temp = list; while (!temp.isEmpty()) { System.out.print("Node " + (count++) + " : "); System.out.println(temp.dequeue()); } endTime = System.currentTimeMillis(); endSystemTime = System.nanoTime(); lapsedTime_SLL_Que = (endTime - startTime)/MILLI_SECONDS; lapsed_SLL_Que_ForCpu = (endSystemTime - startSystemTime)/NON_SECONDS; } /** * Prints the report with the data from the trial */

public void outputToFile(){ outputString = "There are 20,068 words in the list" + LS + "Stack " + "-" + " simple linked list: " + formatter.format(lapsedTime_SLL) + " seconds CPU time " + formatter.format(lapsedTime_SLL_ForCpu) + " seconds wall clock" + LS + "Stack " + "-" + " linkedList: " + formatter.format(lapsedTime_LL) + " seconds CPU time " + formatter.format(lapsedTime_LL_ForCpu) + " seconds wall clock" + LS + "Stack " + "-" + " stack<> class: " + formatter.format(lapsedTime_SC) + " seconds CPU time " + formatter.format(lapsed_SC_ForCpu) + " seconds wall clock " + LS + "Queue " + "-" + " simple linked list: " + formatter.format(lapsedTime_SLL_Que) + " seconds CPU time " + formatter.format(lapsed_SLL_Que_ForCpu) + " seconds wall clock" + LS + "Queue " + "-" + " linked List: " + formatter.format(lapsedTime_LL_Que) + " seconds CPU time " + formatter.format(lapsedTime_LL_Que_ForCpu)+ " seconds wall clock " + LS + "Double List: " + formatter.format(lapsed_DL) + " seconds CPU time " + formatter.format(lapsed_DL_ForCpu) + " seconds wall clock" + LS + LS; //LS + "The word is " + searchWord() + "and the key is " + searchKey() + LS + LS; System.out.println(outputString); writeReport(outputString); }// end of outputToFile /** * Writes the report with the data from the trial */ private void writeReport(String reportString){ try{ BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fileName), true)); writer.write(reportString); writer.close(); }catch(Exception ex){ System.out.println("try a different file name"); } }

public void printList (){ int count = 1; Stack list = new Stack(); Stack temp = list; stackFromFile(); while (!temp.isEmpty()) { System.out.print("Node " + (count++) + " : "); System.out.println(temp.pop()); } }

public static void main (String [] arg)throws IOException{ P4Program mainClass = new P4Program(); mainClass.mainMenu(); mainClass.printList(); //mainClass.searchWord(); //mainClass.linkedStackFromFile(); //mainClass.stackFromFile(); //mainClass.linkedListFromFile_Queue(); //mainClass.simpleLinklistFromFile(); //mainClass.simpleLinkedlistFromFile_Queue(); //mainClass.outputToFile(); //mainClass.doublyLinkedFromFile(); } }

import java.util.*; import java.io.*; public class SimpleLinkedListStack { private int n; // size of the stack private Node first; // top of stack

// helper linked list class private class Node { private String item; private Node next; }

/** * Initializes an empty stack. */ public SimpleLinkedListStack() { first = null; n = 0; } /** * Adds the item to this stack. * @param item the item to add */ public void push(String item) { Node oldfirst = first; first = new Node(); first.item = item; first.next = oldfirst; n++; } /** * Removes and returns the item most recently added to this stack. * @return the item most recently added * @throws java.util.NoSuchElementException if this stack is empty */ public String pop() { if (isEmpty()) throw new NoSuchElementException("Stack underflow"); String item = first.item; // save item to return first = first.next; // delete first node n--; return item; // return the saved item }

/** * Is this stack empty? * @return true if this stack is empty; false otherwise */ public boolean isEmpty() { return first == null; }

}

import java.io.*; import java.util.*;

public class SimpleLinkedListQueue{ private int n; // number of elements on queue private Node first; // beginning of queue private Node last; // end of queue

// helper linked list class private class Node { private String item; private Node next; }

/** * Initializes an empty queue. */ public SimpleLinkedListQueue() { first = null; last = null; n = 0; }

/** * Is this queue empty? * @return true if this queue is empty; false otherwise */ public boolean isEmpty() { return first == null; } /** * Adds the item to this queue. * @param item the item to add */ public void enqueue(String item) { Node oldlast = last; last = new Node(); last.item = item; last.next = null; if (isEmpty()) first = last; else oldlast.next = last; n++; }

/** * Removes and returns the item on this queue that was least recently added. * @return the item on this queue that was least recently added * @throws java.util.NoSuchElementException if this queue is empty */ public String dequeue() { if (isEmpty()) throw new NoSuchElementException("Queue underflow"); String item = first.item; first = first.next; n--; if (isEmpty()) last = null; // to avoid loitering return item; } }

import java.util.*; import java.io.*; public class DoublyLinkedList { Node head; // head of list /* Doubly Linked list Node*/ public class Node { public String item; private Node prev; private Node next; // Constructor to create a new node // next and prev is by default initialized as null public Node(String data){ item = data; } } //Adding a node at the front of the list public void addToFront(String new_data) { /* 1. allocate node * 2. put in the data */ Node new_Node = new Node(new_data); /* 3. Make next of new node as head and previous as NULL */ new_Node.next = head; new_Node.prev = null; /* 4. change prev of head node to new node */ if(head != null) head.prev = new_Node; /* 5. move the head to point to the new node */ head = new_Node; } // This method prints the list. public String printList() { if (isEmpty()) throw new NoSuchElementException("List underflow"); Node last = null; Node current = head; while(current != null) { System.out.println(current.item + " "); last = current; current = current.next; } System.out.println(); return current.item; } /** * Is this queue empty? * @return true if this queue is empty; false otherwise */ public boolean isEmpty() { return head == null; } }

import java.io.*; import java.util.*; public class LinkedStack{ private int n; // size of the stack private Node first; // top of stack

// helper linked list class private class Node { private Item item; private Node next; }

/** * Initializes an empty stack. */ public LinkedStack() { first = null; n = 0; }

/** * Is this stack empty? * @return true if this stack is empty; false otherwise */ public boolean isEmpty() { return first == null; }

/** * Returns the number of items in the stack. * @return the number of items in the stack */ public int size() { return n; }

/** * Adds the item to this stack. * @param item the item to add */ public void push(Item item) { Node oldfirst = first; first = new Node(); first.item = item; first.next = oldfirst; n++; }

/** * Removes and returns the item most recently added to this stack. * @return the item most recently added * @throws java.util.NoSuchElementException if this stack is empty */ public Item pop() { if (isEmpty()) throw new NoSuchElementException("Stack underflow"); Item item = first.item; // save item to return first = first.next; // delete first node n--; return item; // return the saved item } }

import java.io.*; import java.util.*; public class LinkedQueue{ private int n; // number of elements on queue private Node first; // beginning of queue private Node last; // end of queue

// helper linked list class private class Node { private Item item; private Node next; }

/** * Initializes an empty queue. */ public LinkedQueue() { first = null; last = null; n = 0; }

/** * Is this queue empty? * @return true if this queue is empty; false otherwise */ public boolean isEmpty() { return first == null; }

/** * Returns the number of items in this queue. * @return the number of items in this queue */ public int size() { return n; } /** * Adds the item to this queue. * @param item the item to add */ public void enqueue(Item item) { Node oldlast = last; last = new Node(); last.item = item; last.next = null; if (isEmpty()) first = last; else oldlast.next = last; n++; }

/** * Removes and returns the item on this queue that was least recently added. * @return the item on this queue that was least recently added * @throws java.util.NoSuchElementException if this queue is empty */ public Item dequeue() { if (isEmpty()) throw new NoSuchElementException("Queue underflow"); Item item = first.item; first = first.next; n--; if (isEmpty()) last = null; // to avoid loitering return item; } }

import java.util.*; import java.io.*; public class DoublyLinkedList { Node head; // head of list /* Doubly Linked list Node*/ public class Node { public String item; private Node prev; private Node next; // Constructor to create a new node // next and prev is by default initialized as null public Node(String data){ item = data; } } //Adding a node at the front of the list public void addToFront(String new_data) { /* 1. allocate node * 2. put in the data */ Node new_Node = new Node(new_data); /* 3. Make next of new node as head and previous as NULL */ new_Node.next = head; new_Node.prev = null; /* 4. change prev of head node to new node */ if(head != null) head.prev = new_Node; /* 5. move the head to point to the new node */ head = new_Node; } // This method prints the list. public String printList() { if (isEmpty()) throw new NoSuchElementException("List underflow"); Node last = null; Node current = head; while(current != null) { System.out.println(current.item + " "); last = current; current = current.next; } System.out.println(); return current.item; } /** * Is this queue empty? * @return true if this queue is empty; false otherwise */ public boolean isEmpty() { return head == null; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!