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
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
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
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
// 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
// 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
Get step-by-step solutions from verified subject matter experts
