-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Doubly linked list #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.