Kotlin Stack
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
Stack provides a data structure that implements a Last In First out mechanism on the objects on stack. This data structure is useful to implement a series function calls for instance. This is where StackOverflow exception comes from. Stack is part of the Java collections framework. Stack extends the Vector class to provide Stack specific operations.
Following are the operations we can perform on a Stack.
We can add several elements to Stack using addAll method. In the following example, we created a Stack and added four planets using addAll method.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Jupiter"))
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
Earth
Jupiter
We can use push method to push an element to the top of the Stack. In the following example, we used push method to push Pluto to the top of the Stack.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Jupiter"))
planets.push("Pluto")
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
Earth
Jupiter
Pluto
We can use pop method to remove element from the top of the Stack. In the following example, we used pop method to remove Pluto from the top of the Stack. We decided that Pluto is not needed among Planets.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Jupiter"))
planets.push("Pluto")
println("List of Planets = ")
for(planet in planets)
{
println(planet)
}
var poppedPlanet = planets.pop()
println("\nPopped Planet = " + poppedPlanet)
println("\nList of Planets after pop = ")
for(planet in planets)
{
println(planet)
}
}
Output:
List of Planets =
Mercury
Venus
Earth
Jupiter
Pluto
Popped Planet = Pluto
List of Planets after pop =
Mercury
Venus
Earth
Jupiter
We can use peek method to see the element at the top of the Stack without popping it. In the following example, we used peek to see the top of the Stack. In the end the planet Pluto is not removed from the Stack of planets.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Jupiter"))
planets.push("Pluto")
println("List of Planets = ")
for(planet in planets)
{
println(planet)
}
var peekPlanet = planets.peek()
println("\nPeek Planet = " + peekPlanet)
println("\nList of Planets after peek = ")
for(planet in planets)
{
println(planet)
}
}
Output:
List of Planets =
Mercury
Venus
Earth
Jupiter
Pluto
Peek Planet = Pluto
List of Planets after peek =
Mercury
Venus
Earth
Jupiter
Pluto
We can search for an element in stack using search function. If search is successful, the search function will return the position in Stack where the element is found. It the search does not find the element, it will return -1. In the following example we are trying to search planets for Earth and Pluto.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Jupiter"))
println("Search for Earth = " + planets.search("Earth"))
println("Search for Pluto = " + planets.search("Pluto"))
}
Output:
Search for Earth = 2
Search for Pluto = -1
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
println("Stack capacity = " + planets.capacity())
}
Output:
Stack capacity = 10
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.ensureCapacity(100)
println("Stack capacity = " + planets.capacity())
}
Output:
Stack capacity = 100
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
Earth
Pluto
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
var firstPlanet = planets.firstElement()
println("First planet = " + firstPlanet)
}
Output:
First planet = Mercury
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
var lastPlanet = planets.lastElement()
println("First planet = " + lastPlanet)
}
Output:
Last planet = Pluto
We can use method addAll to add a list of elements to stack. In the following example, we are adding more planets to our stack of planets using addAll method.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.addAll(listOf("Jupiter", "Saturn"))
for(planet in planets)
{
println(planet);
}
}
Output:
Mercury
Venus
Earth
Pluto
Jupiter
Saturn
We can use add method, specifying an index where to add the element followed by the element. This accomplishes adding element to that location. In the below example, we are using add method to insert Jupiter in between the Stack.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.add(2, "Jupiter");
for(planet in planets)
{
println(planet);
}
}
Output:
Mercury
Venus
Jupiter
Earth
Pluto
We can use contains method to check if Stack contains the element or not. In the below example, we are checking if Stack contains the planet Pluto.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
if(planets.contains("Pluto"))
{
println("Pluto is a Planet!")
}
else
{
println("Pluto is NOT a Planet!")
}
}
Output:
Pluto is a Planet!
We can use remove method to remove an element from Stack. In the below example, we are removing the planet Pluto from the Stack of planets.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.remove("Pluto")
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
Earth
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.remove("Jupiter")
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
Earth
Pluto
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.removeAll(listOf("Earth", "Pluto"))
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.removeAt(2)
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
Pluto
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.removeIf { x -> x.startsWith("E")}
for(planet in planets)
{
println(planet)
}
}
Output:
Mercury
Venus
Pluto
We can reverse the elements in place for the Stack using reverse function. In the below example, we are reversing the planets stack and printing the stack.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>)
{
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.reverse()
for(planet in planets)
{
println(planet)
}
}
Output:
Pluto
Earth
Venus
Mercury
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>) {
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
var planetsReversed = planets.reversed()
println("Original Planets = ")
for (planet in planets) {
println(planet)
}
println("\n\nPlanets Reversed = ")
for (planet in planetsReversed) {
println(planet)
}
}
Output:
Original Planets =
Mercury
Venus
Earth
Pluto
Planets Reversed =
Pluto
Earth
Venus
Mercury
We can clear the contents of stack using clear function. In the following example, we are using the clear function to clear the contents of the stack. We also demonstrate a property of stack called size. size property can be used to find out the number of elements in the stack.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>) {
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
planets.clear()
for (planet in planets) {
println(planet)
}
println("Size of stack = " + planets.size)
}
Output:
Size of stack = 0
We can get the count of elements in stack using count function. In the below example, we are using the count function to get the count for the number of planets in the stack.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>) {
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
println("Count of elements in stack = " + planets.count())
}
Output:
Count of elements in stack = 4
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>) {
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Pluto"))
var numOfPlanetsStartingWithE = planets.count { x -> x.startsWith("E") }
println("Count of planets starting with E in stack = " + numOfPlanetsStartingWithE)
}
Output:
Count of planets starting with E in stack = 1
We can remove duplicate elements from stack and get a new stack, using distinct function. In the below example, we have some duplicate entries in our planets stack. We then used distinct function call to get a new stack without the duplicates.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>) {
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Earth", "Venus", "Pluto"))
var uniquePlanets = planets.distinct()
for(planet in uniquePlanets)
{
println(planet)
}
}
Output:
Mercury
Venus
Earth
Pluto
We can check for emptiness of a stack using isEmpty function. In the following example, we check if stack of planets is empty or not and print a message accordingly.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>) {
var planets = Stack<String>();
planets.addAll(listOf("Mercury", "Venus", "Earth", "Earth", "Venus", "Pluto"))
planets.clear()
if(planets.isEmpty())
{
println("Planets stack is empty!")
}
else
{
println("Planets stack is NOT Empty!")
}
}
Output:
Planets stack is empty!
We can use the max function on stack to find the maximum element. In the below example, we used max function to determine the highest integer in our stack.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>) {
var numbers = Stack<Int>();
numbers.addAll(listOf(21, 44, 99, 74, 33))
var maxValue = numbers.max()
println("Stack Max value = " + maxValue)
}
Output:
Stack Max value = 99
We can use the min function on stack to find the minimum element. In the below example, we used min function to determine the smallest integer in our stack.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>) {
var numbers = Stack<Int>();
numbers.addAll(listOf(21, 44, 99, 74, 33))
var minValue = numbers.min()
println("Stack Min value = " + minValue)
}
Output:
Stack Min value = 21
We can use the get function to get value at a given index. In the following example, we used get function to retrieve the value at position 2 of the Stack.
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>) {
var numbers = Stack<Int>();
numbers.addAll(listOf(21, 44, 99, 74, 33))
var value = numbers.get(2)
println("value = " + value)
}
Output:
value = 99
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>) {
var numbers = Stack<Int>();
numbers.addAll(listOf(21, 44, 99, 74, 33))
var value = numbers[2]
println("value = " + value)
}
Output:
value = 99
package com.cosmiclearn.test
import java.util.Stack
fun main(args: Array<String>) {
var numbers = Stack<Int>();
numbers.addAll(listOf(21, 44, 99, 74, 33))
var value = numbers[2]
println("value = " + value)
}
Output:
value = 2