| 12 | }; |
| 13 | |
| 14 | class SLL { |
| 15 | private: |
| 16 | Node *head = NULL; |
| 17 | |
| 18 | public: |
| 19 | void append(int val) { |
| 20 | Node *c = head; |
| 21 | Node *newnode = new Node(val); |
| 22 | Node *previous; |
| 23 | |
| 24 | if (!c) { |
| 25 | head = newnode; |
| 26 | } else { |
| 27 | while (true) { |
| 28 | if (c == NULL) { |
| 29 | break; |
| 30 | } |
| 31 | previous = c; |
| 32 | c = c->next; |
| 33 | } |
| 34 | previous->next = newnode; |
| 35 | } |
| 36 | delete c; |
| 37 | } |
| 38 | |
| 39 | int length() { |
| 40 | Node *c = head; |
| 41 | int count = 0; |
| 42 | while (true) { |
| 43 | if (c == NULL) { |
| 44 | break; |
| 45 | } |
| 46 | c = c->next; |
| 47 | count++; |
| 48 | } |
| 49 | delete c; |
| 50 | return count; |
| 51 | } |
| 52 | |
| 53 | Node getHead() { |
| 54 | return *head; |
| 55 | } |
| 56 | |
| 57 | Node getNode(int index) { |
| 58 | if (index > length()) { |
| 59 | throw "IndexError: index out of range"; |
| 60 | } |
| 61 | Node *c = head; |
| 62 | for (int i = 0; i < index; i++) { |
| 63 | c = c->next; |
| 64 | } |
| 65 | return *c; |
| 66 | } |
| 67 | |
| 68 | void delNode(int index) { |
| 69 | if (index > length()) { |
| 70 | throw "IndexError: index out of range"; |
| 71 | } else if (!index) { |