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

Stack (Using Array)

This document contains a Java implementation of a Stack data structure with methods for pushing, popping, peeking, and checking if the stack is empty. The main method demonstrates the functionality of the stack by performing various operations and printing the results. It includes error handling for stack overflow and underflow conditions.

Uploaded by

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

Stack (Using Array)

This document contains a Java implementation of a Stack data structure with methods for pushing, popping, peeking, and checking if the stack is empty. The main method demonstrates the functionality of the stack by performing various operations and printing the results. It includes error handling for stack overflow and underflow conditions.

Uploaded by

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

package test;

class Stack
{
static final int MAX = 1000;
int top;
int a[] = new int[MAX];

Stack()
{
top = -1;
}

public static void main(String[] args)


{
Stack s = new Stack();
if([Link]())
{
[Link]("Stack is Empty");
}
else
{
[Link]("Stack is not Empty");
}
[Link](10);
[Link](20);
[Link](30);
if([Link]())
{
[Link]("Stack is Empty");
}
else
{
[Link]("Stack is not Empty");
}
int popped = [Link]();
int peeked = [Link]();
}

public boolean push(int x)


{
if(top >= (MAX-1))
{
[Link]("Stack Overflow");
return false;
}

a[++top] = x;
[Link](x+" Element has been pushed into Stack");
return true;
}

public int pop()


{
if(top < 0)
{
[Link]("Stack Underflow");
return Integer.MIN_VALUE;
}

int popped = a[top--];


[Link](popped+" Element has been popped from Stack");
return popped;
}
public int peek()
{
if(top < 0)
{
[Link]("Stack Underflow");
return Integer.MIN_VALUE;
}

int peeked = a[top];


[Link](peeked+" Element has been peeked from Stack");
return peeked;
}

public boolean isEmpty()


{
return (top < 0);
}
}

You might also like