• We just launched and are currently in beta. Join us as we build and grow the community.

Stack In JAVA

scurged09

Digital Advertising Guru
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
51
Likes
86
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Stack in JAVA

In this tutorial you will learn
1) What is a stack?
2) How is stack used in programming?
3) Implementation of stack in JAVA
What is a stack?
Stack is one of the most important data structure in programming, it can be imagined as the bottle of balls which is open from one end. So the addition and deletion of balls can be done through one end only. The element which goes last in the stack is first to be extracted from the stack. So the stack works on last in first out principle (LIFO).
How is stack used in programming
Stack is very useful for programming purposes. The stack can be used to store the last used values of some variables as well as in operating system and micro controller programming the stacks can be used handle interrupts and used to store the program counter values and addresses.

Implementation of stack in JAVA
The stack in JAVA is implemented using ordinary array. Stack has few functions associated with it. They are:
Push: Used to put data into the stack
Pop: Used to remove data from the top of the stack
Top: The first element of the array is called the top of the stack.
In the code below first I have defined the class Stack, this class contains data members, methods and the constructor which initialize the data members. Then in the main of the program I have the object of stack class and performed operations on it using its methods. The detail and significance of each line is provided in the comments with the code below.

  1. import

    java.util.Scanner

    ;


  2. public

    class

    UsingJAVA {
  3. public

    static

    class

    Stack

    {

    //we are defining the stack class
  4. private

    int

    maximumSize;

    //its the size of the array
  5. private

    int

    [

    ]

    structure;

    //this is the array in which the contents are stored
  6. private

    int

    top;
  7. public

    Stack

    (

    int

    M)

    //this is the constructor
  8. {
  9. maximumSize =

    M;

    //The size of the array is of the user's choice
  10. structure =

    new

    int

    [

    maximumSize]

    ;

    // we are making an array
  11. top =

    -

    1

    ;

    // top is declared -1 because we need to access the 0th postion at first insertion
  12. }
  13. public

    void

    push(

    int

    j)

    //function to put values into stack.
  14. {
  15. structure[

    ++

    top]

    =

    j;

    //the value of top pointer is increased and value is inserted
  16. }
  17. public

    int

    pop(

    )

    //to take data out of the stack
  18. {
  19. return

    structure[

    top--

    ]

    ;

    //decrement takes the top to the element below the top element
  20. }

  21. public

    boolean

    isEmpty(

    )

    //standard function to check whether the stack is empty or not
  22. {
  23. return

    (

    top ==

    -

    1

    )

    ;
  24. }
  25. public

    boolean

    isFull(

    )

    //standard function to check if the stack is full?
  26. {
  27. return

    (

    top ==

    maximumSize -

    1

    )

    ;
  28. }
  29. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  30. int

    x;
  31. System

    .out

    .println

    (

    "What should be the size of the stack?"

    )

    ;
  32. Scanner inputScanner=

    new

    Scanner(

    System

    .in

    )

    ;
  33. x=

    inputScanner.nextInt

    (

    )

    ;
  34. Stack

    Workstack =

    new

    Stack

    (

    x)

    ;

    //the object of the stack class is made
  35. System

    .out

    .println

    (

    "Please enter the values you want to insert into stack"

    )

    ;
  36. for

    (

    int

    j=

    0

    ;

    j<

    x ;

    j++

    )

    //for loop used to insert the number of values into the stack
  37. {

    int

    z;
  38. z=

    inputScanner.nextInt

    (

    )

    ;
  39. Workstack.push

    (

    z)

    ;

    //pushing a value into stack
  40. }
  41. System

    .out

    .println

    (

    "The contents of the stack are popped out and they are:"

    )

    ;
  42. while

    (

    !

    Workstack.isEmpty

    (

    )

    )

    {
  43. int

    element =

    Workstack.pop

    (

    )

    ;

    //retrieving each element and saving it in variable
  44. System

    .out

    .print

    (

    element)

    ;

    //displaying the variable to screen.
  45. System

    .out

    .println

    (

    )

    ;
  46. }
  47. System

    .out

    .println

    (

    )

    ;
  48. }
  49. }

  50. }

Screen Shot
capture.png


Book traversal links for Stack In JAVA

  • ‹ Queue Interface in JAVA
  • Up
  • Introduction to JAVA ›


Download
You must upgrade your account or reply in the thread to view hidden text.
 

449,708

322,558

322,567

Top