Advertisement
Guest User

Untitled

a guest
Aug 26th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.91 KB | None | 0 0
  1. import std.stdio;
  2.  
  3. class Stack(T) {
  4.   private T[] data;
  5.   private int size;
  6.  
  7.   public this() {
  8.     data = new T[16];
  9.     size = 0;
  10.   }
  11.  
  12.   public auto push(T thing) {
  13.     if (size == data.length) {
  14.       data.length *= 2;
  15.     }
  16.     data[size] = thing;
  17.     size++;
  18.   }
  19.  
  20.   public ref T top() {
  21.     return data[size-1];
  22.   }
  23.  
  24.   public auto pop() {
  25.     size--;
  26.     if (size < data.length/4) {
  27.       data.length /= 2;
  28.     }
  29.   }
  30. }
  31.  
  32. class Queue(T) {
  33.   private Stack!T stack1;
  34.   private Stack!T stack2;
  35.  
  36.   public this() {
  37.     stack1 = new Stack!T();
  38.     stack2 = new Stack!T();
  39.   }
  40.  
  41.   public auto enqueue(T thing) {
  42.  
  43.   }
  44.  
  45.   public ref T front() {
  46.  
  47.   }
  48.  
  49.   public ref T back() {
  50.  
  51.   }
  52.  
  53.   public auto dequeue() {
  54.  
  55.   }
  56. }
  57.  
  58. void main() {
  59.     Stack!int thingy = new Stack!int;
  60.   foreach(x; 1..100) {
  61.     thingy.push(x);
  62.   }
  63.   writeln(thingy.top);
  64.   thingy.pop();
  65.   writeln(thingy.top);
  66.  
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement