Created
May 26, 2017 18:04
-
-
Save Zekt/d3278717b179af284d07332096c21aff to your computer and use it in GitHub Desktop.
資訊之芽 2017 170 Doubly linked list
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
struct node { | |
int data; | |
node *prev, *next; //分別指向前一格與後一格 | |
}; | |
void insert(node *prev, int data) { | |
node* newNode = new node; | |
newNode->data = data; | |
newNode->prev = prev; | |
newNode->next = prev->next; | |
if(prev->next) | |
prev->next->prev = newNode; | |
prev->next = newNode; | |
} | |
void remove(node* ptr) { | |
node* next = ptr->next; | |
node* prev = ptr->prev; | |
if(next) | |
next->prev = prev; | |
if(prev) | |
prev->next = next; | |
delete ptr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment