3.2 Doubly linked list

Source: Internet
Author: User

1. IntroductionThe previous 3.1 single-linked list in the operation process has a disadvantage, is that the latter node can not directly find the front node, which makes a lot of operations have to search for nodes from beginning to end, the algorithm efficiency becomes very low, the way to solve this problem is to redefine the node of the list of nodes so that each node has two pointers, one point to the precursor to the linked list.
Node definition
  
 
  1. template<class T>
  2. class DLLNode {
  3. public:
  4. DLLNode() {
  5. next = prev = 0;
  6. }
  7. DLLNode(const T& el, DLLNode<T> *n = 0, DLLNode<T> *p = 0) {
  8. info = el; next = n; prev = p;
  9. }
  10. T info;
  11. DLLNode<T> *next, *prev;
  12. };

The limitations of the methods defined in section 3.1 are that linked lists can only store integers. If you want the list to store floating point numbers or arrays. You have to re-write a similar set of code. However, it is better to declare this class only once and not to determine in advance what type of data it is stored in, and in C + + it is easy to do so with a template. To illustrate the use of templates in linked list processing, the template is used to define the linked list from this section, but examples of list operations still use linked lists that store integers.
2. Operation of a doubly linked listthe Doublylinkedlist class defines the operation of a doubly linked list
  
 
  1. template<class T>
  2. class DoublyLinkedList {
  3. public:
  4. DoublyLinkedList() {
  5. head = tail = 0;
  6. }
  7. void addToDLLTail(const T&);//Insert a node to the end of a doubly linked list
  8. T deleteFromDLLTail();//Delete the node at the end and return its value

  9. ~DoublyLinkedList() {
  10. clear();
  11. }
  12. bool isEmpty() const {
  13. return head == 0;
  14. }
  15. void clear();
  16. void setToNull() {
  17. head = tail = 0;
  18. }
  19. void addToDLLHead(const T&);
  20. T deleteFromDLLHead();
  21. T& firstEl();
  22. T* find(const T&) const;
  23. protected:
  24. DLLNode<T> *head, *tail;
  25. friend ostream& operator<<(ostream& out, const DoublyLinkedList<T>& dll) {
  26. for (DLLNode<T> *tmp = dll.head; tmp != 0; tmp = tmp->next)
  27. out << tmp->info << ‘ ‘;
  28. return out;
  29. }
  30. };

here are just two ways to do this: insert a node to the end of a doubly linked list and delete the node at the end, and the rest of the method readers can refer to the code themselves. (1)Insert a node to the end of a doubly linked list

The following steps are specifically divided into:
    • Create a new node and initialize three data members (info initialized to el,next initialized to null)
    • Set the value of prev to tail
    • Point tail to the newly joined node
    • The next point of the predecessor node points to the newly added node
  
 
  1. template<class T>
  2. void DoublyLinkedList<T>::addToDLLTail(const T& el) {
  3. if (tail != 0) {
  4. tail = new DLLNode<T>(el,0,tail);
  5. tail->prev->next = tail;
  6. }
  7. else head = tail = new DLLNode<T>(el);
  8. }
(2)Delete the node at the end

It is simpler to delete a node at the end of a doubly linked list because it does not require a loop to find the precursor node for the node to be deleted, tail points to the predecessor of the node to be deleted, and then sets the next node to be deleted to null. However, if the list to be operated on is an empty list, it may cause the program to crash, so the user should check whether the linked list is empty before deleting the end node, and check if the linked list is empty and the code is also provided.
 
   
  
  1. if (!list.isEmpty())
  2. n = list.deleteFromDLLTail();
  3. else do not delete;
Another special case is that the list has only one node, and the head and tail are set to null.
Because the last node can be accessed directly, the time complexity of executing these two methods is O (1). Full code See: http://www.oschina.net/code/snippet_588162_48663

From for notes (Wiz)

List of attachments

    3.2 Doubly linked list

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.