Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
12
Task/Stack/Deja-Vu/stack.djv
Normal file
12
Task/Stack/Deja-Vu/stack.djv
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
local :stack [] #lists used to be stacks in DV
|
||||
|
||||
push-to stack 1
|
||||
push-to stack 2
|
||||
push-to stack 3
|
||||
|
||||
!. pop-from stack #prints 3
|
||||
!. pop-from stack #prints 2
|
||||
!. pop-from stack #prints 1
|
||||
|
||||
if stack: #empty lists are falsy
|
||||
error #this stack should be empty now!
|
||||
|
|
@ -1,29 +1,21 @@
|
|||
public class Stack{
|
||||
private Node first = null;
|
||||
public boolean isEmpty(){
|
||||
return first == null;
|
||||
}
|
||||
public Object Pop(){
|
||||
if(isEmpty())
|
||||
throw new Exception("Can't Pop from an empty Stack.");
|
||||
else{
|
||||
Object temp = first.value;
|
||||
first = first.next;
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
public void Push(Object o){
|
||||
first = new Node(o, first);
|
||||
}
|
||||
class Node{
|
||||
public Node next;
|
||||
public Object value;
|
||||
public Node(Object value){
|
||||
this(value, null);
|
||||
}
|
||||
public Node(Object value, Node next){
|
||||
this.next = next;
|
||||
this.value = value;
|
||||
}
|
||||
import java.util.Stack;
|
||||
|
||||
public class StackTest {
|
||||
public static void main( final String[] args ) {
|
||||
final Stack<String> stack = new Stack<String>();
|
||||
|
||||
System.out.println( "New stack empty? " + stack.empty() );
|
||||
|
||||
stack.push( "There can be only one" );
|
||||
System.out.println( "Pushed stack empty? " + stack.empty() );
|
||||
System.out.println( "Popped single entry: " + stack.pop() );
|
||||
|
||||
stack.push( "First" );
|
||||
stack.push( "Second" );
|
||||
System.out.println( "Popped entry should be second: " + stack.pop() );
|
||||
|
||||
// Popping an empty stack will throw...
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +1,27 @@
|
|||
public class Stack<T>{
|
||||
public class Stack{
|
||||
private Node first = null;
|
||||
public boolean isEmpty(){
|
||||
return first == null;
|
||||
}
|
||||
public T Pop(){
|
||||
public Object Pop(){
|
||||
if(isEmpty())
|
||||
throw new Exception("Can't Pop from an empty Stack.");
|
||||
else{
|
||||
T temp = first.value;
|
||||
Object temp = first.value;
|
||||
first = first.next;
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
public void Push(T o){
|
||||
public void Push(Object o){
|
||||
first = new Node(o, first);
|
||||
}
|
||||
class Node{
|
||||
public Node next;
|
||||
public T value;
|
||||
public Node(T value){
|
||||
public Object value;
|
||||
public Node(Object value){
|
||||
this(value, null);
|
||||
}
|
||||
public Node(T value, Node next){
|
||||
public Node(Object value, Node next){
|
||||
this.next = next;
|
||||
this.value = value;
|
||||
}
|
||||
|
|
|
|||
29
Task/Stack/Java/stack-3.java
Normal file
29
Task/Stack/Java/stack-3.java
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
public class Stack<T>{
|
||||
private Node first = null;
|
||||
public boolean isEmpty(){
|
||||
return first == null;
|
||||
}
|
||||
public T Pop(){
|
||||
if(isEmpty())
|
||||
throw new Exception("Can't Pop from an empty Stack.");
|
||||
else{
|
||||
T temp = first.value;
|
||||
first = first.next;
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
public void Push(T o){
|
||||
first = new Node(o, first);
|
||||
}
|
||||
class Node{
|
||||
public Node next;
|
||||
public T value;
|
||||
public Node(T value){
|
||||
this(value, null);
|
||||
}
|
||||
public Node(T value, Node next){
|
||||
this.next = next;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Task/Stack/Julia/stack.julia
Normal file
8
Task/Stack/Julia/stack.julia
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
stack = {}
|
||||
push!(stack, 1)
|
||||
push!(stack, 2)
|
||||
push!(stack, 3)
|
||||
println(pop!(stack)) # 3
|
||||
println(length(stack)) # 2
|
||||
empty!(stack)
|
||||
println(length(stack)) # 0
|
||||
73
Task/Stack/Nimrod/stack.nimrod
Normal file
73
Task/Stack/Nimrod/stack.nimrod
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import math
|
||||
|
||||
type
|
||||
EStackEmpty = object of E_Base
|
||||
|
||||
TStack* [A] = object
|
||||
data: seq[A]
|
||||
count: int
|
||||
|
||||
proc initStack*[A](initialSize = 32): TStack[A] =
|
||||
assert isPowerOfTwo(initialSize)
|
||||
result.count = 0
|
||||
newSeq(result.data,initialSize)
|
||||
|
||||
proc cap*[A] (s: TStack[A]): int =
|
||||
result = s.data.len
|
||||
|
||||
proc len*[A](stack: TStack[A]): int =
|
||||
result = stack.count
|
||||
|
||||
proc push*[A](s: var TStack[A], item: A) =
|
||||
if s.count == s.data.len:
|
||||
# not enough room, make container bigger
|
||||
var d: Seq[A]
|
||||
newSeq(d,s.len * 2)
|
||||
for i in 0 .. s.data.len - 1:
|
||||
shallowCopy(d[i],s.data[i])
|
||||
shallowCopy(s.data,d)
|
||||
s.data[s.count] = item
|
||||
inc(s.count)
|
||||
|
||||
proc pop*[A](s: var TStack[A]): A {.raises: [EStackEmpty].}=
|
||||
if s.count == 0:
|
||||
raise newException(EStackEmpty,"the stack is empty")
|
||||
dec(s.count)
|
||||
result = s.data[s.count]
|
||||
|
||||
proc top*[A](s: TStack[A]): A =
|
||||
result = s.data[s.count - 1]
|
||||
|
||||
proc isEmpty*[A](s: var TStack[A]): bool =
|
||||
return s.count == 0
|
||||
|
||||
#Tests
|
||||
when isMainModule:
|
||||
var stk: TStack[char] = initStack[char](4)
|
||||
stk.push('a')
|
||||
stk.push('b')
|
||||
stk.push('c')
|
||||
stk.push('d')
|
||||
|
||||
assert(stk.count == 4)
|
||||
assert(stk.data.len == 4)
|
||||
stk.push('e')
|
||||
assert(stk.cap == 8)
|
||||
assert(stk.top == 'e')
|
||||
|
||||
|
||||
discard stk.pop
|
||||
discard stk.pop
|
||||
discard stk.pop
|
||||
discard stk.pop
|
||||
assert(stk.isEmpty == false)
|
||||
discard stk.pop
|
||||
assert(stk.isEmpty == true)
|
||||
|
||||
try:
|
||||
discard stk.pop
|
||||
except:
|
||||
let
|
||||
e = getCurrentException()
|
||||
msg = getCurrentExceptionMsg()
|
||||
echo "Exception: [[", repr(e), "]] msg: ", msg
|
||||
Loading…
Add table
Add a link
Reference in a new issue