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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I want convert the code from  singly-linked list to Circular-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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education