Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CSE 205 ASSIGNMENT 10 LINKED LIST // Assignment #: 10 // Name: Your name // StudentID: Your id // Lab Lecture: Your lecture // Description:

CSE 205 ASSIGNMENT 10 LINKED LIST

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

// Assignment #: 10 // Name: Your name // StudentID: Your id // Lab Lecture: Your lecture // Description: A linked list is a sequence of nodes with efficient // element insertion and removal. // This class contains a subset of the methods of the // standard java.util.LinkedList class. import java.util.*; public class LinkedList { /ested class to represent a node private class Node { public Object data; public Node next; } //only instance variable that points to the first node. private Node first; // Constructs an empty linked list. public LinkedList() { first = null; } // Returns the first element in the linked list. public Object getFirst() { if (first == null) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else return first.data; } // Removes the first element in the linked list. public Object removeFirst() { if (first == null) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else { Object element = first.data; first = first.next; //change the reference since it's removed. return element; } } // Adds an element to the front of the linked list. public void addFirst(Object element) { //create a new node Node newNode = new Node(); newNode.data = element; newNode.next = first; //change the first reference to the new node. first = newNode; } // Returns an iterator for iterating through this list. public ListIterator listIterator() { return new LinkedListIterator(); } /********************************************************* Add your methods here *********************************************************/ /ested class to define its iterator private class LinkedListIterator implements ListIterator { private Node position; //current position private Node previous; //it is used for remove() method // Constructs an iterator that points to the front // of the linked list. public LinkedListIterator() { position = null; previous = null; } // Tests if there is an element after the iterator position. public boolean hasNext() { if (position == null) /ot traversed yet { if (first != null) return true; else return false; } else { if (position.next != null) return true; else return false; } } // Moves the iterator past the next element, and returns // the traversed element's data. public Object next() { if (!hasNext()) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else { previous = position; // Remember for remove if (position == null) position = first; else position = position.next; return position.data; } } // Adds an element after the iterator position // and moves the iterator past the inserted element. public void add(Object element) { if (position == null) /ever traversed yet { addFirst(element); position = first; } else { //making a new node to add Node newNode = new Node(); newNode.data = element; newNode.next = position.next; //change the link to insert the new node position.next = newNode; //move the position forward to the new node position = newNode; } //this means that we cannot call remove() right after add() previous = position; } // Removes the last traversed element. This method may // only be called after a call to the next() method. public void remove() { if (previous == position) /ot after next() is called { IllegalStateException ex = new IllegalStateException(); throw ex; } else { if (position == first) { removeFirst(); } else { previous.next = position.next; //removing } //stepping back //this also means that remove() cannot be called twice in a row. position = previous; } } // Sets the last traversed element to a different value. public void set(Object element) { if (position == null) { NoSuchElementException ex = new NoSuchElementException(); throw ex; } else position.data = element; } } //end of LinkedListIterator class } //end of LinkedList class 

// Assignment #: 10 // Name: Your name // StudentID: Your id // Lab Lecture: Your lecture // Description: The ListIterator interface allows access of a position in a linked list. // This interface contains a subset of the methods of the // standard java.util.ListIterator interface. The methods for // backward traversal are not included. public interface ListIterator { //Move Moves the iterator past the next element. Object next(); // Tests if there is an element after the iterator position. boolean hasNext(); // Adds an element before the iterator position // and moves the iterator past the inserted element. void add(Object element); // Removes the last traversed element. This method may // only be called after a call to the next() method. void remove(); // Sets the last traversed element to a different value. void set(Object element); } 

// Assignment #: 10 // Name: Your name // StudentID: Your id // Lab Lecture: Your lecture // Description: The Assignment 10 class displays a menu of choices to a user // and performs the chosen task. It will keep asking a user to // enter the next choice until the choice of 'Q' (Quit) is // entered. import java.io.*; public class Assignment10 { public static void main(String[] args) { char input1; String inputInfo = new String(); int operation2; String line = new String(); //create a linked list to be used in this method. LinkedList list1 = new LinkedList(); try { // print out the menu printMenu(); // create a BufferedReader object to read input from a keyboard BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); do { System.out.print("What action would you like to perform? "); line = stdin.readLine().trim(); //read a line input1 = line.charAt(0); input1 = Character.toUpperCase(input1); if(line.length() == 1) { //check if a user entered only one character switch(input1) { case 'A': //Add String System.out.print("Please enter a string to add: "); String str1 = stdin.readLine().trim(); System.out.print("Please enter an index to add: "); inputInfo = stdin.readLine().trim(); int addIndex = Integer.parseInt(inputInfo); list1.addElement(addIndex, str1); break; case 'D': //Search for the Index of a String System.out.print("Please enter a string to search: "); inputInfo = stdin.readLine().trim(); operation2=list1.searchElement(inputInfo); if(operation2 > -1) System.out.print("string found at " + operation2 + " "); else System.out.print("string not found "); break; case 'E': //Search for String at an Index System.out.print("Please enter an index to search: "); inputInfo = stdin.readLine().trim(); int searchIndex = Integer.parseInt(inputInfo); System.out.print("string at the given index is " + list1.getElement(searchIndex) + " "); break; case 'L': //List Strings System.out.print(list1.toString()); break; case 'O': // List Current Size System.out.print("The current size is " + list1.size() + " "); break; case 'Q': break; //Quit case 'R': //Remove Last Few System.out.print("Please enter a number of elements to be removed from the end of the linked list: "); inputInfo = stdin.readLine().trim(); int howMany = Integer.parseInt(inputInfo); list1.removeLastFew(howMany); break; case 'S': //Set String System.out.print("Please enter a string to set: "); String str2 = stdin.readLine().trim(); System.out.print("Please enter an index to set: "); inputInfo = stdin.readLine().trim(); int setIndex = Integer.parseInt(inputInfo); list1.setElement(setIndex, str2); break; case 'T': //Reverse list1.reverse(); System.out.print("list reversed "); break; case '?': printMenu(); break; //Display Menu default: System.out.print("Unknown action "); break; } // switch(input1) } else System.out.print("Unknown action "); } while (input1 != 'Q' || line.length() != 1); // do-while } catch (IOException exception) { System.out.print("IO Exception "); } // try-catch(IOException) } // main() /** The method printMenu displays the menu to a user **/ public static void printMenu() { System.out.print("Choice\t\tAction " + "------\t\t------ " + "A\t\tAdd String " + "D\t\tSearch for Index " + "E\t\tSearch for String " + "L\t\tList Strings " + "O\t\tList Current Size " + "Q\t\tQuit " + "R\t\tRemove Last Few " + "S\t\tSet String " + "T\t\tReverse " + "?\t\tDisplay Help "); } // printMenu() } // class Assignment10 
2017xMicrosoft Word- Hw Chegg Study | Guided S. x zichang- C Secure https:/ m asucourses asu.edu/bbcswebdav pid 1/0 6943 dt-content-rid-1 1441 3911 1/courses/201 /Hall-1-CSL205 /41 26/HW10 pdf ::: Apps = : 26 (19)Double integra O intermediate lava i-: 20tT Male, m lestn gram Chin tOne ted Pr ED Microsoft Word -HW10 Minimal Submitted Files Importan: This is an individual assignment. Please do not collaborate. No late assignment will be accepted. Make sure that you write every line of your code. Using code writen by someone else will be sidered a violation of the academic integrity and wil resuls in a rt to the Dean's You are required, but not limited, to turn in the following source files Assignment10java (This file does not need to be modified) LinkedList.iava (It needs to he modified) Listlteratorjava (This file does not need to be modified) Requirements to get full credits in Documentation 1. The assignment number, your name, StudentID, Lecture number, and a class description need to be included at the top of each file/class A description of each method is also needed. Some additional comments inside of long methods (such as a "main" method) to explain codes that are hard to follow should be written. 2. 3. You can look at the Java programs in the text book to see how comments are added to New Skills to be Applied In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed: Linked Lists Program Description Class Diagram: Activate Windows ta Settinas to activate Wind

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications Dexa 2021 Workshops Biokdd Iwcfs Mlkgraphs Al Cares Protime Alsys 2021 Virtual Event September 27 30 2021 Proceedings

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Atif Mashkoor ,Johannes Sametinger ,Anna Fensel ,Jorge Martinez-Gil ,Lukas Fischer

1st Edition

ISBN: 3030871002, 978-3030871000

More Books

Students also viewed these Databases questions

Question

Describe Balor method and give the chemical reaction.

Answered: 1 week ago

Question

How to prepare washing soda from common salt?

Answered: 1 week ago