Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct Node {
  7.     int data;
  8.     Node *next;
  9.     Node *prev;
  10. };
  11.  
  12. typedef struct DLinkedList *List;
  13. struct DLinkedList {
  14.     Node *first;
  15. };
  16.  
  17. void AppendEnd( int v );
  18. bool Search( int value );
  19. void Delete( int value );
  20. List list;
  21.  
  22. // test
  23. int main()
  24. {
  25.     AppendEnd(4);
  26.     AppendEnd(6);
  27.     if(Search(4)) cout << "true";
  28.     else cout << "false";
  29.     if(Search(6)) cout << "\ttrue\n";
  30.     Delete(6);
  31.     if(Search(6)) cout << "true";
  32.     return 0;
  33. }
  34.  
  35.  
  36.  
  37. // Gan gia tri value vao cuoi cua linked-list
  38. void AppendEnd( int value ) {
  39.  
  40.     Node *node = new Node;
  41.     node->data = value;
  42.     node->next = NULL;
  43.     node->prev = NULL;
  44.    
  45.     if( list == NULL ) {
  46.         list = new DLinkedList;
  47.         list->first = node;
  48.     }
  49.     else {
  50.         Node *curr = list->first;
  51.         while( curr->next != NULL )
  52.             curr = curr->next;
  53.  
  54.         curr->next = node;
  55.         node->prev = curr;
  56.     }
  57. }
  58.  
  59.  
  60. // tim xem trong linked list co gia tri value hay ko
  61. bool Search( int value ) {
  62.  
  63.     Node *curr = list->first;
  64.    
  65.     while( curr != NULL ) {
  66.         if( curr->data == value )
  67.             return true;
  68.         curr = curr->next;
  69.     }    
  70.     return false;
  71. }
  72.  
  73.  
  74. // Xoa element dau tien co gia tri value trong list
  75. void Delete( int value ) {
  76.  
  77.     Node *curr = list->first;
  78.     while( curr != NULL ) {
  79.         if( curr->data == value ) {
  80.             curr->next->prev = curr->prev;
  81.             curr->prev->next = curr->next;
  82.             delete curr;
  83.             return;
  84.         }
  85.         curr = curr->next;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement