#singly_linked_list
Explore tagged Tumblr posts
nhanthanh · 4 years ago
Text
Linked list
Là cấu trúc dữ liệu gồm dãy các node kết nối với nhau thông qua các link (liên kết). 
Mỗi node bao gồm data và address của node kế tiếp.
struct node 
     int data; 
     struct node *next; 
};
Một linked-list (singly-linked list) cơ bản sẽ giống như sau:
Tumblr media
class linked_list 
     private: 
          node *head,*tail; 
     public: 
     linked_list()
     { 
          head = NULL; 
          tail = NULL; 
     } 
};
Một linked-list sẽ bắt đầu từ node head và kết thúc ở node tail.
Ta tạo thêm function để thêm node vào linked-list:
void add_node(int n) 
     node *tmp = new node; 
     tmp->data = n; 
     tmp->next = NULL; 
     if(head == NULL) 
     { 
          head = tmp; 
          tail = tmp; 
     } 
else 
     { 
          tail->next = tmp; 
          tail = tail->next; 
     } 
}
0 notes