Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.72 KB | None | 0 0
  1. import std.stdio;
  2. import std.conv;
  3.  
  4. class LinkedList
  5. {
  6. private:
  7.     struct Node
  8.     {
  9.         int val;
  10.         Node* next = null;
  11.  
  12.         this(int newVal)
  13.         {
  14.             val = newVal;
  15.         }
  16.     }
  17.  
  18.     Node* root = null;
  19.  
  20. public:
  21.     void append(int newVal)
  22.     {
  23.         if (root is null)
  24.         {
  25.             root = new Node(newVal);
  26.             return;
  27.         }
  28.  
  29.         auto curr = root;
  30.         while (curr.next !is null)
  31.         {
  32.             curr = curr.next;
  33.         }
  34.         curr.next = new Node(newVal);
  35.     }
  36.  
  37.     override string toString()
  38.     {
  39.         string s;
  40.         auto curr = root;
  41.         while (curr !is null)
  42.         {
  43.             writeln(s);
  44.             s ~= to!string(curr.val) ~ " ";
  45.             curr = curr.next;
  46.         }
  47.         return s;
  48.     }
  49. }
  50.  
  51. void main()
  52. {
  53.     LinkedList l;
  54.  
  55.     for (int i = 0; i < 10; ++i)
  56.         l.append(i);
  57.  
  58.     writeln(l);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement