Question

In: Computer Science

can u give me an example of how to removing Double Linked List a node at...

can u give me an example of how to removing Double Linked List a node at a given location in java

the method should be like this:

public void removeFromLocation(int location) {

}
      
      
      
      
   }

Solutions

Expert Solution

Source Program:

public void removeFromLocation(int location)
{
Node temp=head;
int i=0,count=0;
while(temp.next!=null)
{
count++;
temp=temp.next;
}
System.out.println("number of nodes:"+(count+1));
temp=head;
if(location <=(count+1))
{
if(location==1)
{
temp=temp.next;
head=temp;
}
else if(location==(count+1))
{
while(temp.next!=null)
{
temp=temp.next;
}
temp=temp.previous;
temp.next=null;
  
}
else
{
i=1;
while(i<location)
{
temp=temp.next;
i++;
}
temp=temp.previous;
temp.next=temp.next.next;
  
}
}

sample output:

Complete Program:

public class DoublyLinkedList {
class Node
{
int data;
Node previous;
Node next;
public Node(int data)
{
this.data = data;
}
}
Node head, tail = null;   
public void addNode(int data)
{
Node newNode = new Node(data);
if(head == null)
{
head = tail = newNode;
head.previous = null;
tail.next = null;
}
else
{
tail.next = newNode;
newNode.previous = tail;
tail = newNode;
tail.next = null;
}
}
public void removeFromLocation(int location)
{
Node temp=head;
int i=0,count=0;
while(temp.next!=null)
{
count++;
temp=temp.next;
}
System.out.println("number of nodes:"+(count+1));
temp=head;
if(location <=(count+1))
{
if(location==1)
{
temp=temp.next;
head=temp;
}
else if(location==(count+1))
{
while(temp.next!=null)
{
temp=temp.next;
}
temp=temp.previous;
temp.next=null;
  
}
else
{
i=1;
while(i<location)
{
temp=temp.next;
i++;
}
temp=temp.previous;
temp.next=temp.next.next;
  
}
}
}
public void display()
{
Node current = head;
if(head == null)
{
System.out.println("List is empty");
return;
}
System.out.println("Nodes of doubly linked list: ");
while(current != null)
{
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args)
{
DoublyLinkedList dList = new DoublyLinkedList();   
dList.addNode(1);
dList.addNode(2);
dList.addNode(3);
dList.addNode(4);
dList.addNode(5);   
dList.display();
dList.removeFromLocation(3);
dList.display();
}
}


Related Solutions

can someone explain to me circular, double, linked list? can someone show a program on how...
can someone explain to me circular, double, linked list? can someone show a program on how you would go about using recursive with proper functions and one not using recursive but somethibg simikar! bibary search tree, preorder, post order? good explanation of linked list sorted.
In a double linked chain, each node can point to the previous node as well as...
In a double linked chain, each node can point to the previous node as well as the next node. Illustrate and list the steps necessary to add a node to the end of the doubly linked chain.
In a double linked chain, each node can point to the previous node as well as...
In a double linked chain, each node can point to the previous node as well as the next node. In a double linked chain, each node can point to the previous node as well as the next node.
c++ example of a double linked list of chars I need to create a double linked...
c++ example of a double linked list of chars I need to create a double linked list of chars. this should be a class that allows you to input a single character at a time, list the resulting characters, find any character and delete the first example of a character.
1a) Write the start of the class declaration for a node in a linked list (give...
1a) Write the start of the class declaration for a node in a linked list (give the name of the class and the instance variables). The name of the node should be SpecialNode. The data in each SpecialNode will include both a String and a Song object. b)Using the SpecialNode class you created in question above, write a constructor forSpecialNode that has three parameters and initializes all the SpecialNode instance variables. c) Write a line of code to instantiate a...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
Consider a linked list whose nodes are objects of the class Node: class Node {    ...
Consider a linked list whose nodes are objects of the class Node: class Node {     public int data;     public Node next; } prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list. Which of the following statements is used to insert a new node, referenced by newNodebetween prev and curr? Group of answer choices newNode.next = curr; prev.next = newNode; newNode.next = head; head = newNode;...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
Can someone give me an example an non example of coefficient?
Can someone give me an example an non example of coefficient?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT