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

Stack Program

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)
42 views2 pages

Stack Program

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
You are on page 1/ 2

import java.util.

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 == S.length)
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--)
System.out.println(S[i]);
}

public static void main(String args[]) {


int ch;
StackPrg obj = new StackPrg();
Scanner in = new Scanner(System.in);
System.out.println("Enter size of stack:");
int size = in.nextInt();
obj.createStack(size);

do {
System.out.println("\n 1.Push\n 2.Pop\n 3.Print\n 0.Exit\n ");
ch = in.nextInt();

switch(ch) {
case 1:
try {if(obj.isFull()!= true)
System.out.println("Enter:");
int e = in.nextInt();
obj.push(e);
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Stack full");
}
/* else {
System.out.println("Stack full");
}*/
break;
case 2:
if(obj.isEmpty()!= true) {
System.out.println("Poped: " + obj.pop());

}
else {
System.out.println("Stack Empty");
}
break;
case 3:
if(obj.isEmpty()!= true) {
obj.printStack();
}
else {
System.out.println("Stack empty");
}
break;
case 0:
System.out.println("Exiting......");
break;
default:
System.out.println("Wrong choice");
break;
}
} while(ch!=0);

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy