I want convert the code from  singly-linked list to doubly-linked list

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 9PE
icon
Related questions
Question

I want convert the code from  singly-linked list to doubly-linked list 

 

 

 

package Problem;

class Node{

       private Object value;     // Holds the data of the node

       private Node next;        // Holds the follwoing node's address

 

       Node(Object v, Node n){

             this.value =v;

             this.next = n;

       }

 

       public Node getNext(){

             return this.next;

       }

 

       public Object getValue(){

             return this.value;

       }

 

       public void setNext(Node n){

             this.next=n;

       }

 

       public void setValue(Object v)

       {

             this.value=v;

       }

}

 

class SLL{

       public Node head;

       SLL(){

             this.head=null;

       }

 

       public  void display(){

             Node curr = this.head;

             while(curr != null)

             {

                    System.out.print(curr.getValue());

                    if(curr.getNext()!=null)

                           System.out.print("-->");

                    curr=curr.getNext();

             }

             System.out.println();

       }

 

       public void insertAtFront(Object v){

             Node new_node = new Node(v, this.head);

             this.head = new_node;

       }

 

       public void insertAtEnd(Object v){

             Node curr = this.head;

             while(curr .getNext()!= null)

                    curr = curr.getNext();

 

             Node new_node = new Node(v, null);

             curr.setNext(new_node);

       }

 

       public void insertAfter(Object v, Object key){

             Node curr = this.head;

             while(curr.getValue() != key)

                    curr=curr.getNext();

 

             Node new_node = new Node(v, curr.getNext());

             curr.setNext(new_node);

 

       }

 

       public void Delete(Object v)

       {

             if(v == this.head.getValue())

                    this.head = head.getNext();

             else

             {

                    Node curr = this.head;

                    Node previous = this .head;

                    while(curr.getValue()!=v)

                    {

                           previous = curr;

                           curr = curr.getNext();

                    }

                    previous.setNext(curr.getNext());

             }

       }

 

       public Node Search(Object v){

             Node curr = this.head;

             while(curr != null)

             {

                    if(curr.getValue() == v)

                           return curr;

                    curr = curr.getNext();

             }

             return null;

       }

 

       //The following function gets the last node in the list

       public Node Last(){

             Node curr = this.head;

             while(curr.getNext()!= null)

                    curr = curr.getNext();

             return curr;

       }

 

       // Count the elements of the lsit

       public int size(){

             Node curr = this.head;

             int c=0;

             while(curr != null)

             {

                    curr = curr.getNext();

                    c++;

             }

             return c;

       }

 

}

public class f {

 

       public static void main(String[] args) {

 

             SLL ls = new SLL();

             ls.insertAtFront(1);

             ls.insertAtFront(21);

             ls.insertAtFront(22);

             ls.insertAtFront(3);

             ls.display();

 

             //ls.insertAtEnd(33);

             //ls.insertAtFront(33);

 

             //ls.insertAfter(33, 22);

             //ls.Delete(22);

             //Node g = ls.Search(22);

             //System.out.println(g.getNext().getValue());

 

             System.out.println(ls.size());

             System.out.println(ls.Last().getValue());

             //ls.display();

}

}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning