Question

Soukaina Filali Boubrahimi CSc 2720 Data Structures: Lab 8 Due Date: Sunday 31t Mar, 201911:59pm We have seen in class how se
printlist (head); head selectionSort_as(head); System.out println(n List After selectionSort asc printlist(head): // Expected
Soukaina Filali Boubrahimi CSc 2720 Data Structures: Lab 8 Due Date: Sunday 31t Mar, 201911:59pm We have seen in class how selection sort algorithm works on arrays data structure. In this lab we will practice how selection sort can be performed on a linked list ADT 1. Convert the following selection sort pseudo-code to perform the sort in ascending order (selectionsort asc function) a. Find the node with the minimum value in the linked list of length rn b. Append the min node in a new result linked list c. Delete min from original linked list d. Repeat steps a-c until the original linked list is empty e. Return the result linked list 2. Convert the following selection-sort pseudo-code to perform the sort in descending order (selectionSort_desc function) a. Find the node with the maximum value in the linked list of length n b. Append the max node in a new result linked list c. Delete max from original linked list d. Repeat steps a-c until the original linked ist is empty e. Return the result linked list Note: You might assume that all characters are lower-case. For example: Input: str "ilovedata Output: ->a -a -d-e 1 ->o ->t- (using selectionSort asc) .Node Class Template public class Node( char item; Node next; Node(char c) iten c next null; public class InsertSort ( public static void nain(String[] args) Node head .訒t tial izel ist("ílovedata"); System.out.printin("n List Before selectionSort asc)
printlist (head); head selectionSort_as(head); System.out println(n List After selectionSort asc printlist(head): // Expected answer head initializelist( ilovedata") System.out println(In List Before selectionSort desc) printlist(head); head selectionSort desc(head); System.out.printin( n List After selectionSort desc") printiist(head) // Expected answer: public static Node selectionSort asc(Node head)( Node result null; T INSERT CODE HERE return result; public static Node selectionSort desC (Node head)f Node result null; // İNSERT CODE HERE return result; /I Method that takes a string and insert its characters into a linked list public static Node initializelist(String str) Node heade new Node(str.charat (e))cur int i for(cure head, ǐ"1 ; icstr.length();ǐ++,curecur.next)( cur.next-new Node(str.charAt ()) return head; /I Method for printing 1inked list public static void printList (Node head)( Node cur head; İf(heade-null) Systen.out,print("
Comments
    0 0
    Add a comment Improve this question Transcribed image text
    Answer #1

    //I have made utility functions getMin(),getMax(),deleteNode()
    class Node{
       char item;
       Node next;
      
       Node(char c){
           item=c;
           next=null;
       }
    }
    class InsertSort{
      
       public static void main(String[] args){
          
           Node head=initializeList("ilovedata");
           System.out.println("\n List Before selectionSort_acs");
           printList(head);
           head=selectionSort_asc(head);
           System.out.println("\n List After selectionSort_asc");
           printList(head);
           // Expected answer: -> a -> a -> d -> e -> i -> l -> o -> t -> v
          
          
           head=initializeList("ilovedata");
           System.out.println("\n List Before selectionSort_decs");
           printList(head);
           head=selectionSort_desc(head);
           System.out.println("\n List After selectionSort_desc");
           printList(head);
           // Excepted answer: -> v -> t -> o -> l -> i -> e -> d -> a -> a
          
       }
       public static Node selectionSort_asc(Node head){
           Node result=null,min,cur=null;
          
           //INSERTED CODE HERE
          
           //firstly initialize result
           if(head!=null){
           min=getMin(head);
           result=min;
           head=deleteNode(head,min);
           }
           //then insert the nodes one by one
           for(cur=result;head!=null;cur=cur.next){
               min=getMin(head);
               cur.next=min;
               head=deleteNode(head,min);
           }
          
           if(cur!=null)
           cur.next=null;
          
           return result;
       }
       public static Node selectionSort_desc(Node head){
           Node result=null,max,cur=null;
          
           //INSERTED CODE HERE
           //firstly initialize result
           if(head!=null){
           max=getMax(head);
           result=max;
           head=deleteNode(head,max);
           }
           //then insert the nodes one by one
           for(cur=result;head!=null;cur=cur.next){
               max=getMax(head);
               cur.next=max;
               head=deleteNode(head,max);
           }
          
           if(cur!=null)
           cur.next=null;
          
           return result;
       }
      
      
      
       //Method to find node having minimum value of item
      
       public static Node getMin(Node head){
       //find the node with minimum value of item and return it
           Node min=head;
           for(Node cur=head;cur!=null;cur=cur.next){
               if(min.item>cur.item){
                   min=cur;
               }
           }
           return min;
       }
      
       //Method to find node having maximum value of item
      
       public static Node getMax(Node head){
       //find the node with maximum value of item and return it
           Node max=head;
           for(Node cur=head;cur!=null;cur=cur.next){
               if(max.item<cur.item){
                   max=cur;
               }
           }
           return max;
       }
      
       //Method to delete the node
      
       public static Node deleteNode(Node head,Node x){
       //if x is null return head without deleting
       if(x==null){
       return head;
       }
      
       //if the first node is to be deleted then return head.next first node will be automatically deleted
           if(x==head){
           return head.next;
           }
          
           //otherwise traverse get the node to be deleted remove it from between and come out of the loop
           for(Node cur=head;cur.next!=null;cur=cur.next){
           if(cur.next==x){
           cur.next=cur.next.next; //cur.next will have value of cur.next.next cur.next will not be accessable then
           break;
           }
           }
           return head;
       }
      
       // Mehthod that takes a string and insert its characters into a linked list
      
       public static Node initializeList(String str){
           Node head=new Node(str.charAt(0)),cur;
           int i;
          
           for(cur=head,i=1;i<str.length();i++,cur=cur.next){
               cur.next=new Node(str.charAt(i));
           }
           return head;
       }
      
       //Method for printing linked list
       public static void printList(Node head){
           Node cur=head;
           if(head==null) System.out.print("<Empty>");
           for(;cur!=null;cur=cur.next){
               System.out.print("-> "+cur.item+" ");
           }
       }
    }

    Add a comment
    Know the answer?
    Add Answer to:
    Soukaina Filali Boubrahimi CSc 2720 Data Structures: Lab 8 Due Date: Sunday 31t Mar, 201911:59pm ...
    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
    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
    ADVERTISEMENT