Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions Data_Structure/Doubly_Linked_List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
Task : Doubly Linked List
Author : Phumipat C.
Language : C++
Explanation :
*/
#include <iostream>

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;
Comment thread
phumipatc marked this conversation as resolved.
}

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;
}