Monday, September 4, 2017

Java - Stack Class

Source Link:
https://www.tutorialspoint.com/java/util/java_util_stack.htm

Some Useful Methods:

boolean empty()
This method tests if this stack is empty.

E peek()
This method looks at the object at the top of this stack without removing it from the stack.

E pop()
This method removes the object at the top of this stack and returns that object as the value of this function.

E push(E item)
This method pushes an item onto the top of this stack.

int search(Object o)
This method returns the 1-based position where an object is on this stack.

Example:
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");
      
   // checking the top object
   System.out.println("Top object is: "+st.peek());
   }     
}
Let us compile and run the above program, this will produce the following result.
Top object is: code 

2 comments:

Codility - Lesson 16 Greedy algorithms - 2. MaxNonoverlappingSegments

Source Link: https://app.codility.com/programmers/lessons/16-greedy_algorithms/max_nonoverlapping_segments/ Question: Located on a line ...