Answers for "stack in java"

30

java stack

// construct with non-primative elements only!
Stack<String> stack = new Stack<String>();

// to add a value to the top of the stack:
stack.push("Hello");

// to return and remove a value from the top:
String top = stack.pop();

// to return a value without removing it:
String peek = stack.peek();
Posted by: Guest on March-16-2020
11

java stack methods

import java.util.Stack<E>;
Stack<Integer> myStack = new Stack<Integer>();
myStack.push(1);
myStack.pop();
myStack.peek();
myStack.empty(); // True if stack is empty
Posted by: Guest on April-06-2020
8

stack class in java

import java.util.Stack;
class Main {
    public static void main(String[] args) {
        Stack<String> animals= new Stack<>();
        // Add elements to Stack
        animals.push("Dog");
        animals.push("Horse");
        // Remove element from Stack
      	animals.pop();
      	// Access element from top of Stack
      	animals.peek();
    }
}
Posted by: Guest on May-20-2020
4

stack in java

// Java code for stack implementation 

import java.io.*; 
import java.util.*; 

class Test 
{ 
	// Pushing element on the top of the stack 
	static void stack_push(Stack<Integer> stack) 
	{ 
		for(int i = 0; i < 5; i++) 
		{ 
			stack.push(i); 
		} 
	} 
	
	// Popping element from the top of the stack 
	static void stack_pop(Stack<Integer> stack) 
	{ 
		System.out.println("Pop Operation:"); 

		for(int i = 0; i < 5; i++) 
		{ 
			Integer y = (Integer) stack.pop(); 
			System.out.println(y); 
		} 
	} 

	// Displaying element on the top of the stack 
	static void stack_peek(Stack<Integer> stack) 
	{ 
		Integer element = (Integer) stack.peek(); 
		System.out.println("Element on stack top: " + element); 
	} 
	
	// Searching element in the stack 
	static void stack_search(Stack<Integer> stack, int element) 
	{ 
		Integer pos = (Integer) stack.search(element); 

		if(pos == -1) 
			System.out.println("Element not found"); 
		else
			System.out.println("Element is found at position: " + pos); 
	} 


	public static void main (String[] args) 
	{ 
		Stack<Integer> stack = new Stack<Integer>(); 

		stack_push(stack); 
		stack_pop(stack); 
		stack_push(stack); 
		stack_peek(stack); 
		stack_search(stack, 2); 
		stack_search(stack, 6); 
	} 
}
Posted by: Guest on February-02-2021
0

java stack pop

package com.tutorialspoint;

import java.util.*;

public class StackDemo {
   public static void main(String args[]) {

      // creating stack
      Stack st = new Stack();

      // populating stack
      st.push("Java");
      st.push("Source");
      st.push("code");

      // removing top object
      System.out.println("Removed object is: "+st.pop());

      // elements after remove
      System.out.println("Elements after remove: "+st);
   }    
}
Posted by: Guest on April-14-2020
1

stack implementation

typedef struct Nodo{
   Elem val;
   struct Nodo *next;
} *Stack;
Stack Empty(){return NULL;}
bool IsEmpty(Stack a){return a==NULL;}
Elem Top(Stack a){return a->val;} 
Stack Pop(Stack l){return l->next;}
Stack Push(Elem x,Stack res){
    Stack nuevo=(Stack)malloc(sizeof(struct Nodo));
    nuevo->val=x;
    nuevo->next=res;
    return nuevo;
}
Posted by: Guest on April-30-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language