Question

Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying...

Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying structure. Include the following methods:

1. insert an element within the linked list.(this should also work for the front and the rear of the list)

2. Remove an element from the linked list

3. Display (print) the elements of the linked list in order.

4. A method to check if the list is "empty".

Test your solution using a linked list that initially has the characters A, B, D, E, F, and G. Insert "C" between B and D. Remove element "F".

[Hint: One solution (recommended) is to use 2 arrays. One for the data, and the other for the "next" pointer. Also, consider using dummy nodes for the front, and possibly the rear of the list]

Extend the solution so that it simulates a doubly linked list! (hint: You will need a third array to represent the "backward" pointers)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi Student,s

Please find below the solution of your query i.e. SIngly linkedlist implementation using Java -

1. FIrat create a Node class
class Node {
public int data;
public Node next;

public void displayNodeData() {
System.out.println("{ " + data + " } ");
}
}

2. Now Write the program to create a Singly linked list with all the operations you mentioned
package org.arpit.java2blog;

class Node {
   public int data;
   public Node next;

   public void displayNodeData() {
       System.out.println("{ " + data + " } ");
   }
}

public class SinglyLinkedList {
   private Node head;

   public boolean isEmpty() {
       return (head == null);
   }

   // used to insert a node at the start of linked list
   public void insertFirst(int data) {
       Node newNode = new Node();
       newNode.data = data;
       newNode.next = head;
       head = newNode;
   }

   // used to delete node from start of linked list
   public Node deleteFirst() {
       Node temp = head;
       head = head.next;
       return temp;
   }

   // Use to delete node after particular node
   public void deleteAfter(Node after) {
       Node temp = head;
       while (temp.next != null && temp.data != after.data) {
           temp = temp.next;
       }
       if (temp.next != null)
           temp.next = temp.next.next;
   }

   // used to insert a node at the start of linked list
   public void insertLast(int data) {
       Node current = head;
       while (current.next != null) {
           current = current.next; // we'll loop until current.next is null
       }
       Node newNode = new Node();
       newNode.data = data;
       current.next = newNode;
   }

   // For printing Linked List
   public void printLinkedList() {
       System.out.println("Printing LinkedList (head --> last) ");
       Node current = head;
       while (current != null) {
           current.displayNodeData();
           current = current.next;
       }
       System.out.println();
   }
}

3. Now let us create a driver class-

23
24

package org.arpit.java2blog;

public class LinkedListMain {

   public static void main(String args[])
   {
       SinglyLinkedList myLinkedlist = new SinglyLinkedList();
       myLinkedlist.insertFirst(5);
       myLinkedlist.insertFirst(6);
       myLinkedlist.insertFirst(7);
       myLinkedlist.insertFirst(1);
       myLinkedlist.insertLast(2);
       // Linked list will be
       // 2 -> 1 -> 7 -> 6 -> 5
       Node node=new Node();
       node.data=1;
       myLinkedlist.deleteAfter(node);
       // After deleting node after 1,Linked list will be
       // 2 -> 1 -> 6 -> 5
       myLinkedlist.printLinkedList();
   }
}
4 Output-
Printing LinkedList (head --> last)
{ 1 }
{ 6 }
{ 5 }
{ 2 }

5. Similarly you can write all your methods. If you face any concern write the same in comments.

Please hit the like button if you liked my answer.
Thanks in advance.

Regards,
Ankit Tandon

Add a comment
Know the answer?
Add Answer to:
Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying...
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
  • ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in...

    ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in reverse order when you are NOT allowed to allocate memory dynamically. What is the running time of the algorithm? b) Write pseudo code to output a singly-linked list in reverse order when you are ALLOWED to allocate memory dynamically. What is the running time of the algorithm? c) You have an increasingly-sorted circular list (using an array) of n elements that is full. The...

  • Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using...

    Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using only a constant amount of additional storage and no recursion. Assume the list object has a head pointer _head and consists of Node objects; a Node object has a pointer Node* _next

  • Write a C function that iterates through a singly linked list and converts it to a...

    Write a C function that iterates through a singly linked list and converts it to a doubly linked list. Your solution must NOT create a second list. You must come up with the appropriate parameters and return type. Note: the nodes in the singly linked list have a field for a previous pointer, but that field has not initially been set in any of the nodes. You must do this as part of your solution!

  • how to implement a linked list object using only singly linked list toolkit. Then implement a...

    how to implement a linked list object using only singly linked list toolkit. Then implement a FREQUENCY function to count the ovcurrence of each element in the list. task#1: Add = operator to node: implement the assignment operator for the node such that setting a node = overwrites the value in the node with the value. task#2:Linked List class implement the methods of linked list. insert search and locate remove node* operator [] task#3: Implement the Frequency

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • Answer all questions 1- in circular singly linked list, previous pointer of the first node points...

    Answer all questions 1- in circular singly linked list, previous pointer of the first node points to which node A. First node B. Itself C. null D. Last node 2- Which of the following is NOT an applications of linked lists? A. Implementation of stacks and queues B. Dynamic memory allocation C. Manipulation of polynomials D. Keeping people at home during epidemics like corona virus 3- In a circular singly linked list? A. Components are all linked together in some...

  • Draw a sketch of a singly linked list containing the following int values: 3, 1, 4,...

    Draw a sketch of a singly linked list containing the following int values: 3, 1, 4, 1, 5. The 3 should be at the front of the list. Remember to sketch the head pointer, each node, and each node's next pointer. 2. Write the code for the following new member function for our SinglyLinkedList class. You should write out the definition of the function, but do not need to write out all of the rest of the class. Your code...

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