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