HELLO WELCOME TO THIS JAVA DATA STRUCTURE STACK BLOG...........
JAVA STACK IMPLEMENTATION USING ARRAY WITH
SOURCE CODE...........
FIRST ALL OF WE DISCUSS ABOUT .......
1.) WHAT IS STACK AND THEIR OPERATIONS ?????
SO THANK YOU MEET YOU IN NEW DATA STRUCTURES QUEUE IMPLEMENTATION USING JAVA ...........................
JAVA STACK IMPLEMENTATION USING ARRAY WITH
SOURCE CODE...........
FIRST ALL OF WE DISCUSS ABOUT .......
1.) WHAT IS STACK AND THEIR OPERATIONS ?????
Images Java |
Java Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
Mainly the following three basic operations are performed in the stack:
- Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
- Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
- Top: Returns top element of stack.
- DISPLAY METHOD TO DISPLAY VALUES FROM STACK ------
SO IN REAL LIFE EXAMPLE STACK IS BUNDLE AND SET OF ANYTHING SUCH AS ------- A SET OF BOOKS IN LIBRARY OR A SET OF DINNER PLATES--
Images |
THAT MEANS LAST BOOK PLACED FIRST OUT FROM THE BUNDLES.......
HERE WE MAKE THREE METHODS IN JAVA SOURCE CODE TO IMPLEMENT STACK USING ALSO SWITCH CASE -------
1.) PUSH METHOD EXPLANATION-----
HERE IS THE CODE FOR STACK IMPLEMENTATION IN JAVA IN THIS CODE FIRST WE DECLARE TOP=-1 BECAUSE OF THIS ARRAY STARTS FROM INDEX A[0] . WHEN TOP++ INCREMENT THEN A[TOP] = VALUE INSERT OR PUSH SUCCESSFULLY.
A[TOP] = A[0] = 45 SUPPOSE -----
IN IF CONDITION WE CHECK IF SIZE OF ARRAY == OF SIZE OF STACK THEN STACK OVERFLOW OCCUR OTHERWISE ELSE CONDITION EXECUTED SUCCESSFULLY----
2.) POP METHOD EXPLANATION------
HERE IN POP OPERATION IN IF CONDITION FIRST WE CHECK TOP VALUE IF TOP==-1 THEN WE PRINT MESSAGE STACK IS EMPTY NO POP OPERATION ALLOWED ELSE WE POP ELEMENTS FROM STACK BY DECREASING TOP--
A[TOP] = A[0] = 45 POP SUPPOSE --------
3.) DISPLAY METHOD EXPLANATION--------
IN THIS DISPLAY METHOD WE AGAIN CHECK THE IF CONDITION IF STACK AGAIN TOP==-1 THEN PRINT ON DISPLAY NO ELEMENTS FOUND STACK IS EMPTY
OTHERWISE ------
WE KNOW PRINT ELEMENTS FROM ARRAY WE NEED A FOR LOOP THEN WE DECLARE A VARIABLE int i;------
THEN i INITIALIZE WITH 0 AND GOING TO THE TOP VALUE AND i ++ INCREMENT
THEN WE PRINT ELEMENTS FROM THE STACK --------
SOURCE CODE WITH USEFUL COMMENTS ----------
// STACK IMPLEMENTATION IN JAVA USING ARRAY DATA STRUCTURES AMIT. . . . . . . . . . . . .
IMPORT JAVA. UTIL. *;
IMPORT JAVA. UTIL. SCANNER;
CLASS STACKOPERATIONS {
FINAL INT SIZE = 5;
INT ARRAY[] = NEW INT[SIZE];
INT TOP = -1, VALUE;
SCANNER SC = NEW SCANNER(SYSTEM. IN); // TAKE INPUT FROM USER USING SCANNER CLASS
PUBLIC VOID PUSH() // METHOD CREATED FOR PUSH OPERATION
{
IF (TOP == SIZE - 1) {
SYSTEM. OUT. PRINTLN("STACK OVERFLOW NO MORE PUSHES ALLOWED::::::");
} ELSE {
SYSTEM. OUT. PRINTLN("ENTER VALUE TO PUSH:::::");
VALUE = SC. NEXTINT();
TOP++;
ARRAY[TOP] = VALUE;
SYSTEM. OUT. PRINTLN("VALUE SUCCESSFULLY PUSH INTO STACK::::::");
} // END OF PUSH METHOD
}
PUBLIC VOID POP() // NOW POP OPERATION METHOD CREATED. . . . .
{
IF (TOP==-1)
{
SYSTEM. OUT. PRINTLN("STACK UNDERFLOW OR STACK IS EMPTY:::::");
}
ELSE
{
SYSTEM. OUT. PRINTLN("VALUE POP FROM STACK- ----"+ARRAY[TOP]);
TOP--;
}
} // END OF POP OPERATION METHOD. . . . . .
PUBLIC VOID DISPLAY() // DISPLAY METHOD TO SHOW VALUE AT OUTPUT SCREEN. . . . . . . .
{
IF (TOP==-1)
{
SYSTEM. OUT. PRINTLN("STACK IS EMPTY:::::");
}
ELSE
{
SYSTEM. OUT. PRINTLN("VALUE PRESENT IN STACK ARE::::::");
FOR (INT I=0;I<=TOP;I++)
{
SYSTEM. OUT. PRINTLN(ARRAY[I]);
}
}
} // END OF DISPLAY METHOD
}
CLASS MAIN // MAIN METHOD START FROM HERE
{
PUBLIC STATIC VOID MAIN(STRING[] ARGS) {
STACKOPERATIONS OBJECT = NEW STACKOPERATIONS(); // OBJECT CREATED FOR MAIN CLASS
SCANNER INPUT = NEW SCANNER(SYSTEM. IN);
INT CHOICE, T = 1; // T VARIABLE USED FOR USER CHOICE CREATES AND CHOICE VARIABLE FOR SWITCH CASE. . . . .
DO {
SYSTEM. OUT. PRINTLN("CHOOSE YOUR STACK OPERATION------");
SYSTEM. OUT. PRINTLN("1. ) PUSH--");
SYSTEM. OUT. PRINTLN("2. ) POP--");
SYSTEM. OUT. PRINTLN("3. ) DISPLAY--");
SYSTEM. OUT. PRINTLN("ENTER YOUR CHOICE. . . . . . . . . ");
CHOICE = INPUT. NEXTINT();
SWITCH (CHOICE) {
CASE 1:
OBJECT. PUSH();
BREAK;
CASE 2:
OBJECT. POP();
BREAK;
CASE 3:
OBJECT. DISPLAY();
BREAK;
DEFAULT:
SYSTEM. OUT. PRINTLN("INVALID INPUT-----");
}
SYSTEM. OUT. PRINTLN("DO YOU WANT TO CONTINUE PRESS 1 FOR YES OTHER KEY FOR NO. . . . . . ");
T = INPUT. NEXTINT();
} WHILE(T==1);
}
}
OUTPUT IS ----------
choose your stack operation------
1.) PUSH--
2.) POP--
3.) DISPLAY--
ENTER YOUR CHOICE.........
1
enter value to push:::::
56
Value successfully push into Stack::::::
DO YOU WANT TO CONTINUE PRESS 1 FOR YES OTHER KEY FOR NO......
1
choose your stack operation------
1.) PUSH--
2.) POP--
3.) DISPLAY--
ENTER YOUR CHOICE.........
1
enter value to push:::::
67
Value successfully push into Stack::::::
DO YOU WANT TO CONTINUE PRESS 1 FOR YES OTHER KEY FOR NO......
1
choose your stack operation------
1.) PUSH--
2.) POP--
3.) DISPLAY--
ENTER YOUR CHOICE.........
3
VALUE PRESENT IN STACK ARE::::::
56
67
DO YOU WANT TO CONTINUE PRESS 1 FOR YES OTHER KEY FOR NO......
1
choose your stack operation------
1.) PUSH--
2.) POP--
3.) DISPLAY--
ENTER YOUR CHOICE.........
2
VALUE POP FROM STACK 67--67
DO YOU WANT TO CONTINUE PRESS 1 FOR YES OTHER KEY FOR NO......
1
choose your stack operation------
1.) PUSH--
2.) POP--
3.) DISPLAY--
ENTER YOUR CHOICE.........
3
VALUE PRESENT IN STACK ARE::::::
56
0 Comments
Write your comments or feedback here.........