idastan97

CSCI152 L8 P1-8

Feb 6th, 2017
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.59 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////////// P1
  2.     public static int evenCount(IntStack stk){
  3.         IntStack temp=new ArrayIntStack();
  4.         int s=stk.getSize(), cnt=0;
  5.         for (int i=0; i<s; i++){
  6.             int x;
  7.             try {
  8.                x=stk.pop();
  9.             } catch (Exception ex){
  10.                 System.out.println(ex.getMessage());
  11.                 x=1;
  12.             }
  13.             if (x%2==0){
  14.                 cnt++;
  15.             }
  16.             temp.push(x);
  17.         }
  18.         for (int i=0; i<s; i++){
  19.             try {
  20.                 stk.push(temp.pop());
  21.             } catch (Exception ex) {
  22.                 System.out.println(ex.getMessage());
  23.             }
  24.         }
  25.         return cnt;
  26.     }
  27.    
  28. ///////////////////////////////////////////////////////////////////////////////////////// P2
  29.     public static IntStack copyStack(IntStack orig) {
  30.         IntStack res=new ArrayIntStack(), temp=new ArrayIntStack();
  31.         int s=orig.getSize();
  32.         for (int i=0; i<s; i++){
  33.             try {
  34.                 temp.push(orig.pop());
  35.             } catch (Exception ex){
  36.                 System.out.println(ex.getMessage());
  37.             }
  38.         }
  39.         for (int i=0; i<s; i++){
  40.             try {
  41.                 int x=temp.pop();
  42.                 res.push(x);
  43.                 orig.push(x);
  44.             } catch (Exception ex){
  45.                 System.out.println(ex.getMessage());
  46.             }
  47.         }
  48.         return res;
  49.     }
  50.    
  51. ///////////////////////////////////////////////////////////////////////////////////////// P3
  52.     public static IntQueue copyQueue(IntQueue orig){
  53.         IntQueue res=new ArrayIntQueue();
  54.         int s=orig.getSize();
  55.         for (int i=0; i<s; i++){
  56.             int x;
  57.             try{
  58.                 x=orig.dequeue();
  59.             } catch (Exception ex){
  60.                 System.out.println(ex.getMessage());
  61.                 x=0;
  62.             }
  63.             orig.enqueue(x);
  64.             res.enqueue(x);
  65.         }
  66.         return res;
  67.     }
  68.  
  69. ///////////////////////////////////////////////////////////////////////////////////////// P4
  70.     public static void reverseStack(IntStack toRev){
  71.         IntQueue tempQueue=new ArrayIntQueue();
  72.         int s=toRev.getSize();
  73.         for (int i=0; i<s; i++){
  74.             try{
  75.                 tempQueue.enqueue(toRev.pop());
  76.             } catch (Exception ex){
  77.                 System.out.println(ex.getMessage());
  78.             }
  79.         }
  80.         for (int i=0; i<s; i++){
  81.             try{
  82.                 toRev.push(tempQueue.dequeue());
  83.             } catch (Exception ex){
  84.                 System.out.println(ex.getMessage());
  85.             }
  86.         }
  87.     }
  88.    
  89. ///////////////////////////////////////////////////////////////////////////////////////// P5
  90.     public static void reverseQueue(IntQueue toRev){
  91.         IntStack tempStack=new ArrayIntStack();
  92.         int s=toRev.getSize();
  93.         for (int i=0; i<s; i++){
  94.             try{
  95.                 tempStack.push(toRev.dequeue());
  96.             } catch (Exception ex){
  97.                 System.out.println(ex.getMessage());
  98.             }
  99.         }
  100.         for (int i=0; i<s; i++){
  101.             try{
  102.                 toRev.enqueue(tempStack.pop());
  103.             } catch (Exception ex){
  104.                 System.out.println(ex.getMessage());
  105.             }
  106.         }
  107.     }
  108.    
  109. ///////////////////////////////////////////////////////////////////////////////////////// P6
  110.     public static boolean isPalindrome(IntQueue q){
  111.         IntStack tempStack=new ArrayIntStack();
  112.         IntQueue tempQueue=new ArrayIntQueue();
  113.         int s=q.getSize();
  114.         for (int i=0; i<s; i++){
  115.             int x;
  116.             try {
  117.                 x=q.dequeue();
  118.             } catch (Exception ex){
  119.                 System.out.println(ex.getMessage());
  120.                 x=0;
  121.             }
  122.             tempStack.push(x);
  123.             tempQueue.enqueue(x);
  124.             q.enqueue(x);
  125.         }
  126.         boolean res=true;
  127.         for (int i=0; i<s; i++){
  128.             try {
  129.                 if (tempStack.pop()!=tempQueue.dequeue()){
  130.                     res=false;
  131.                     break;
  132.                 }
  133.             } catch (Exception ex) {
  134.                 System.out.println(ex.getMessage());
  135.             }
  136.         }
  137.         return res;
  138.     }
  139.    
  140. ///////////////////////////////////////////////////////////////////////////////////////// P7
  141.     public static void insert(IntStack st, int pos, int val){
  142.         int s=st.getSize();
  143.         IntStack tempStack=new ArrayIntStack();
  144.         for (int i=0; i<s+1-pos; i++){
  145.             try {
  146.                 tempStack.push(st.pop());
  147.             } catch (Exception ex){
  148.                 System.out.println(ex.getMessage());
  149.             }
  150.         }
  151.         st.push(val);
  152.         for (int i=0; i<s+1-pos; i++){
  153.             try {
  154.                 st.push(tempStack.pop());
  155.             } catch (Exception ex){
  156.                 System.out.println(ex.getMessage());
  157.             }
  158.         }
  159.     }
  160.    
  161. ///////////////////////////////////////////////////////////////////////////////////////// P8
  162.     public static IntStack merge(IntStack s1, IntStack s2){
  163.         IntStack tempStack=new ArrayIntStack(), resStack=new ArrayIntStack();
  164.         while (s1.getSize()>0 || s2.getSize()>0){
  165.             if (s1.getSize()>0 && s2.getSize()>0){
  166.                 int x, y;
  167.                 try {
  168.                     x=s1.pop();
  169.                     y=s2.pop();
  170.                 } catch (Exception ex){
  171.                     System.out.println(ex.getMessage());
  172.                     x=0;
  173.                     y=0;
  174.                 }
  175.                 if (x>=y){
  176.                     tempStack.push(x);
  177.                     s2.push(y);
  178.                 } else {
  179.                     tempStack.push(y);
  180.                     s1.push(x);
  181.                 }
  182.             } else if (s1.getSize()>0){
  183.                 try{
  184.                     tempStack.push(s1.pop());
  185.                 } catch (Exception ex){
  186.                     System.out.println(ex.getMessage());
  187.                 }
  188.             } else {
  189.                 try{
  190.                     tempStack.push(s2.pop());
  191.                 } catch (Exception ex){
  192.                     System.out.println(ex.getMessage());
  193.                 }
  194.             }
  195.         }
  196.         int s=tempStack.getSize();
  197.         for (int i=0; i<s; i++){
  198.             try {
  199.                 resStack.push(tempStack.pop());
  200.             } catch (Exception ex){
  201.                 System.out.println(ex.getMessage());
  202.             }
  203.         }
  204.         return resStack;
  205.     }
Advertisement
Add Comment
Please, Sign In to add comment