Wednesday, January 5, 2011

Article | Stack Using Array In Java

Here is a Source code for the Stack using Array In Java.
A stack is a last in, first out (LIFO) abstract data type and data structure. A stack can have any abstract data type as an element, but is characterized by only two fundamental operations: push and pop. The push operation adds an item to the top of the stack, hiding any items already on the stack, or initializing the stack if it is empty. The pop operation removes an item from the top of the stack, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty stack.

Source Code



  1. /* Stack using array */

  2. import java.io.*;

  3. class MyStack

  4. {

  5. int stack[] = new int[4];

  6. int n=4;

  7. int item;

  8. int top=-1;

  9. InputStreamReader isr=new InputStreamReader(System.in);

  10. BufferedReader br=new BufferedReader(isr);

  11. public void push() throws Exception

  12. {

  13. if(top==n-1)

  14. {

  15. System.out.println("Stack is Full !!");

  16. System.out.println("Status of the stack is\n");

  17. for(int i=0;i<=top;i++)

  18. System.out.print(" "+stack[i]);

  19. System.out.print("\n");

  20. }

  21. else

  22. {

  23. System.out.println("Enter the element to be inserted");

  24. item=Integer.parseInt(br.readLine());

  25. stack[++top]=item;

  26. }

  27. }

  28. public int pop() throws Exception

  29. {

  30. if(top==-1)

  31. {

  32. System.out.println("Stack is Empty !!");

  33. //System.exit(-1);

  34. }

  35. else

  36. {

  37. item=stack[top];

  38. top--;

  39. System.out.println(item+" is poped out");

  40. }

  41. return(item);

  42. }

  43. public void traverse() throws Exception

  44. {

  45. int i;

  46. if(top==-1)

  47. {

  48. System.out.println("Stack is Empty !!");

  49. // System.exit(0);

  50. }

  51. else

  52. {

  53. for(i=top;i>=0;i--)

  54. {

  55. System.out.println("Traversed the element "+stack[i]+"\n");

  56. }

  57. }

  58. }

  59. }

  60. class Stack_Menu

  61. {

  62. public void menu() throws Exception

  63. {

  64. InputStreamReader isr=new InputStreamReader(System.in);

  65. BufferedReader br=new BufferedReader(isr);

  66. MyStack ms=new MyStack();

  67. while(true)

  68. {

  69. System.out.println("MENU");

  70. System.out.println("1.push\n2.pop\n3.traverse\n4.exit");

  71. System.out.println("\n\nEnter your choice");

  72. int ch=Integer.parseInt(br.readLine());

  73. switch(ch)

  74. {

  75. case 1: ms.push();

  76. break;

  77. case 2: ms.pop();

  78. break;

  79. case 3: ms.traverse();

  80. break;

  81. case 4: System.out.println("Good Bye");

  82. System.exit(0);

  83. default: System.out.println("Wrong Choice !!");

  84. }

  85. }

  86. }

  87. }

  88. class Main

  89. {

  90. public static void main(String[] arg)

  91. {

  92. Stack_Menu sm= new Stack_Menu();

  93. try

  94. {

  95. sm.menu();

  96. }

  97. catch(Exception ex)

  98. {

  99. System.out.println("Input-Output Error");

  100. }

  101. }

  102. }

Loading...

Comments :

0 comments to “ Stack Using Array In Java ”

Post a Comment

Enter any comments

Followers