Question

JAVA: BASIC LINKED LISTS : Could you, please, provide SIMPLE code snippets to represent these operations?...

JAVA: BASIC LINKED LISTS : Could you, please, provide SIMPLE code snippets to represent these operations?

1. Removing a data element at an index "i"

2. Removing a given data element from the end of a linked list

3. Get/find the data element at a given index

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

If you have any doubts please comment

import java.io.*;
import java.util.*;
public class LinkedList {
  
Node head;
static class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}

//method to insert values into linkedlist
public static LinkedList insert(LinkedList list, int data)
{
Node new_node = new Node(data);
new_node.next = null;
if (list.head == null) {
list.head = new_node;
}
else {
Node last = list.head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}
return list;
}

//method to print the linkedlist
public static void printList(LinkedList list)
{
Node currNode = list.head;
while (currNode != null) {
System.out.print(currNode.data + " ");
currNode = currNode.next;
}
}

//method to delete the element at given index
public static LinkedList deleteAtPosition(LinkedList list, int index)
{
Node currNode = list.head, prev = null;
if (index == 0 && currNode != null) {
list.head = currNode.next;
System.out.println(index + " position element deleted");
return list;
}
int counter = 0;
while (currNode != null) {
  
if (counter == index) {
prev.next = currNode.next;
System.out.println(index + " position element deleted");
break;
}
else {
prev = currNode;
currNode = currNode.next;
counter++;
}
}
if (currNode == null) {
System.out.println(index + " position element not found");
}
return list;
}

//method to delete element at end of the linkedlist
   public static LinkedList deleteAtEnd(LinkedList list)
{
Node currNode = list.head, prev = null;
       Node temp = currNode;
if (currNode == null) {
System.out.println("List is empty");
return null;
}
       if(currNode.next == null){
           currNode = null;
           return null;
       }
       else{
           while (temp.next != null) {
                   temp = temp.next;
           }
           temp = null;
       }
       return list;
}

//method to find the element at given index
   public static LinkedList findElement(LinkedList list,int index)
{
Node currNode = list.head, prev = null;
if (index == 0 && currNode != null) {
System.out.println("Data element at the given index is:" +currNode.data);
}
int counter = 0;
while (currNode != null) {
  
if (counter == index) {
System.out.println("Data element at the given index is:"+currNode.data);
break;
}
else {
prev = currNode;
currNode = currNode.next;
counter++;
}
}
if (currNode == null) {
System.out.println(index + " position element not found");
}
return list;
}

//Driver code
public static void main(String[] args)
{
LinkedList list = new LinkedList();
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);
       int i,f;
       System.out.println("The Linked List is:");
       printList(list);
       System.out.println();
       System.out.println("Enter an index:");
       Scanner s= new Scanner(System.in);
       i=s.nextInt();
       deleteAtPosition(list,i);
       System.out.println("LinkedList after removing data element at given index is:");
       System.out.println();
       printList(list);
       deleteAtEnd(list);
       System.out.println();
       System.out.println("The last Element Removed");
       System.out.println();
       System.out.println("Enter an index to find the element:");
       f= s.nextInt();
       findElement(list,f);
}
}

Output:-

Add a comment
Know the answer?
Add Answer to:
JAVA: BASIC LINKED LISTS : Could you, please, provide SIMPLE code snippets to represent these operations?...
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
  • C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists...

    C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists of any primitive type you want. (Array of size 2020) (This could be based on MSDN libraries or the lab) – you do not need to instantiate any of the linked lists to contain any actual values. Paste your code for that here (this should only be one line) Based on your code or the lab from 4 or your doubly linked list from...

  • Generic Linked Lists ( Program should be written in Java language). Modify the linked list class...

    Generic Linked Lists ( Program should be written in Java language). Modify the linked list class presented in this chapter so it works with generic types. Add the following methods drawn from the java.util.List interface: -void clear(): remove all elements from the list. -E get(int index): return the element at position index in the list. -E set(int index, E element): replace the element at the specified position with the specified element and return the previous element. Test your generic linked...

  • 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...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list...

    Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list L1 with 4 nodes. You can use insert operation to add nodes to the list. Each element represent an airport code (e.g. BOS, ATL, JFK, MSP, etc.). Display the list L1 after it is created. B. Given singly linked list L1, create another singly linked list L2 that contains the same elements but in the reverse order. Display the content of both L1 and...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • I need a python 3 help. Please help me with this question Part 2. Linked Lists...

    I need a python 3 help. Please help me with this question Part 2. Linked Lists You start working with the class LinkNode that represents a single node of a linked list. It has two instance attributes: value and next. class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Before you start with the coding questions, answer the following questions about the constructor Valid Constructor or Not? LinkNode(1, 3) LinkNode(1, None) LinkNode(1,...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

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