Question

Implement the following operation for doubly Linked List: public void insertAt(int position, E data){ } public...

Implement the following operation for doubly Linked List:

public void insertAt(int position, E data){

}

public void insertEnd(E data){

}

public void deleteAt(int position) {

}

public void deleteEnd(){

}

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

public void insertAt(int position, E data) {

Node newNode = new Node();

newNode.data = data;

Node ptr = head;

position = position - 1;

for (int i = 1; ; i++) {

if (ptr == null) {

break;

}

if (i == position) {

newNode.next = ptr.next;

ptr.next.prev = newNode;

ptr.next = newNode;

newNode.prev = ptr;

break;

}

ptr = ptr.next;

}

}

public void insertEnd(E data) {

Node new_Node = new Node();

new_Node.data = data;

new_Node.next = head;

new_Node.prev = null;

if (head != null)

head.prev = new_Node;

head = new_Node;

}

private Node deleteNode(Node head, Node del)

{

if (head == null || del == null)

return null;

if (head == del)

head = del.next;

if (del.next != null)

del.next.prev = del.prev;

if (del.prev != null)

del.prev.next = del.next;

del = null;

return head;

}

public void deleteAt(int position)

{

if (head == null || n <= 0)

return;

Node current = head;

int i;

for (i = 1; current != null && i < position; i++)

{

current = current.next;

}

if (current == null)

return;

deleteNode(head, current);

}

NOTE: As per HOMEWORKLIB POLICY I am allowed to answer specific number of questions (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
Implement the following operation for doubly Linked List: public void insertAt(int position, E data){ } public...
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
  • In C++ Create a data structure doubly linked list, implement the following operations for the doubly...

    In C++ Create a data structure doubly linked list, implement the following operations for the doubly linked list: addFirst addLast insertBefore insertAfter delete printList

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • Public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=nul...

    public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=null; if(list.head==null){ list.head=node; } else{ Node n = list.head; Node temp = list.head; while(n.next!=null){ n=n.next; n.prev=temp; temp=temp.next; } n.next=node; node.prev=n; System.out.println(node.prev + " " + n.data); } } How can i turn this doubly linked list into a circular doubly linkedlist, using java

  • This class implements a doubly linked list in which the nodes are of the class DLNode....

    This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...

  • public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=null; if(list.head==null){ list.head=node; }...

    public void add(linked list, int data){ Node node = new Node(data); node.next=null; node.prev=null; if(list.head==null){ list.head=node; } else{ Node n = list.head; Node temp = list.head; while(n.next!=null){ n=n.next; n.prev=temp; temp=temp.next; } n.next=node; node.prev=n; System.out.println(node.prev + " " + n.data); } } How can i turn this doubly linked list into a circular doubly linkedlist, using java

  • // C++ Doubly linked list class clinknode{ friend class DList; public:     clinknode(){next = prev=0;data =0;}...

    // C++ Doubly linked list class clinknode{ friend class DList; public:     clinknode(){next = prev=0;data =0;} protected:     clinknode *next;     clinknode *prev;     int data; } class DList{ public: DList(){first = last = 0;} void insertFront(int key); bool found(int key){ return search(key)!=0;} void deleteAll(int key); // other list functions... private: clinknode *search(int); clinknode *first; clinknode *last; } (20 points) Write the code for the deleteAll (int key) member function. It takes a key value and deletes all of the...

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • In python - Implement a doubly linked circular linked list of Node objects called CircularDoublyLinkedList. The...

    In python - Implement a doubly linked circular linked list of Node objects called CircularDoublyLinkedList. The data of each Node in the list is an integer. You will collect measurements for: An already sorted linked list An already sorted linked list in descending order A linked list containing random data

  • 3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubl...

    3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubly linked list rather than a singly linked list or not?(3 marks) 3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubly linked list rather than a singly linked list or not?(3 marks)

  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

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