Question

write a java program implementing stacks using arrays. (not with linked list)

write a java program implementing stacks using arrays. (not with linked list)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code in java Compiler:-

//a class to implement the stack using array
public class stackimplementation{
int size;
int arr[];
int top;
//the logic here , by using an array, there is no need to remove the element//the element directly from the array, simply we can increase or //decrease the pointer,pointing the indexes of the array.
//If we want to delete the memory of the elements in the array, we can do that too by freeing up the memory space ...
//Array is a simple storage space with some predefined capacity or size
//constructor for stackimplementation to intialize the stack
stackimplementation(int size)
{
this.size = size;
this.arr = new int[size];
this.top = -1;
}
//a method to check the stack is empty or not
public boolean isEmpty()
{
if(top==-1) return true;
return false;
}
// a method to check the stack is full or not
public boolean isFull()
{
if(top==(size-1)) return true;
return false;
}
//a method to push the new element into the stack
public void push(int elementtopush)
{
if(top<size) {
top++;
arr[top] = elementtopush;
System.out.println("The Pushed element is:" + elementtopush);
}else if(top>=(size-1))
{
System.out.println("Stack is Full!");
}
//the other way to use the full method to push a element into the stack
/*if(!isFull())
{ arr[++top] = elementtopush;
System.out.println("Pushed element is: "+elementtopush);
}
else{
  
System.out.println("Stack is Full, pop the elements to push new elements")
}*/
}
//a method to pop the element of the stack
// If we change the function to int then return statements to return the popped element is used................
public void pop()
{
if(top<0) {
System.out.println("There are no elements in the stack,that means stack is empty, push the elements........");
}
else
{
top--;
int x = arr[top];
System.out.println("The popped element is: "+x);
}
//the other way to use the empty method to pop a element from the stack
/*if(!isEmpty())
{ int x = arr[top--];
System.out.println("Popped element is: "+x);
}
else{
  
System.out.println("Stack is empty, there are no elements. Push the elements into the stack ");
}*/
}
//to know the top of the element
// here also the method can be changed to
public void peek()
{
System.out.println("The element at the top of the stack is : "+arr[top]);

}
// the main method in the java program which is called by the java compiler
public static void main(String[] args)
{
//an object of the class stackimplementation is created to use the methods defined in the class such as pop,push etc.......
stackimplementation stack = new stackimplementation(15);
//Initially,there are no elements in the stack
// if(stack.isEmpty()) System.out.println("Stack is empty");
stack.pop();
//pushing the elements
stack.push(10);
stack.push(20);
stack.push(30);
//to know the top of the element means, to which index the pointer top is pointed too...........
stack.peek();
//decreasing the pointer top
stack.pop();
//to know the top of the element means, to which index the pointer top is pointed too...........
stack.peek();

//add some more methods if u want
}//end of the main method
}//end class stackimplementation

Output:-

If you want modifications in the code , for further explanation, please feel free to comment

Add a comment
Know the answer?
Add Answer to:
write a java program implementing stacks using arrays. (not with linked list)
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT