Question

In: Computer Science

Suppose you are given two circularly linked lists, L and M. Create an algorithm for telling...

Suppose you are given two circularly linked lists, L and M. Create an algorithm
for telling if L and M store the same sequence of elements (but perhaps with
different starting points). Please provide a main() function with it in Java to test it.

Solutions

Expert Solution

public class Test {
   public static void main(String[] args){
       CircularLinkedList list1 = new CircularLinkedList();
       list1.append(6);
       list1.append(2);
       list1.append(3);
       list1.append(4);
       list1.append(5);
      
       CircularLinkedList list2 = new CircularLinkedList();
       list2.append(2);
       list2.append(3);
       list2.append(4);
       list2.append(5);
       list2.append(6);
      
       list1.displayList();
       list2.displayList();
      
       System.out.println(" check :"+checkSame(list1,list2));
      
       list1.append(3);
       list2.append(10);
       list1.displayList();
       list2.displayList();
       System.out.println(" check :"+checkSame(list1,list2));
      
      
      
   }
  
   public static boolean checkSame(CircularLinkedList l1,CircularLinkedList l2){
       if(l1.getCount() != l2.getCount()) return false;
       else{
           Node l1_start = l1.getStartNode();
           Node l2_start = l2.getStartNode();
           Node n1=l1.getStartNode();
           Node n2=l2.getStartNode();
           while(n2.link != l2_start){
               if(n2.data==n1.data){
                   break;
               }
               n2=n2.link;
           }
           while(n1.link != l1_start){
               if(n1.data != n2.data){
                   return false;
               }
               n1=n1.link;
               n2=n2.link;
           }
           return true;
       }
   }
}
class Node{
    int data;
    public Node link;
    public Node(int data){
        this.data=data;
    }
    @SuppressWarnings("unused")
    public Node(int data,Node link){
        this.data=data;
        this.link=link;
    }
   }
class CircularLinkedList {
    private Node start;
    private int count;
    public void append(int x){
        count++;
        Node temp=new Node(x);
        if(start==null){
            start=temp;
        }else{
            Node tp=start;
            while(tp.link!=start){
                tp=tp.link;
            }
            tp.link=temp;
        }
        temp.link=start;
    }
    public void addBeg(int x){
        count++;
        Node temp=new Node(x);
        if(start==null){
            temp.link=temp;
        }else{
            Node tp=start;
            while(tp.link!=start){
                tp=tp.link;
            }
            tp.link=temp;
            temp.link=start;
        }
        start=temp;
    }
    public void addAt(int pos,int x){
        Node temp,tp;
        temp=new Node(x);
        tp=start;
        for(int i=0;i<pos;i++){
            if(tp.link==start)
                break;
            tp=tp.link;
        }
        temp.link=tp.link;
        tp.link=temp;
        count++;
    }
    public void displayList(){
        if(start==null)
            System.out.println("List is empty..");
        else{
        Node temp=start;
        System.out.print("[ ");
        while(temp.link!=start){
            System.out.print(" "+temp.data+" ");
            temp=temp.link;
        }
        System.out.print(temp.data+" ]");
        System.out.println();
    }
}
    public void deleteAt(int position){
        Node current=start;
        Node previous=start;
        for(int i=0;i<position;i++){
            if(current.link==start)
                break;
            previous=current;
            current=current.link;
        }
        System.out.print(current.data);
        if(position==0)
            deleteFirst();
        else
            previous.link=current.link;
        count--;
    }
    public Node getStartNode(){
       return start;
    }
    public void deleteFirst() {
        Node temp=start;
        while(temp.link!=start){
            temp=temp.link;
        }
        temp.link=start.link;
        start=start.link;
        count--;
    }
    public int getCount(){
        return count;
    }
}


Related Solutions

Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
Topic: Students will be able to create skills in the use of linked lists, the stack,...
Topic: Students will be able to create skills in the use of linked lists, the stack, and the queue abstract data types, by implementing solutions to fundamental data structures and associated problems. Add the code for the methods where it says to implement. The main class is already done. There is a sample of the output. 1. A double-ended queue, or deque, is a data structure consisting of a list of items on which the following operations are defined: addToBack(x):...
(15 pts) Describe an O(n)-time algorithm that partitions a doubly linked list L into two doubly...
(15 pts) Describe an O(n)-time algorithm that partitions a doubly linked list L into two doubly linked lists L1 and L2, where L1 contains the odd-number-th elements in L and L2 contains the even-number-th elements in L. Elements in both L1 and L2 should appear in the same order as they appear in L. For example, if L contains the numbers 4, 7, 9, 1, and -3, in that order, then the output L1 should contain 4, 9, and -3,...
Working with Linked Lists in C++ Tasks As stated in the objectives, you have two methods...
Working with Linked Lists in C++ Tasks As stated in the objectives, you have two methods to implement. These methods (which contain TODO comments) are found in linked_list.cpp. Part 1 Implement the copy constructor; use the LinkedBag as inspiration for your solution as the technique of creating a deep copy is essentially the same. Part 2 Implement the replace method. //linked_list.cpp #include "linked_list.h" // Header file #include <cassert> template<class Object> LinkedList<Object>::LinkedList() : headPtr( nullptr ), itemCount( 0 ) { }...
Write an algorithm to check equality of two link lists.(java)
Write an algorithm to check equality of two link lists.(java)
3- Use FirstLastList: Write method public void join(FirstLastList SecondList) such that given two linked lists, join...
3- Use FirstLastList: Write method public void join(FirstLastList SecondList) such that given two linked lists, join them together to give one. So if the lists lst1= [1,3,7,4] and lst2=[2,4,5,8,6], the result of lst1.join(lst2) is lst1=[1,3,7,4,2,4,5,8,6] and lst2=[]. 4- Use FirstLastList: Write method public void swap(). It swaps the first and last elements of a FirstLastList. So if lst1= [1,3,7,4], lst1.swap() = [4,3,7,1]. Display or throw an exception if the list contains less than two elements. public class FirstLastList { private...
1- Use FirstLastList: Write method public void join(FirstLastList SecondList) such that given two linked lists, join...
1- Use FirstLastList: Write method public void join(FirstLastList SecondList) such that given two linked lists, join them together to give one. So if the lists lst1= [1,3,7,4] and lst2=[2,4,5,8,6], the result of lst1.join(lst2) is lst1=[1,3,7,4,2,4,5,8,6] and lst2=[]. 2- Use FirstLastList: Write method public void swap(). It swaps the first and last elements of a FirstLastList. So if lst1= [1,3,7,4], lst1.swap() = [4,3,7,1]. Display or throw an exception if the list contains less than two elements. Demonstrate by displaying the list...
Triplicates. Given three lists of N names each, devise a linearithmic algorithm to determine if there...
Triplicates. Given three lists of N names each, devise a linearithmic algorithm to determine if there is any name common to all three lists, and if so, return the first such name
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT