Skip to content

Instantly share code, notes, and snippets.

@mhshams
Created January 26, 2016 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhshams/3d74dd27e7a5dea16468 to your computer and use it in GitHub Desktop.
Save mhshams/3d74dd27e7a5dea16468 to your computer and use it in GitHub Desktop.
package io.dahgan
/**
* Stack is a data structure that stores some elements and these elements can be received in LIFO order.
* That means the last element that is pushed to stack would be the first one that can be poped.
*
* Our stack has a limited capacity and can not store element more than its capacity.
*
* Note to developer:
* - define an array of integers to keep the stack elements.
* - you can initialize the array with (0) in the beginning
* - define a variable "top" to keep the index of last element that has been added to stack. ( -1 in the beginning )
*/
class MyStack(capacity: Int) {
/**
* Adds the given number to the stack
* @return true if the push was successful, false if the stack was full
*/
fun push(n: Int): Boolean = false
/**
* Removes the last element in the stack and returns it.
* @return the last element in the stack, -1 if the stack is empty
*/
fun pop(): Int = -1
/**
* Returns the last element in the stack WITHOUT removing it.
* @return the last element in the stack, -1 if the stack is empty
*/
fun peek(): Int = -1
/**
* Checks if the stack is empty.
* @return true if the stack is empty, false otherwise
*/
fun isEmpty(): Boolean = false
/**
* Checks if the stack capacity is full.
* @return true if the stack is full, false otherwise
*/
fun isFull(): Boolean = false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment