Question

Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method

BELOW IS THE CODE I ALREADY HAVE

import java.io.*; 1/ Java program to implement // a Singly Linked List public class LinkedList { Node head; // head of list /

// Create a new node with given data Node new_node = new Node (data); new_node. next = null; // If the Linked List is empty,

Node currNode = list.head; System.out.print(LinkedList: ); // Traverse through the LinkedList while (currNode != null) { //

list = insert(list, 2); list = insert(list, 3); list = insert(list, 4); list = insert(list, 5); list = insert(list, 6); list

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

package DS;

public class MyLinkedList

{

// Head of list

Node head;

// Linked list node

// This inner class is made static

// so that main can access it

static class Node

{

int data;

Node next;

// Constructor

Node(int d)

{

data = d;

next = null;

}

}

// Method to insert a new node

public static MyLinkedList insert(MyLinkedList list, int data)

{

// Create a new node with given data

Node new_node = new Node(data);

new_node.next = null;

// If the Linked list is empty,

// then make the new node as head

if(list.head == null)

list.head = new_node;

else

{

// Else traverse till the last node

// and insert the new_node there

Node last = list.head;

while(last.next != null)

last = last.next;

last.next = new_node;

}

// Returns the list by head

return list;

}

// Method to print linked list

public static void printList(MyLinkedList list)

{

Node currNode = list.head;

System.out.print("\nLinkedList: ");

// Traverse through the LinkedList

while(currNode != null)

{

// Print the data at current node

System.out.print(currNode.data + " ");

// Go to next node

currNode = currNode.next;

}

}

public static MyLinkedList delete(MyLinkedList list, int data)

{

// Store head node

Node currNode = list.head;

Node prevNode = list.head;

  

// If head node itself holds the key to be deleted

if (currNode != null && currNode.data == data)

{

list.head = currNode.next; // Changed head

System.out.println("\nDeleted node " + data + " in the LinkedList.");

return list;

}

  

// Search for the key to be deleted, keep track of the

// previous node as we need to change currNode.next

while (currNode != null && currNode.data != data)

{

prevNode = currNode;

currNode = currNode.next;

}

  

// If key was not present in linked list

if (currNode == null)

{

System.out.println("\n" + data + " not available in the LinkedList.");

return list;

}

  

// Unlink the node from linked list

prevNode.next = currNode.next;

System.out.println("\nDeleted node " + data + " in the LinkedList.");

return list;

}

// Driver code

public static void main(String ss[])

{

// Starts with the empty list

MyLinkedList list = new MyLinkedList();

/************* Insertion ************/

// insert the values

list = insert(list, 1);

list = insert(list, 2);

list = insert(list, 3);

list = insert(list, 4);

list = insert(list, 5);

list = insert(list, 6);

list = insert(list, 7);

list = insert(list, 8);

// Print the LinkedList

printList(list);

// Call the method to delete data 2

list = delete(list, 2);

printList(list);

// Call the method to delete data 12

list = delete(list, 12);

printList(list);

}// End of main method

}// End of class

Sample Output:


LinkedList: 1 2 3 4 5 6 7 8
Deleted node 2 in the LinkedList.

LinkedList: 1 3 4 5 6 7 8
12 not available in the LinkedList.

LinkedList: 1 3 4 5 6 7 8

Add a comment
Know the answer?
Add Answer to:
BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...
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
  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • Inventory (linked lists: insert at the front of a list)

    import java.util.Scanner;public class Inventory {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);             InventoryNode headNode;                                                    InventoryNode currNode;      InventoryNode lastNode;      String item;      int numberOfItems;      int i;      // Front of nodes list           ...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • n JAVA, students will create a linked list structure that will be used to support a...

    n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • Soukaina Filali Boubrahimi CSc 2720 Data Structures: Lab 8 Due Date: Sunday 31t Mar, 201911:59pm ...

    Soukaina Filali Boubrahimi CSc 2720 Data Structures: Lab 8 Due Date: Sunday 31t Mar, 201911:59pm We have seen in class how selection sort algorithm works on arrays data structure. In this lab we will practice how selection sort can be performed on a linked list ADT 1. Convert the following selection sort pseudo-code to perform the sort in ascending order (selectionsort asc function) a. Find the node with the minimum value in the linked list of length rn b. Append...

  • this is i have code for double linked list with insert at sorted list. i have...

    this is i have code for double linked list with insert at sorted list. i have some error that stdout: ------- Error: compilation stderr: ------- InsertDouble.c: In function ‘list* InsertDouble(LIST, int)’: InsertDouble.c:51:14: error: cannot convert ‘list’ to ‘list*’ in assignment Currentptr = *head; // set a pointer which is current one ^ it keep give me this error i am not sure how to fix is anyone possible to help me? #include <stdio.h> #include <stdlib.h> typedef struct list {   ...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

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
ADVERTISEMENT