Created
August 23, 2016 03:13
-
-
Save oskimura/5de5c63400d512f8d6fbbb17ba8a5166 to your computer and use it in GitHub Desktop.
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
| #include <stdio.h> | |
| template <typename T> | |
| class list | |
| { | |
| struct node { | |
| node *next; | |
| node *prev; | |
| T val; | |
| }; | |
| node* head; | |
| public: | |
| list() | |
| { | |
| head = NULL; | |
| }; | |
| void insert(T e) | |
| { | |
| node* n = new node(); | |
| n->val = e; | |
| n->next = head; | |
| if (head!=NULL) { | |
| head->prev = n; | |
| } | |
| n->prev=NULL; | |
| head = n; | |
| }; | |
| void del(T e) | |
| { | |
| node *x = head; | |
| while (x) { | |
| if (e == x->val) { | |
| if (x->next!=NULL) { | |
| x->next->prev = x->prev; | |
| } | |
| if (x->prev!=NULL) { | |
| x->prev->next = x->next; | |
| } | |
| else { | |
| head = x->next; | |
| } | |
| } | |
| x = x->next; | |
| } | |
| } | |
| node* find(T e) | |
| { | |
| node* x = head; | |
| while (x) { | |
| if (e==x->val) { | |
| return x; | |
| } | |
| x = x->next; | |
| } | |
| return NULL; | |
| } | |
| void print() | |
| { | |
| node *x = head; | |
| while(x) { | |
| printf("%d",x->val); | |
| x = x->next; | |
| } | |
| } | |
| }; | |
| int main() { | |
| list<char> l; | |
| l.insert('a'); | |
| l.print(); | |
| l.del('a'); | |
| l.print(); | |
| l.insert('a'); | |
| l.print(); | |
| l.insert('b'); | |
| l.print(); | |
| l.del('a'); | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment