Hey, I have tried me best to answer your question. If still you have any query, then do tell me in comments. And, if you found the solution helpful, then pls do upvote...Thanks :)
//Comments are provided for better understanding..
*************************************************************
Code-
class Stack{
static final int MAX=100;
int top;
int a[]=new int[MAX];
//Default Constructor
Stack(){
top=-1;
}
//method to push an element to stack
boolean push(int val){
if(top>=(MAX-1)){
System.out.println("Stack is full");
return false;
}else{
a[++top]=val;
System.out.println(val+" added to stack...");
return true;
}
}
//method to check if a stack is empty or not
boolean isEmpty(){
return (top<0);
}
//method to pop an element from the stack
int pop(){
if(top<0){
System.out.println("Stack is empty...");
return 0;
}else{
int val=a[top--];
return val;
}
}
//method to get the top element of stack
int peek(){
if(top<0){
System.out.println("Stack is empty...");
return 0;
}else{
int val=a[top];
return val;
}
}
//method to reverse a stack
Stack reverseStack(Stack stack){
Stack new_stack=new Stack();
while (!stack.isEmpty()){
new_stack.push(stack.pop());
}
return new_stack;
}
//Main method
public static void main(String []args){
Stack stack=new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println(stack.peek());
Stack new_stack=stack.reverseStack(stack);
System.out.println(new_stack.peek());
}
}
please answer Now ----data structure - java Q1: Create a complete Stack data structure with its...
Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....
Create a new Java Application that test MyStack by having the following methods in main class: 1. A method to generate a number of element between two given values and save them in a stack 2. A method to print a stack (10 elements per line). The original stack should remain as is after the print 3. A method to exchange the first element and the last element in a stack 4. A Boolean method to search for a value...
Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...
This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...
STACK Data Structure using JAVA 1. In a FILO structure, elements are processed in reverse order, in which they arrive. Suppose during execution, an element of the stack becomes least important in terms of priority and has to be but at the bottom of the stack. Method Signature: public void decreasePriority(<AnyType> el) Write an operator that places a certain element of the stack at the bottom of the stack.
Write a program that uses a stack to reverse its inputs. Your
stack must be generic and you must demonstrate that it accepts both
String and Integer types. Your stack must implement the following
methods:
push,
pop,
isEmpty (returns true if the stack is empty and false
otherwise), and
size (returns an integer value for the number of items in the
stack).
You may use either an ArrayList or a LinkedList to implement
your stack. Also, your pop method must...
Using java Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list. Create three functions that utilize these data structures Write a function that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse...
In Java please. 2. Create a stack class MyStack2 that extends ArrayList. Write a test program that reads a number of strings from the user then displays them in reverse order. 3. Modify your code in (2) so that MyStack become a generic class (i.e. MyStack).
java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> { LinkedList<T> list; public Stack() { list = new LinkedList<T>(); } public boolean isEmpty() { return list.isEmpty(); ...
Java:
Create the skeleton.
Create a new file called ‘BasicJava4.java’
Create a class in the file with the appropriate name.
public class BasicJava4 {
Add four methods to the class that return a default value.
public static boolean isAlphabetic(char aChar)
public static int round(double num)
public static boolean useSameChars(String str1, String
str2)
public static int reverse(int num)
Implement the methods.
public static boolean isAlphabetic(char aChar):
Returns true if the argument is an alphabetic character, return
false otherwise. Do NOT use...