Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////////////////// P1
- public static int evenCount(IntStack stk){
- IntStack temp=new ArrayIntStack();
- int s=stk.getSize(), cnt=0;
- for (int i=0; i<s; i++){
- int x;
- try {
- x=stk.pop();
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- x=1;
- }
- if (x%2==0){
- cnt++;
- }
- temp.push(x);
- }
- for (int i=0; i<s; i++){
- try {
- stk.push(temp.pop());
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- return cnt;
- }
- ///////////////////////////////////////////////////////////////////////////////////////// P2
- public static IntStack copyStack(IntStack orig) {
- IntStack res=new ArrayIntStack(), temp=new ArrayIntStack();
- int s=orig.getSize();
- for (int i=0; i<s; i++){
- try {
- temp.push(orig.pop());
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- for (int i=0; i<s; i++){
- try {
- int x=temp.pop();
- res.push(x);
- orig.push(x);
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////////////// P3
- public static IntQueue copyQueue(IntQueue orig){
- IntQueue res=new ArrayIntQueue();
- int s=orig.getSize();
- for (int i=0; i<s; i++){
- int x;
- try{
- x=orig.dequeue();
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- x=0;
- }
- orig.enqueue(x);
- res.enqueue(x);
- }
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////////////// P4
- public static void reverseStack(IntStack toRev){
- IntQueue tempQueue=new ArrayIntQueue();
- int s=toRev.getSize();
- for (int i=0; i<s; i++){
- try{
- tempQueue.enqueue(toRev.pop());
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- for (int i=0; i<s; i++){
- try{
- toRev.push(tempQueue.dequeue());
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////// P5
- public static void reverseQueue(IntQueue toRev){
- IntStack tempStack=new ArrayIntStack();
- int s=toRev.getSize();
- for (int i=0; i<s; i++){
- try{
- tempStack.push(toRev.dequeue());
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- for (int i=0; i<s; i++){
- try{
- toRev.enqueue(tempStack.pop());
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////// P6
- public static boolean isPalindrome(IntQueue q){
- IntStack tempStack=new ArrayIntStack();
- IntQueue tempQueue=new ArrayIntQueue();
- int s=q.getSize();
- for (int i=0; i<s; i++){
- int x;
- try {
- x=q.dequeue();
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- x=0;
- }
- tempStack.push(x);
- tempQueue.enqueue(x);
- q.enqueue(x);
- }
- boolean res=true;
- for (int i=0; i<s; i++){
- try {
- if (tempStack.pop()!=tempQueue.dequeue()){
- res=false;
- break;
- }
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- return res;
- }
- ///////////////////////////////////////////////////////////////////////////////////////// P7
- public static void insert(IntStack st, int pos, int val){
- int s=st.getSize();
- IntStack tempStack=new ArrayIntStack();
- for (int i=0; i<s+1-pos; i++){
- try {
- tempStack.push(st.pop());
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- st.push(val);
- for (int i=0; i<s+1-pos; i++){
- try {
- st.push(tempStack.pop());
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////// P8
- public static IntStack merge(IntStack s1, IntStack s2){
- IntStack tempStack=new ArrayIntStack(), resStack=new ArrayIntStack();
- while (s1.getSize()>0 || s2.getSize()>0){
- if (s1.getSize()>0 && s2.getSize()>0){
- int x, y;
- try {
- x=s1.pop();
- y=s2.pop();
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- x=0;
- y=0;
- }
- if (x>=y){
- tempStack.push(x);
- s2.push(y);
- } else {
- tempStack.push(y);
- s1.push(x);
- }
- } else if (s1.getSize()>0){
- try{
- tempStack.push(s1.pop());
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- } else {
- try{
- tempStack.push(s2.pop());
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- }
- int s=tempStack.getSize();
- for (int i=0; i<s; i++){
- try {
- resStack.push(tempStack.pop());
- } catch (Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- return resStack;
- }
Advertisement
Add Comment
Please, Sign In to add comment