User Tools

Site Tools


stack

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
stack [2007/10/28 16:43] – external edit 127.0.0.1stack [2009/05/06 19:58] (current) franck
Line 1: Line 1:
-Below we implement the following Stack interface in several different ways. 
- 
 <code java> <code java>
 /** /**
  * A Stack.  * A Stack.
  */  */
-public interface Stack+public interface Stack<T>
 { {
   /**   /**
Line 11: Line 9:
    *    *
    * @return the top element of this stack.    * @return the top element of this stack.
-   * @throw EmptyException if this stack is empty.+   * @throw Exception if this stack is empty.
    */    */
-  public Integer pop() throws EmptyException+  public pop() throws Exception
 +                                                                                
   /**   /**
    * Pushes the given element onto this stack.    * Pushes the given element onto this stack.
Line 20: Line 18:
    * @param the element to be pushed onto this stack.    * @param the element to be pushed onto this stack.
    */    */
-  public void push(Integer element);+  public void push(element);
 } }
 </code> </code>
  
-In each implementation of the above interface we will exploit the class Node.+In each implementation of the above interface we will exploit the class Node. 
  
 <code java> <code java>
 /** /**
- * A node of a linked list.  Each node contains an integer.+ * A node of a linked list.
  */  */
-public class Node+public class Node<T>
 { {
-  private Integer element; +  private element; 
-  private Node next; +  private Node<T> next; 
 +  
   /**   /**
-   Creates a new node with the given element and next node.+   Initializes this node with the given element and next node.
    *    *
    * @param element the element stored in the node.    * @param element the element stored in the node.
Line 42: Line 40:
    * @param next the next node in the list.    * @param next the next node in the list.
    */    */
-  public Node(Integer element, Node next)+  public Node(element, Node<T> next)
   {   {
     this.element = element;     this.element = element;
     this.next = next;     this.next = next;
   }   }
 +  
   /**   /**
    * Returns the element of this node.    * Returns the element of this node.
Line 53: Line 51:
    * @return the element of this node.    * @return the element of this node.
    */    */
-  public Integer getElement()+  public getElement()
   {   {
     return this.element;     return this.element;
   }   }
 +  
   /**   /**
    * Returns the next node.    * Returns the next node.
Line 63: Line 61:
    * @return the next node.    * @return the next node.
    */    */
-  public Node getNext()+  public Node<T> getNext()
   {   {
     return this.next;     return this.next;
   }   }
 +  
   /**   /**
    * Sets the next node to the given node.    * Sets the next node to the given node.
Line 73: Line 71:
    * @param node the new next node.    * @param node the new next node.
    */    */
-  public void setNext(Node next)+  public void setNext(Node<T> next)
   {   {
     this.next = next;     this.next = next;
Line 80: Line 78:
 </code> </code>
  
-In the first implementation, we simply lock the whole stack when we pop or push an element.+In the first implementation, we simply lock the whole stack when we pop or push an element. 
  
 <code java> <code java>
-public class Stack1 implements Stack+public class First<T> implements Stack<T>
 { {
-  private Node top; +  private Node<T> top; 
- +                                                                                 
-  /** +  public First()
-   * Creates an empty stack. +
-   */ +
-  public Stack1()+
   {   {
     this.top = null;     this.top = null;
   }   }
- +   
-  public synchronized Integer pop() throws EmptyException+  public synchronized pop() throws Exception
   {   {
     if (this.top == null)     if (this.top == null)
     {     {
-      throw new EmptyException();+      throw new Exception();
     }     }
     else     else
     {     {
-      Integer element = this.top.getElement();+      element = this.top.getElement();
       this.top = this.top.getNext();       this.top = this.top.getNext();
       return element;       return element;
     }     }
   }   }
- +   
-  public synchronized void push(Integer element)+  public synchronized void push(element)
   {   {
-    this.top = new Node(element, this.top);+    this.top = new Node<T>(element, this.top);
   }   }
 } }
 </code> </code>
  
-In the second implementation, we lock the top node when we pop or push an element.+In the second implementation, we lock the top node when we pop or push an element. 
  
 <code java> <code java>
-public class Stack2 implements Stack+public class Second<T> implements Stack<T>
 { {
-  private Node top; +  private Node<T> top; 
- +   
-  /** +  public Second()
-   * Creates an empty stack. +
-   */ +
-  public Stack2()+
   {   {
-    this.top = new Node(null, null);+    this.top = null;
   }   }
- +                                                                                 
-  public Integer pop() throws EmptyException+  public pop() throws Exception
   {   {
 +    Node<T> node;
     synchronized (this.top)     synchronized (this.top)
     {     {
-      if (this.top.getNext() == null)+      if (this.top == null)
       {       {
-        throw new EmptyException();+        throw new Exception();
       }       }
       else       else
       {       {
-        Integer element = this.top.getElement();+        node = this.top;
         this.top = this.top.getNext();         this.top = this.top.getNext();
-        return element; 
       }       }
     }     }
 +    return node.getElement();
   }   }
- +                                                                                 
-  public void push(Integer element)+  public void push(element)
   {   {
-    Node node = new Node(element, null);+    Node<T> node = new Node<T>(element, null);
     synchronized (this.top)     synchronized (this.top)
     {     {
Line 160: Line 153:
 </code> </code>
  
-The third implementation does not use any locking.  We exploit the class AtomicReference.+The third implementation does not use any locking. We exploit the class AtomicReference. 
  
 <code java> <code java>
 import java.util.concurrent.atomic.*; import java.util.concurrent.atomic.*;
- +   
-public class Stack3 implements Stack+public class Third<T> implements Stack<T>
 { {
-  private volatile AtomicReference<Node> top; +  private volatile AtomicReference<Node<T>> top; 
- +   
-  /** +  public Third()
-   * Creates an empty stack. +
-   */ +
-  public Stack3()+
   {   {
-    this.top = new AtomicReference<Node>();+    this.top = new AtomicReference<Node<T>>();
   }   }
- +   
-  public Integer pop() throws EmptyException+  public pop() throws Exception
   {   {
-    Node node;+    Node<T> node;
     do     do
     {     {
Line 185: Line 175:
       if (node == null)       if (node == null)
       {       {
-        throw new EmptyException();+        throw new Exception();
       }       }
     }     }
Line 191: Line 181:
     return node.getElement();     return node.getElement();
   }   }
- +   
-  public void push(Integer element)+  public void push(element)
   {   {
-    Node node = new Node(element, null);+    Node<T> node = new Node<T>(element, null);
     do     do
     {     {
Line 204: Line 194:
 </code> </code>
  
-Also the fourth implementation does not use any locking.  This time we exploit the class AtomicReferenceFieldUpdater.+Also the fourth implementation does not use any locking. This time we exploit the class AtomicReferenceFieldUpdater. 
  
 <code java> <code java>
 import java.util.concurrent.atomic.*; import java.util.concurrent.atomic.*;
- +   
-public class Stack4 implements Stack+public class Fourth<T> implements Stack<T>
 { {
-  private volatile Node top; +  private volatile Node<T> top; 
- +   
-  private static final AtomicReferenceFieldUpdater<Stack4, Node> topUpdater = AtomicReferenceFieldUpdater.newUpdater(Stack\ +  private static final AtomicReferenceFieldUpdater<Fourth, Node> topUpdater 
-4.class, Node.class, "top"); +    = AtomicReferenceFieldUpdater.newUpdater(Fourth.class, Node.class, "top"); 
- +   
-  /** +  public Fourth()
-   * Creates an empty stack. +
-   */ +
-  public Stack4()+
   {   {
     this.top = null;     this.top = null;
   }   }
- +   
-  public Integer pop() throws EmptyException+  public pop() throws Exception
   {   {
-    Node node;+    Node<T> node;
     do     do
     {     {
Line 232: Line 219:
       if (node == null)       if (node == null)
       {       {
-        throw new EmptyException();+        throw new Exception();
       }       }
-    } while (!Stack4.topUpdater.compareAndSet(this, node, node.getNext()));+    } 
 +    while (Fourth.topUpdater.compareAndSet(this, node, node.getNext()));
     return node.getElement();     return node.getElement();
   }   }
- +   
-  public void push(Integer element)+  public void push(element)
   {   {
-    Node node = new Node(element, null);+    Node<T> node = new Node<T>(element, null);
     do     do
     {     {
         node.setNext(this.top);         node.setNext(this.top);
     }     }
-    while (!Stack4.topUpdater.compareAndSet(this, node.getNext(), node));+    while (Fourth.topUpdater.compareAndSet(this, node.getNext(), node));
   }   }
 } }
 </code> </code>
  
stack.1193589836.txt.gz · Last modified: 2009/04/26 19:37 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki