Thursday, June 9, 2011

Some Stack Methods

Good Morning !
how are you ? Am starving i did not eat my breakfast yet ^^"
any how , as usual today i studied stack , and later I'll study Queue & linkedList.
this is source contains following methods :
  • copy stack 
  • store element without changing the Stack ( the last one / second one )
  • [Advanced] - pick by position
emm the source code is not well organize but method are easy to understand
any way if you've any question be free to ask. :)

 
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package operationonstack; import java.util.Scanner; import java.util.Stack; /** * * @author USER */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { // Set secondElement to the second element in myStack, //leaving myStack without its original top two elements. Scanner sc = new Scanner(System.in); char Element; char store; String he = sc.next(); char h = he.charAt(0); Stack A = new Stack(); Stack B = new Stack(); char a[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'I'}; if (h == 'b') { storeELE(A, a); } if (h == 'S') { for (int i = 0; i < a.length; i++) { A.push(a[i]); } System.out.println("A: " + A); while (!A.isEmpty()) { B.push(A.peek()); A.pop(); } System.out.println("B: " + B); store = (Character) B.peek(); B.pop(); Element = (Character) B.peek(); System.out.println("Element: " + Element); A.push(store); while (!B.isEmpty()) { A.push(B.peek()); B.pop(); break; } System.out.println("A still w/o any changes: " + A); } if (h == 'C') { copyStack(A, a); stackPostionTemp(A, 2); } // -------------------------------------------------------------------- // // Set bottom equal to the bottom element in myStack, leaving myStack empty // } public static Stack storeELE(Stack C, char aR[]) { Stack D = new Stack(); char Elemen = ' '; char mem; for (int i = 0; i < aR.length; i++) { C.push(aR[i]); } System.out.println("C: " + C); while (!C.isEmpty()) { D.push(C.peek()); C.pop(); } System.out.println("D: " + D); mem = (Character) D.peek(); System.out.println("bottom: " + mem); while (!D.isEmpty()) { C.push(D.peek()); D.pop(); } System.out.println("C still w/o any changes: " + C); return C; } public static Stack copyStack(Stack C, char a[]) { Stack D = new Stack(); Stack T = new Stack(); for (int i = 0; i < a.length; i++) { C.push(a[i]); } System.out.println("Stack that you wanna copy it: " + C); while (!C.isEmpty()) { D.push(C.peek()); C.pop(); } while (!D.isEmpty()) { T.push(D.peek()); D.pop(); } System.out.println("Copy of the Stack: " + T); while (!T.isEmpty()) { D.push(T.peek()); T.pop(); } while (!D.isEmpty()) { C.push(D.peek()); D.pop(); } System.out.println("The Orginal Stack : " + C); return C; } //PICK BY : POSTION Method public static Stack stackPostionTemp(Stack c, int pos) { Stack D = new Stack(); Stack temp = new Stack(); char E = ' '; while(!c.isEmpty()) { temp.push(c.peek()); c.pop(); } for (int i = 1; i < temp.size(); i++) { if (pos == i) { for (int k = 1; k <=pos; k++) { D.push(temp.peek()); temp.pop(); } System.out.println("Temp Stack is: " + D); E = (Character) D.peek(); D.pop(); System.out.println( "The Value you wanna pick is : "+E); while(!D.isEmpty()) { temp.push(D.peek()); D.pop(); } while(!temp.isEmpty()) { c.push(temp.peek()); temp.pop(); } } } System.out.println("the Stack after: " + c); return c; } public static Stack PoP(Stack c, int pos) { Stack D = new Stack(); char E = ' '; for (int i = 1; i < c.size(); i++) { if (pos == i) { for (int k = 1; k <=pos; k++) { D.push(c.peek()); c.pop(); } System.out.println("Temp Stack is: " + D); E = (Character) D.peek(); D.pop(); System.out.println( "The Value you wanna pick is : "+E); for (int k = 1; k <=D.size(); k++) { c.push(D.peek()); D.pop(); } } } System.out.println("the Stack after: " + c); return c; } } // copy - Right iShrne - 2011

No comments: