|
| 1 | +/* |
| 2 | + Task : Singly Linked List |
| 3 | + Author : Phumipat C. |
| 4 | + Language : C++ |
| 5 | + Explanation : |
| 6 | +*/ |
| 7 | +#include <iostream> |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +struct Node{ |
| 11 | + int data; |
| 12 | + Node *next; |
| 13 | + Node(int val){ |
| 14 | + data = val; |
| 15 | + next = NULL; |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +Node *create_node(int val){ |
| 20 | + Node *new_node = new Node(val); |
| 21 | + return new_node; |
| 22 | +} |
| 23 | + |
| 24 | +void insert_node_after(Node *prev_node, int val){ |
| 25 | + if(prev_node == NULL){ |
| 26 | + cout << "The given previous node cannot be NULL"; |
| 27 | + return ; |
| 28 | + } |
| 29 | + Node *new_node = create_node(val); |
| 30 | + new_node->next = prev_node->next; |
| 31 | + prev_node->next = new_node; |
| 32 | +} |
| 33 | + |
| 34 | +Node *move_backward(Node *node_to_move, Node *head){ |
| 35 | + if(node_to_move == NULL){ |
| 36 | + cout << "The given node cannot be NULL"; |
| 37 | + return node_to_move; |
| 38 | + } |
| 39 | + |
| 40 | + Node *current = head; |
| 41 | + while(current != NULL && current->next != node_to_move){ |
| 42 | + current = current->next; |
| 43 | + } |
| 44 | + return current; |
| 45 | +} |
| 46 | + |
| 47 | +Node *move_forward(Node *node_to_move){ |
| 48 | + if(node_to_move == NULL || node_to_move->next == NULL){ |
| 49 | + cout << "The given node cannot be NULL or the last node"; |
| 50 | + return node_to_move; |
| 51 | + } |
| 52 | + |
| 53 | + Node *next_node = node_to_move->next; |
| 54 | + return next_node; |
| 55 | +} |
| 56 | + |
| 57 | +Node *move_to_front(Node *node_to_move, Node *head){ |
| 58 | + if(node_to_move == NULL){ |
| 59 | + cout << "The given node cannot be NULL"; |
| 60 | + return node_to_move; |
| 61 | + } |
| 62 | + |
| 63 | + return node_to_move = head; |
| 64 | +} |
| 65 | + |
| 66 | +Node *move_to_end(Node *node_to_move){ |
| 67 | + if(node_to_move == NULL){ |
| 68 | + cout << "The given node cannot be NULL"; |
| 69 | + return node_to_move; |
| 70 | + } |
| 71 | + |
| 72 | + while(node_to_move->next != NULL){ |
| 73 | + node_to_move = move_forward(node_to_move); |
| 74 | + } |
| 75 | + return node_to_move; |
| 76 | +} |
| 77 | + |
| 78 | +void PrintCurrentNode(Node *current){ |
| 79 | + if(current == NULL){ |
| 80 | + cout << "The given node cannot be NULL"; |
| 81 | + return ; |
| 82 | + } |
| 83 | + cout << current->data << "\n"; |
| 84 | +} |
| 85 | + |
| 86 | +int main() |
| 87 | +{ |
| 88 | + // Linked list: 1 |
| 89 | + Node *head = create_node(1); |
| 90 | + Node *current = head; |
| 91 | + |
| 92 | + // Linked list: 1 2 |
| 93 | + insert_node_after(current, 2); |
| 94 | + |
| 95 | + // current pointing to 2 |
| 96 | + current = move_forward(current); |
| 97 | + |
| 98 | + // Linked list: 1 2 3 |
| 99 | + insert_node_after(current, 3); |
| 100 | + |
| 101 | + // current pointing to 1 |
| 102 | + current = move_to_front(current, head); |
| 103 | + |
| 104 | + // Linked list: 1 4 2 3 |
| 105 | + insert_node_after(current, 4); |
| 106 | + |
| 107 | + // current pointing to 3 |
| 108 | + current = move_to_end(current); |
| 109 | + |
| 110 | + // Linked list: 1 4 2 3 5 |
| 111 | + insert_node_after(current, 5); |
| 112 | + |
| 113 | + return 0; |
| 114 | +} |
0 commit comments