Stack: Difference between revisions

From Rosetta Code
Content added Content deleted
(New page: {Task} ==C++== ==Java== public class Stack { private Node first = null; public boolean isEmpty { return (first == null); } public Object Pop(...)
 
Line 39: Line 39:




public class Stack
public class Stack
{
{
private Node first = null;
private Node first = null;
public bool Empty {
public bool Empty {
get {
get {
return (first == null);
return (first == null);
}
}
}
}
public object Pop() {
public object Pop() {
if (first == null)
if (first == null)
throw new Exception("Can't Pop from an empty Stack.");
throw new Exception("Can't Pop from an empty Stack.");
else {
else {
object temp = first.Value;
object temp = first.Value;
first = first.Next;
first = first.Next;
return temp;
return temp;
}
}
}
}
public void Push(object o) {
public void Push(object o) {
first = new Node(o, first);
first = new Node(o, first);
}
}
class Node
class Node
{
{
public Node Next;
public Node Next;
public object Value;
public object Value;
public Node(object value): this(value, null) {}
public Node(object value): this(value, null) {}
public Node(object value, Node next) {
public Node(object value, Node next) {
Next = next;
Next = next;
Value = value;
Value = value;
}
}
}
}
}
}

Revision as of 03:08, 24 February 2007

{Task}

C++

Java

public class Stack {

   private Node first = null;
   public boolean isEmpty {
       return (first == null);
   }
   public Object Pop() {
       if (first == null) 
           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) {
           Next = next;
           Value = value;
       }
   }

}


C#

 public class Stack
 {
     private Node first = null;
     public bool Empty {
         get {
             return (first == null);
         }
     }
     public object Pop() {
         if (first == null) 
             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) {
             Next = next;
             Value = value;
         }
     }
 }