From 081bb07c09f770ef549471a67cac038d59d94527 Mon Sep 17 00:00:00 2001 From: Phumipat Chaiprasertsud Date: Wed, 1 Oct 2025 21:14:37 +0700 Subject: [PATCH 1/2] feat: Doubly linked list --- Data_Structure/Doubly_Linked_List.cpp | 125 ++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 Data_Structure/Doubly_Linked_List.cpp diff --git a/Data_Structure/Doubly_Linked_List.cpp b/Data_Structure/Doubly_Linked_List.cpp new file mode 100644 index 0000000..a531c22 --- /dev/null +++ b/Data_Structure/Doubly_Linked_List.cpp @@ -0,0 +1,125 @@ +/* + Task : Doubly Linkedin List + Author : Phumipat C. + Language : C++ + Explanation : +*/ +#include +#define all(x) begin(x), end(x) +#define sz(x) (int)(x).size() +#define MOD (LL)(1e9 + 7) +using namespace std; +const int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}}; +const int dir8[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, 1, -1, 0, 1, -1}}; + +struct Node{ + int data; + Node *next; + Node *prev; + Node(int val){ + data = val; + next = NULL; + prev = NULL; + } +}; + +Node *create_node(int val){ + Node *new_node = new Node(val); + return new_node; +} + +void insert_node_after(Node *prev_node, int val){ + if(prev_node == NULL){ + cout << "The given previous node cannot be NULL"; + return ; + } + Node *new_node = create_node(val); + new_node->next = prev_node->next; + prev_node->next = new_node; + new_node->prev = prev_node; + if(new_node->next != NULL){ + new_node->next->prev = new_node; + } +} + +Node *move_backward(Node *node_to_move){ + if(node_to_move == NULL || node_to_move->prev == NULL){ + cout << "The given node cannot be NULL or the first node"; + return node_to_move; + } + + Node *prev_node = node_to_move->prev; + return prev_node; +} + +Node *move_forward(Node *node_to_move){ + if(node_to_move == NULL || node_to_move->next == NULL){ + cout << "The given node cannot be NULL or the last node"; + return node_to_move; + } + + Node *next_node = node_to_move->next; + return next_node; +} + +Node *move_to_front(Node *node_to_move){ + if(node_to_move == NULL){ + cout << "The given node cannot be NULL"; + return node_to_move; + } + + while(node_to_move->prev != NULL){ + node_to_move = move_backward(node_to_move); + } + return node_to_move; +} + +Node *move_to_end(Node *node_to_move){ + if(node_to_move == NULL){ + cout << "The given node cannot be NULL"; + return node_to_move; + } + + while(node_to_move->next != NULL){ + node_to_move = move_forward(node_to_move); + } + return node_to_move; +} + +void PrintCurrentNode(Node *current){ + if(current == NULL){ + cout << "The given node cannot be NULL"; + return ; + } + cout << current->data << "\n"; +} + +int main() +{ + // Linked list: 1 + Node *head = create_node(1); + Node *current = head; + + // Linked list: 1 2 + insert_node_after(head, 2); + + // current pointing to 2 + current = move_forward(head); + + // Linked list: 1 2 3 + insert_node_after(current, 3); + + // current pointing to 1 + current = move_to_front(current); + + // Linked list: 1 4 2 3 + insert_node_after(current, 4); + + // current pointing to 3 + current = move_to_end(current); + + // Linked list: 1 4 2 3 5 + insert_node_after(current, 5); + + return 0; +} From 900166c4de9fd1d25a78a26c2989ea9deec9b268 Mon Sep 17 00:00:00 2001 From: Phumipat Chaiprasertsud Date: Wed, 1 Oct 2025 21:19:08 +0700 Subject: [PATCH 2/2] fix: fix feedback --- Data_Structure/Doubly_Linked_List.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Data_Structure/Doubly_Linked_List.cpp b/Data_Structure/Doubly_Linked_List.cpp index a531c22..fd34538 100644 --- a/Data_Structure/Doubly_Linked_List.cpp +++ b/Data_Structure/Doubly_Linked_List.cpp @@ -1,16 +1,10 @@ /* - Task : Doubly Linkedin List + Task : Doubly Linked List Author : Phumipat C. Language : C++ Explanation : */ -#include -#define all(x) begin(x), end(x) -#define sz(x) (int)(x).size() -#define MOD (LL)(1e9 + 7) -using namespace std; -const int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}}; -const int dir8[2][8] = {{-1, -1, -1, 0, 1, 1, 1, 0}, {-1, 0, 1, 1, -1, 0, 1, -1}}; +#include struct Node{ int data;