Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 19, 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”.
    • 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.

Blurred answer
Students have asked these similar questions
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?
java 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  6
Hello. Please add a toString() function to the Java class implementation of the singly-linked list which returns the contents of the singly-linked list. Also, does Java have a built-in class that implements singly-linked lists? Thank you. public class SinglyLinkedList<E>{   private static class Node<E>   {      private E element;      private Node<E> next;            public Node(E e, Node<E> n)      {         element = e;         next = n;      }               public E getElement() { return element;}      public Node<E> getNext() { return next;}       public void setNext(Node<E> n) { next = n;}   }         private Node<E> head = null;   private Node<E> tail = null;   private int size = 0;   public SinglyLinkedList() {}   public int size() { return size;}   public boolean isEmpty() { return size == 0;}         public E first()    {      if(isEmpty()) return null;      return head.getElement();   }         public E last()    {…
Knowledge Booster
Background pattern image
Computer Science
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
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning