Question

In: Computer Science

method to remove all elements frrom my linked list (java)

method to remove all elements frrom my linked list (java)

Solutions

Expert Solution

Node class

package com;

public class Node {

Node next;

int data;

Node(int data){

this.data=data;

}

}

LinkedList class having all implementation

package com;

public class LinkedListWithBasicOp {

Node head; //head

public void addAtHead(int value){

Node temp=head;

if(head==null){ //if head is null make new new node

head=new Node(value);

}

else{

Node n=new Node(value); //add new node to first

head=n;

n.next=temp;

}

}

public void deleteAtIndex(int index){

Node temp=head;

Node prev=head;

int i=0;

if(index==0){ //if index is 0 make delete node at first

temp=head.next;

head=temp;

temp=null;

}

else{

while(temp!=null){ //Traverse through list

if(i==index){ //if i==index

prev.next=temp.next; //delete node at position i

}

i++;

prev=temp;

temp=temp.next;

}

}

}

public void removeAllNode(){

while(head!=null){

deleteAtIndex(0);

}

}

public void print(){ //print linked list

Node t=head;

if(t==null){

System.out.println("List is empty");

return;

}

while(t!=null){

System.out.print(t.data+"->");

t=t.next;

}

System.out.println("Null");

System.out.println(" ");

}

}

//driver program to test

package com;

public class Tester {

public static void main(String[] args) {

LinkedListWithBasicOp m=new LinkedListWithBasicOp();

m.addAtHead(4);

m.addAtHead(5);

m.addAtHead(3);

m.addAtHead(2);

m.print();

m.removeAllNode();

m.print();

}

}

output

2->3->5->4->Null

List is empty


Related Solutions

write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Remove the minimum element from the linked list in Java public class LinkedList {      ...
Remove the minimum element from the linked list in Java public class LinkedList {       // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)   ...
How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
My add method is not working to add elements into an arrayList in java. This is...
My add method is not working to add elements into an arrayList in java. This is the error message I keep getting: Exception in thread "main" java.lang.NullPointerException    at assignment1.ArrayBag.add(ArrayBag.java:50)    at assignment1.Main.main(Main.java:21) BUILD FAILED (total time: 0 seconds) The following are all the methods I've created. /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package...
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
This function will receive a list of elements with duplicate elements, this function should remove the...
This function will receive a list of elements with duplicate elements, this function should remove the duplicate elements in the list and return a list without duplicate elements. The elements in the returned list must be in the same order that they were found in the list the function received. A duplicate element is an element found more than one time in the specified list. JAVA
Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that...
Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that are inserted. Sort based on the speed of the warrior. Driver code: public class LinkedListDriver { public static void main(String[] args) { LinkedList list = new SortedDoublyLinkedList(); System.out.println(list); Warrior krogg = new Warrior("Krogg", 30, 50, 200); list.insert(krogg); System.out.println(list); Warrior gurkh = new Warrior("Gurkh", 40, 45, 180); list.insert(gurkh); System.out.println(list); Warrior brynn = new Warrior("Brynn", 45, 40, 190); list.insert(brynn); System.out.println(list); Warrior dolf = new Warrior("Dolf", 20,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT