0% found this document useful (0 votes)
9 views2 pages

Java Stack Implementation Example

This Java code defines a Stack class with methods to create a stack of a given size, push elements onto the stack, pop elements off the stack, check if the stack is full or empty, and print the contents of the stack. It uses a main method to test the stack functionality by prompting the user for operations and handling their input until they choose to exit.

Uploaded by

Pritam Patil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Java Stack Implementation Example

This Java code defines a Stack class with methods to create a stack of a given size, push elements onto the stack, pop elements off the stack, check if the stack is full or empty, and print the contents of the stack. It uses a main method to test the stack functionality by prompting the user for operations and handling their input until they choose to exit.

Uploaded by

Pritam Patil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link].

Scanner;
public class StackPrg {

int S[], tos;

void createStack(int size) {


S = new int[size];
tos = -1;
}

void push(int e) {
tos++;
S[tos] = e;
}

boolean isFull() {
if(tos == [Link])
return true;
else
return false;
}

int pop() {
int temp;
temp = S[tos];
tos-- ;
return temp;
}

boolean isEmpty() {
if(tos == -1)
return true;
else
return false;
}

void printStack() {
int i;
for(i = tos; i >=0; i--)
[Link](S[i]);
}

public static void main(String args[]) {


int ch;
StackPrg obj = new StackPrg();
Scanner in = new Scanner([Link]);
[Link]("Enter size of stack:");
int size = [Link]();
[Link](size);

do {
[Link]("\n [Link]\n [Link]\n [Link]\n [Link]\n ");
ch = [Link]();

switch(ch) {
case 1:
try {if([Link]()!= true)
[Link]("Enter:");
int e = [Link]();
[Link](e);
}catch(ArrayIndexOutOfBoundsException e) {
[Link]("Stack full");
}
/* else {
[Link]("Stack full");
}*/
break;
case 2:
if([Link]()!= true) {
[Link]("Poped: " + [Link]());

}
else {
[Link]("Stack Empty");
}
break;
case 3:
if([Link]()!= true) {
[Link]();
}
else {
[Link]("Stack empty");
}
break;
case 0:
[Link]("Exiting......");
break;
default:
[Link]("Wrong choice");
break;
}
} while(ch!=0);

}
}

Common questions

Powered by AI

An ArrayIndexOutOfBoundsException may occur if an attempt is made to push an element to a full stack. This exception is caught in a try-catch block, which outputs a message indicating that the stack is full .

The 'isEmpty' method checks if 'tos' is equal to -1, indicating the stack is empty. This prevents operations like popping or printing when there are no elements, maintaining the integrity of stack operations by avoiding invalid memory accesses .

The 'StackPrg' class checks if the stack is full by using the 'isFull()' method, which compares the current index 'tos' with the length of the array. If they are equal, the stack is full, and no push operation should be executed .

The stack implementation uses a default case in a switch statement to handle invalid choices with a message 'Wrong choice'. This approach informs users of input errors but provides no correction mechanism. It could be improved by re-prompting users until a valid input is provided, enhancing user interaction .

The 'pop' method first calls 'isEmpty()' to check if the stack is empty. If true, it outputs 'Stack Empty' and does not attempt to pop any element. This approach prevents runtime errors such as ArrayIndexOutOfBoundsException by avoiding access to invalid indices in the stack .

To enhance the 'printStack' method, adding index labels or additional format, such as stack position and separating elements with commas, can improve output clarity. Implementing these would provide more informative and readable stack content .

The 'StackPrg' class uses a Scanner object to capture user input through the console. The Scanner is used to read integer inputs, which determine the stack operations such as push and pop, as well as to set the initial size of the stack .

The 'tos' variable, short for 'top of stack', represents the index of the top element in the stack. When pushing an element, 'tos' is incremented to point to the new top. Conversely, 'tos' is decremented during a pop operation to reflect the removal of the last element .

The use of a 'do-while' loop in the main method allows the program to repeatedly prompt the user for commands until a '0' is entered. This structure supports continuous operation by repeatedly allowing stack operations as per user inputs, ensuring the program remains responsive and interactive .

The array-based stack has fixed size, limiting the number of elements it can store. This can lead to stack overflow if the stack exceeds its predefined capacity, making it less flexible for real-world applications where dynamic resizing might be needed. Implementing a dynamically-resizing stack could overcome this limitation .

You might also like