Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
3rd Edition
ISBN: 9780134038179
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 20, Problem 1PC
Program Plan Intro
Generic Linked Lists
Program Plan:
- Import required package.
- Define “GenericLinkedList” class.
- Define “Node” class.
- Declare a value of a list element in generic type.
- Declare next node in the list using “Node” class.
- Parameterized constructor for “Node” class with two parameters.
- Parameterized constructor for “Node” class with one parameter.
- Declare variable for head of the list.
- Declare variable for last element of list.
- Define the method “clear()” which is used to remove all elements from the list.
- Initializes the index to “0”.
- If the head of the list is not “null”, then set temp node to head of the list and performs “while” loop.
- Remove an element in the list one by one using “while” loop.
- Set “temp_node” to next value of “temp_node”.
- Remove the given element by calling the method “remove”.
- Remove an element in the list one by one using “while” loop.
- Define the method “get()” which is used to return the value at given index in the list.
- Initializes “idx” to “0”.
- If the head of the list is not null, then
- Set head of list to “tempValue”.
- Check condition using “while” loop.
- If “idx” equals to given “index”, then return the element at given position.
- Otherwise, increment the value of “idx” and set “tempValue” to next value of “tempValue”
- Define the method “set()” which is used to replace the value at the specified position with the specified element and also return the previous element.
- Initializes “idx” to “0”.
- Check condition using “while” loop.
- If “idx” equals to given “index”, then replace the element at given position and return the previous element.
- Otherwise, increment the value of “idx” and set “tempValue” to next value of “tempValue”.
- Define the method “isEmpty()” which is used to compute whether list is in empty or not.
- Define the method “size()” which is used to compute the size of list.
- Define the method “add()” which is used to add an element to the list.
- Define the method “remove” which is used to remove an element from the list.
- Define main function.
- Create object for “Scanner” class.
- Create object “list” in “double” type using “GenericLinkedList” class.
- Add elements to “list” using “add()” method.
- Display the elements in list.
- To test the “set” method.
- Initializes the index to “-1”.
- Declare variable in “double” type.
- Performs “do-while” loop.
- Read index string from user.
- Convert the string value in integer.
- If the user entered index is less than “0” or greater than or equal to “size”, then display an error message.
- Read the new element for given index.
- Convert the string to double.
- Replace the new element in the place of old element by calling the method “set”.
- Display the old element in the given index.
- Display the list after calling “set” method.
-
- To test the “get” method.
- Initializes the index to “-1”.
- Performs “do-while” loop.
- Read the index from user.
- Convert the string to integer.
- If the user entered index is less than “0” or greater than or equal to “size”, then display an error message
- Display the element at given index by calling the method “get()”.
- Clear all the elements in the list by calling the method “clear()”.
- Display the size of list after clearing the list by calling “size()” method.
- To test the “get” method.
-
- Define “Node” class.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
data structure-JAVA
Part2: LinkedList implementation
1. Create a linked list of type String Not Object and name it as StudentName.
2. Add the following values to linked list above Jack Omar Jason.
3. Use addFirst to add the student Mary.
4. Use addLast to add the student Emily.
5. Print linked list using System.out.println().
6. Print the size of the linked list.
7. Use removeLast.
8. Print the linked list using for loop for (String anobject: StudentName){…..}
9. Print the linked list using Iterator class.
10. Does linked list hava capacity function?
11. Create another linked list of type String Not Object and name it as TransferStudent.
12. Add the following values to TransferStudent linked list Sara Walter.
13. Add the content of linked list TransferStudent to the end of the linked list StudentName
14. Print StudentName.
15. Print TransferStudent.
16. What is the shortcoming of using Linked List?
6. Suppose that we have defined a singly linked list class that contains a list of unique integers in
ascending order. Create a method that merges the integers into a new list.
Note the additional requirements listed below.
Notes:
●
.
Neither this list nor other list should change.
The input lists will contain id's in sorted order. However, they may contain duplicate values. For
example, other list might contain id's .
You should not create duplicate id's in the list. Important: this list may contain duplicate id's,
and other list may also contain duplicate id's. You must ensure that the resulting list does not
contain duplicates, even if the input lists do contain duplicates.
Chapter 20 Solutions
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Ch. 20.1 - Prob. 20.1CPCh. 20.1 - Prob. 20.2CPCh. 20.3 - Prob. 20.4CPCh. 20 - A list is a collection that _____. a. associates...Ch. 20 - Prob. 2MCCh. 20 - Prob. 3MCCh. 20 - Prob. 4MCCh. 20 - Prob. 5MCCh. 20 - Prob. 6MCCh. 20 - Prob. 7MC
Ch. 20 - Prob. 11TFCh. 20 - Prob. 12TFCh. 20 - Prob. 13TFCh. 20 - Prob. 14TFCh. 20 - Prob. 15TFCh. 20 - Prob. 16TFCh. 20 - Prob. 17TFCh. 20 - Prob. 18TFCh. 20 - Prob. 29TFCh. 20 - Prob. 20TFCh. 20 - Prob. 1FTECh. 20 - Prob. 2FTECh. 20 - Prob. 3FTECh. 20 - Prob. 4FTECh. 20 - Prob. 5FTECh. 20 - Prob. 1AWCh. 20 - Prob. 2AWCh. 20 - Prob. 3AWCh. 20 - Prob. 4AWCh. 20 - Prob. 3SACh. 20 - Prob. 4SACh. 20 - Prob. 5SACh. 20 - Consult the online Java documentation and...Ch. 20 - Prob. 1PCCh. 20 - Prob. 2PC
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- public LLNode secondHalf(LLNode head) { }arrow_forwardInstruction: To test the Linked List class, create a new Java class with the main method, generate Linked List using Integer and check whether all methods do what they’re supposed to do. A sample Java class with main method is provided below including output generated. If you encounter errors, note them and try to correct the codes. Post the changes in your code, if any. Additional Instruction: Linked List is a part of the Collection framework present in java.util package, however, to be able to check the complexity of Linked List operations, we can recode the data structure based on Java Documentation https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html package com.linkedlist; public class linkedListTester { public static void main(String[] args) { ListI<Integer> list = new LinkedList<Integer>(); int n=10; for(int i=0;i<n;i++) { list.addFirst(i); } for(int…arrow_forwardProblem Description: Q1) Write a method public static void downsize (LinkedList employeeNames, int n) that removes every nth employee from a linked list. Q2) Write a method public static void reverse (LinkedList strings) that reverses the entries in a linked list.arrow_forward
- 1- A new static method with one parameter which is a head node for a linked list of integers, the method should create a new linked list which is equivalent to the original list of integers but with all repetitions removed, the method's return value is a head reference for the new list. this is what I have but it's not removing repetitions. public static IntLinkedBag removeRepetition (IntLinkedBag b1) { IntNode next; IntNode head = null; LinkedList<Integer> s = new LinkedList<>(); IntNode new_list = new IntNode(0, null); IntNode now = head; IntNode prev = new_list; while (now!= null) { int x = now.data; if (s.contains(x)) { new_list = new_list.next; } else { s.add(x); new_list.next = new IntNode(x, now); new_list = new_list.next; } now = now.next; } return b1; } }…arrow_forwardjava program java method: Write a method replace to be included in the class KWLinkedList (for doubly linked list) that accepts two parameters, searchItem and repItem of type E. The method searches for searchItem in the doubly linked list, if found then replace it with repItem and return true. If the searchItem is not found in the doubly linked list, then insert repItem at the end of the linked list and return false. Assume that the list is not empty. You can use ListIterator and its methods to search the searchItem in the list and replace it with repItem if found. Do not call any method of class KWLinkedList to add a new node at the end of the list. Method Heading: public boolean replace(E searchItem, E repItem) Example: searchItem: 15 repItem: 17 List (before method call): 9 10 15 20 4 5 6 List (after method call) : 9 10 17 20 4 5 6arrow_forwardhelparrow_forward
- Javaarrow_forwardCreate a class of type Generic so that it has a Get method that receive a list regardless of the type of this list. Print the list, remove the first and the last element in the list, reverse the list and finally print the list After modifications. Proposed methodologies:Generic Class. Technologies: - C#- Visual Studioarrow_forwardJava language Write a method to insert an array of elements at index in a single linked list and then display this list. The method receives this array by parametersarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education