Question

Implement an array-based Linked List in Java. Use double as the item. You need to create...

Implement an array-based Linked List in Java. Use double as the item. You need to create a driver includes several items and inserts them in order in a list.

  1. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods.
  2. DO NOT USE your language's Library List. You will receive zero points.
  3. Write a LinkedList class
  4. Write a driver (tester) call LinkListedDriver to show you have implemented all the necessary methods
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The above problem can be solved in the following steps-

STEP 1- First we will define a class LinkedList. Inside this class we will define a node class named LLNode. This LLNode class will have two variables value and next. Value stores int value and next stores next LLNode.

STEP 2- Then we write the function codes for inserting node at last, inserting node at beginning, deleting a node with a particular value and printing values of linked list.

The actions taken in functions have been mentioned in comments for proper understanding.

STEP 3- Then we created another class which is the tester class- LinkedListDriver class. In the main method of this class, we create an instance of LinkedList class.

And then we start calling the insertLast() and insertBeginning() functions with values. Then display the linked list. And then delete a particular node and again display the linked list.

JAVA CODE-

LinkedList.java

//defined class LinkedList
public class LinkedList {
   //defined a class LLNode that stores value and next LLNode
   class LLNode{
   //store value
   double value;
   //stores next LLNode
   LLNode next;
   //constructor
   LLNode(double val){
   value = val;
   next = null;
   }
   }
   //declare a LLNode Object to store the head of LinkedList
   public LLNode headNode = null;
  
   //function to insert value at the beginning of LinkedList
   public void insertBeginning(double value){
   //create an object of LLNode
       LLNode newNode = new LLNode(value);
       //make this newNode as headNode
       newNode.next = headNode;
       headNode = newNode;
   }
   //function to insert value at the last of LinkedList
   public void insertLast(double value){
   //create an object of LLNode
   LLNode newNode = new LLNode(value);
//check if headNode is null
   if(headNode == null){
   //then this newNode becomes headNode
   headNode = newNode;
   }
   //otherwise
   else{
   //store headNode in current
       LLNode current = headNode;
       //move current to last of LinkedList
       while(current.next != null){
           current = current.next;
       }
       //assign newNode at the last
   current.next = newNode;
   }
   }
   //function to delete a node with value=valToDel
   public void deleteNode(double valToDel){
   //store headNode in curr
       LLNode curr = headNode,prev = null;
       //check if curr is null and value in curr is valToDel at the same time
       if(curr!=null && curr.value == valToDel){
       //then make the headNode as next to headNode
           headNode = headNode.next;
           //end function by returning
           return;
       }
       //now run a loop till we find the node with value=valToDel
       while(curr!=null && curr.value != valToDel){
       //store curr in previous node
           prev = curr;
           //and move curr to next node
           curr = curr.next;
       }
       //check if curr is null, meaning valToDel is not found in any node
       //then end function call by returning
       if(curr == null) return;
       //assing next of prev as next of curr
       prev.next = curr.next;
      
   }
   //function to display the content of LinkedList
   public void display(){
   //store the headNode as current
   LLNode current = headNode;
   //check if current is null, means LinkedList is Empty
   if(current == null){
   System.out.println("Empty list");
   }
   //iterat over all elements in LinkedList
   while(current!=null){
   //print the value in node
   System.out.print(current.value+ " ");
   //move to next node
   current = current.next;
   }
   //change line after printing every node
   System.out.println();
   }
  
}

Image of Code-

LinkedListDriver.java


//LinkedListDriver class to test the LinkedList class
//it has all function calls to LinkedList functions
public class LinkedListDriver {
   //driver method
   public static void main(String args[]){
       //create an instance of LinkedList class
       LinkedList lld = new LinkedList();
       //insert 2.5 at last
       lld.insertLast(2.5);
       //insert 3.8 at last
   lld.insertLast(3.8);
   //insert 7.21 at beginning of linked list
   lld.insertBeginning(7.21);
   //insert 1.65 at beginning of linked list
   lld.insertBeginning(1.65);
   //print the contents of Linked list
   System.out.println("Linked list after insertions : ");
   lld.display();
  
   //delete the node with values=7.21
   lld.deleteNode(7.21);
   //print the contents of linked list after deletion
   System.out.println("Linked list after deletion : ");
   lld.display();
  
   }
}

Image of Code-

OUTPUT-

If this answer helps, please give an up vote and feel free to comment for any query.

Add a comment
Know the answer?
Add Answer to:
Implement an array-based Linked List in Java. Use double as the item. You need to create...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Any help with this is appriciated Array-Based Linked List Implementation Implement an array-based Linked List in...

    Any help with this is appriciated Array-Based Linked List Implementation Implement an array-based Linked List in Java. Use your Use your Can class as a JAR. You need to create a driver that makes several cans and places them in alphabetical order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. DO NOT USE Java's List. You will receive zero points....

  • Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a...

    Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...

  • ANSWER IN JAVA ASAP 1.Implement a stack on the singly linked list with the operations of...

    ANSWER IN JAVA ASAP 1.Implement a stack on the singly linked list with the operations of Lab Assignment 1. Hint: Using the same Stack class you implemented, change the array to an object of the singly linked list class. The functionality of push and pop is now based on the methods of the linked list class.

  • 1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show...

    1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show the Linked List after the following commands are executed: Stack myStack = new Stack(); myStack.push(20); myStack.push(40); myStack.pop(); myStack.push(60); myStack.push(80); 2)If the same commands were used but the Stack was implemented with an Array with maximum size of 10, show what the array would look like after all these commands are executed. Assume O(1) implementation of push and pop here as well. 3)Given a Queue...

  • How do you implement a stack using a singly linked list in Java? Can you make...

    How do you implement a stack using a singly linked list in Java? Can you make it a simple implementation with just push and pop methods.

  • » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...

    Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...

  • // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. //...

    // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...

  • Java's LinkedList class represents a doubly-linked list. What is the Big-O behavior of its addFirstmethod for...

    Java's LinkedList class represents a doubly-linked list. What is the Big-O behavior of its addFirstmethod for a list of size N? Group of answer choices O(1) O(log N) O(N) O(N log N) Flag this Question Question 21 pts Java's ArrayList class represents a basic array. As a convenience for the user, when the capacity of the backing array is exceeded, the class handles creating a new larger array and copying over the existing items. Its add(int index, E element) method...

  • Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant...

    Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant of bubble sort. The rather severe constraints imposed by the singly-linked list organization presents special problems for implementing Shellsort. Your task is to overcome these constraints and develop a simple, elegant implementation that does not sacrifice efficiency or waste space. Step 1. First, implement a routine to build a list: read integers from standard input, and build a list node for each item consisting...

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT