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
write a java program implementing stacks using arrays. (not with linked list)
Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using only a constant amount of additional storage and no recursion. Assume the list object has a head pointer _head and consists of Node objects; a Node object has a pointer Node* _next
Write a java program implementing the Linked list. It should be on an small office who has 5 employees. The program ask the user for ID, First name, Last name and what field the work in(eg: accounting, programmer, HR etc). Each employee (with all the information of that paticular employee) should be placed in one node in the program. The program should repeat and ask the user for all 5 employees information. Also when you display the Linked list it...
Using Java: Provide an implementation of the LinkedQueue class by implementing it using two stacks. Submit your implementation on a java class/file called Queue2Stack.jar. You can use the implementations of stacks provided by the Java API for this exercise or you can use the LinkedStack class we implemented in class.
Java Write a pseudocode and Java program to implement the Stack using arrays. Write Push(), Pop(),and Display() methods to demonstrate that it works.
WRITE A JAVA PROGRAM using STACKS and backtracing to solves the N Queens Problem . The program takes the user's input integer for N and prints out all the solutions for N . The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. For example, the following is the output for 4 entered for userinput. Output for 4 Queens : 1- * * Q * Q *...
use java thanks 1.Given the following arrays, write a Java program to generate the output below. String [] item = {"milo", "water", "coke", "tea", "coffee"}; double [] price = {3.00, 1.00, 5.00, 2.00, 3.50}; int [] quantity = {200, 500, 350, 100, 700}; Re-write Question 1 using array list and display the sale of the day report. Re-write Question 1 using 2-dimensional array and display the sale of the day report. Write a segment program to replace...
Using Java- Write a program that takes an arithmetic expression in an infix form, converts it to a postfix form and then evaluates it. Use linked lists for the infix and postfix queues and the operator and value stacks. You must use sperate Stack and Queue classes with a driver class to run the program. example Input : 111++ Output : (1+ (1+ 1)) Answer: 3
Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that holds an array for the integer values 1-10. Pass the array to a method that will calculate the values to the power of 2 of each element in the array. Then return the list of calculated values back to the main method and print the values
Q1. Write a program in Java a. Create 2 separate arrays, of user defined values, of same size b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....
Java - Write an insertBefore method for a List implemented as a Linked List that contains two dummy nodes. - Write an insertAfter method for a List implemented as a Linked List that contains two dummy nodes.