Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.conv;
- class LinkedList
- {
- private:
- struct Node
- {
- int val;
- Node* next = null;
- this(int newVal)
- {
- val = newVal;
- }
- }
- Node* root = null;
- public:
- void append(int newVal)
- {
- if (root is null)
- {
- root = new Node(newVal);
- return;
- }
- auto curr = root;
- while (curr.next !is null)
- {
- curr = curr.next;
- }
- curr.next = new Node(newVal);
- }
- override string toString()
- {
- string s;
- auto curr = root;
- while (curr !is null)
- {
- writeln(s);
- s ~= to!string(curr.val) ~ " ";
- curr = curr.next;
- }
- return s;
- }
- }
- void main()
- {
- LinkedList l;
- for (int i = 0; i < 10; ++i)
- l.append(i);
- writeln(l);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement