《数据结构与算法分析:C语言描述》复习——第三章“线性表、栈和队列”——双向链表

2014.06.14 20:17

简介:

  双向链表是LRU Cache中要用到的基本结构,每个链表节点左右分别指向上一个和下一个节点,能够自由地左右遍历。

图示:

  《数据结构与算法分析:C语言描述》复习——第三章“线性表、栈和队列”——双向链表_第1张图片

实现:

  1 // My implementation for doubly linked list.
  2 struct ListNode {
  3     int val;
  4     ListNode *prev;
  5     ListNode *next;
  6     ListNode(int _val = 0): val(_val), next(nullptr), prev(nullptr) {};
  7 };
  8 
  9 class DoublyLinkedList {
 10 public:
 11     DoublyLinkedList() {
 12         m_size = 0;
 13         m_head = nullptr;
 14         m_tail = nullptr;
 15     }
 16     
 17     void insertFront(int val) {
 18         if (m_size == 0) {
 19             m_head = m_tail = new ListNode(val);
 20         } else {
 21             ListNode *ptr = new ListNode(val);
 22             ptr->next = m_head;
 23             m_head->prev = ptr;
 24             m_head = ptr;
 25         }
 26         ++m_size;
 27     }
 28     
 29     void insertBack(int val) {
 30         if (m_size == 0) {
 31             m_head = m_tail = new ListNode(val);
 32         } else {
 33             m_tail->next = new ListNode(val);
 34             m_tail->next->prev = m_tail;
 35             m_tail = m_tail->next;
 36         }
 37         ++m_size;
 38     }
 39     
 40     void insertNode(int pos, int val) {
 41         int i;
 42         
 43         if (i <= 0) {
 44             insertFront(val);
 45         } else if (i >= m_size) {
 46             insertBack(val);
 47         } else {
 48             ListNode *ptr1, *ptr2;
 49             
 50             ptr1 = m_head;
 51             for (i = 0; i < pos - 1; ++i) {
 52                 ptr1 = ptr1->next;
 53             }
 54             ptr2 = new ListNode(val);
 55             ptr2->next = ptr1->next;
 56             ptr1->next->prev = ptr2;
 57             ptr1->next = ptr2;
 58             ptr2->prev = ptr1;
 59             ++m_size;
 60         }
 61     }
 62     
 63     void deleteNode(int pos) {
 64         if (pos < 0 || pos > m_size - 1) {
 65             return;
 66         }
 67         
 68         ListNode *ptr1, *ptr2;
 69         if (pos == 0) {
 70             ptr1 = m_head;
 71             if (m_size == 1) {
 72                 m_head = m_tail = nullptr;
 73             } else {
 74                 m_head = m_head->next;
 75                 m_head->prev = nullptr;
 76             }
 77             delete ptr1;
 78         } else {
 79             ptr1 = m_head;
 80             for (int i = 0; i < pos - 1; ++i) {
 81                 ptr1 = ptr1->next;
 82             }
 83             ptr2 = ptr1->next;
 84             ptr1->next = ptr2->next;
 85             if (ptr2->next == nullptr) {
 86                 m_tail = ptr1;
 87             } else {
 88                 ptr2->next->prev = ptr1;
 89             }
 90             delete ptr2;
 91         }
 92         --m_size;
 93     }
 94     
 95     void updateNode(int pos, int val) {
 96         if (pos < 0 || pos > m_size - 1) {
 97             return;
 98         }
 99         
100         ListNode *ptr = m_head;
101         for (int i = 0; i < pos; ++i) {
102             ptr = ptr->next;
103         }
104         ptr->val = val;
105     }
106     
107     ListNode *findNode(int val) {
108         ListNode *ptr = m_head;
109         while (ptr != nullptr) {
110             if (ptr->val == val) {
111                 return ptr;
112             }
113             ptr = ptr->next;
114         }
115         
116         return nullptr;
117     }
118     
119     ~DoublyLinkedList() {
120         ListNode *ptr = m_head;
121         while (m_head != nullptr) {
122             m_head = m_head->next;
123             delete ptr;
124             ptr = m_head;
125         }
126         m_head = m_tail = nullptr;
127     }
128 private:
129     int m_size;
130     ListNode *m_head;
131     ListNode *m_tail;
132 };
133 
134 int main()
135 {
136     return 0;
137 }

 

你可能感兴趣的:(数据结构与算法)