SHARE
TWEET

Untitled

a guest Jun 16th, 2019 41 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * CS 200 Colorado State University, Fall 2011
  3.  */
  4.  
  5. public class Member {
  6.  
  7. private String userID;
  8. private String first;
  9. private String last;
  10. private EdgeStack edgeStack;
  11.  
  12. public void sortScore(Member member){
  13.     // calling a helper method
  14.     // this greedy method takes all the creds
  15.     edgeStack = sortEdgeStack(member);
  16. }
  17.  
  18. private EdgeStack sortEdgeStack(Member member)
  19. {
  20.     // our temp stacks
  21.     EdgeStack tempEdgeStack_A = new EdgeStack();
  22.     EdgeStack tempEdgeStack_B = new EdgeStack();
  23.  
  24.     // our return value
  25.     EdgeStack result = null;
  26.     // storing the size of the stack
  27.     int sizeOfStack = member.getEdgeStack().getSize();
  28.     // base case
  29.     if(sizeOfStack<0){
  30.         return null;
  31.     }
  32.     // our true base case
  33.     else if(sizeOfStack==1)
  34.     {
  35.         // init stack
  36.         EdgeStack base = new EdgeStack();
  37.  
  38.         base.push(member.getEdgeStack().pop());
  39.         return base;
  40.     }
  41.     else
  42.     {
  43.  
  44.         // pop and store
  45.         for(int i = 0; i < (sizeOfStack / 2); i++)
  46.         {
  47.             tempEdgeStack_A.push(member.getEdgeStack().pop());
  48.         }
  49.         // pop and store into b
  50.         for(int j = (sizeOfStack/2)+1; j < sizeOfStack; j++)
  51.         {
  52.             tempEdgeStack_B.push(member.getEdgeStack().pop());
  53.         }
  54.  
  55.         tempEdgeStack_A = sortEdgeStack(member);
  56.         tempEdgeStack_B = sortEdgeStack(member);
  57.         result = merge(tempEdgeStack_A,tempEdgeStack_B);
  58.         return result;
  59.     }
  60. }
  61.  
  62.  
  63. private EdgeStack merge(EdgeStack tempEdgeStack_A, EdgeStack tempEdgeStack_B) {
  64.  
  65.     EdgeStack result = new EdgeStack();
  66.  
  67.     // while either or
  68.     while(tempEdgeStack_A.getSize()> 0 || tempEdgeStack_B.getSize() > 0)
  69.     {
  70.         // if both are bigger then 0
  71.         if(tempEdgeStack_A.getSize()> 0 && tempEdgeStack_B.getSize() > 0)
  72.         {
  73.             if(tempEdgeStack_A.peek().getEdgeRank()<=tempEdgeStack_B.peek().getEdgeRank())
  74.             {
  75.                 // adds b to result
  76.                 result.push(tempEdgeStack_A.pop());
  77.             }
  78.             else
  79.             {
  80.                 result.push(tempEdgeStack_B.pop());
  81.             }
  82.         }
  83.         // these elses cover if A or B are > 0 but A or B is also less then or equal too 0;
  84.         else if(tempEdgeStack_A.getSize()> 0)
  85.         {
  86.             while(tempEdgeStack_A.iterator().hasNext())
  87.             {
  88.                 result.push(tempEdgeStack_A.iterator().next());
  89.             }
  90.         }
  91.         else if(tempEdgeStack_B.getSize()> 0)
  92.         {
  93.             while(tempEdgeStack_B.iterator().hasNext())
  94.             {
  95.                 result.push(tempEdgeStack_B.iterator().next());
  96.             }
  97.         }
  98.     }
  99.  
  100.     return result;
  101. }
  102.  }
  103.      
  104. import java.util.LinkedList;
  105. import java.util.ListIterator;
  106.  
  107. /**
  108.  * CS 200 Colorado State University, Fall 2011
  109.  */
  110.  
  111. public class EdgeStack {
  112.  
  113.  
  114. private LinkedList<Edge> llist=new LinkedList<Edge>();
  115.  
  116. public EdgeStack(){
  117.     //add your code
  118. }
  119.  
  120. public boolean isEmpty(){
  121.     return llist.isEmpty();
  122. }
  123.  
  124. public void push(Edge e){
  125.     llist.add(e);
  126. }
  127.  
  128. public Edge getIndexAt(int n){
  129.     return llist.get(n);
  130. }
  131.  
  132. public Edge pop(){
  133.     return llist.remove();
  134. }
  135.  
  136. public Edge peek(){
  137.  
  138.     return llist.getLast();
  139. }
  140.  
  141. public int getSize(){
  142.     return llist.size();
  143. }
  144.  
  145. //  public Edge peek(int n){
  146. //      LinkedList<Edge> temp=llist;
  147. //      return temp.peek();
  148. //  }
  149.  
  150.  
  151. public LinkedList<Edge> popAll(){
  152.     LinkedList<Edge> temp=llist;
  153.     llist=null;
  154.     return temp;    }
  155.  
  156. public ListIterator<Edge> iterator()
  157. {
  158.     return llist.listIterator();
  159. }
  160.  
  161. }
  162.      
  163. else if(tempEdgeStack_A.getSize()> 0)
  164.     {
  165.         while(tempEdgeStack_A.iterator().hasNext())
  166.         {
  167.             result.push(tempEdgeStack_A.iterator().next());
  168.         }
  169.     }
  170.     else if(tempEdgeStack_B.getSize()> 0)
  171.     {
  172.         while(tempEdgeStack_B.iterator().hasNext())
  173.         {
  174.             result.push(tempEdgeStack_B.iterator().next());
  175.         }
  176.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top