⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dlist.h

📁 采用c++实现的文本编辑器 采用了数据结构中的双链表实现
💻 H
字号:
// This is the file to include in your code if you want access to the
// complete DList template class

// First, get the declaration for the base list class
#include "list.h"

// Linked list implementation
template <class Elem> class DList: public List<Elem> {
private:
  DLink<Elem>* head;       // Pointer to list header
  DLink<Elem>* tail;       // Pointer to last Elem in list 
  DLink<Elem>* fence;      // Last element on left side
  int leftcnt;            // Size of left partition
  int rightcnt;           // Size of right partition
  void init() {           // Intialization routine
    fence = tail = head = new DLink<Elem>;
    leftcnt = rightcnt = 0;
  }
  void removeall() {   // Return link nodes to free store
    while(head != NULL) {
      fence = head;
      head = head->next;
      delete fence;    }
  }
public:
  DList(int size=DefaultListSize) { init(); }

  ~DList() { removeall(); }  // Destructor
  void clear() { removeall(); init();}
  bool insert(const Elem&);
  bool append(const Elem&);
  bool remove(Elem&);
  void setStart()
    { fence = head; rightcnt += leftcnt; leftcnt = 0; }
  void setEnd()
    { fence = tail; leftcnt += rightcnt; rightcnt = 0; }
  void prev();
  void next() {
    if (fence != tail) // Don't move fence if right empty
      { fence = fence->next; rightcnt--; leftcnt++; }
  }
  int leftLength() const  { return leftcnt; }
  int rightLength() const { return rightcnt; }
  bool setPos(int pos);
  bool getValue(Elem& it) const {
    if(rightLength() == 0) return false;
    it = fence->next->element;
    return true;
  }
  void print() const;
};

// First are the functions not implemented in dlist.h that
// are different from the singly linked list version.
template <class Elem> // Insert at front of right partition
bool DList<Elem>::insert(const Elem& item) {
  fence->next = new DLink<Elem>(item, fence, fence->next);  
  if (fence->next->next != NULL) // If not deleting at end
    fence->next->next->prev = fence->next;
  if (tail == fence)             // Appending new Elem
    tail = fence->next;          //   so set tail
  rightcnt++;                    // Added to right
  return true;
}

template <class Elem> // Append Elem to end of the list.
bool DList<Elem>::append(const Elem& item) {
  tail = tail->next = new DLink<Elem>(item, tail, NULL);
  rightcnt++;                    // Added to right
  return true;
}

// Remove and return first Elem in right partition
template <class Elem> bool DList<Elem>::remove(Elem& it) {
  if (fence->next == NULL) return false; // Empty right
  it = fence->next->element;       // Remember value
  DLink<Elem>* ltemp = fence->next; // Remember link node
  if (ltemp->next != NULL) 
	  ltemp->next->prev = fence;
  else 
	  tail = fence;               // Reset tail
  fence->next = ltemp->next;       // Remove from list
  delete ltemp;                    // Reclaim space
  rightcnt--;                      // Removed from right
  return true;
}

// Move fence one step left; no change if left is empty
template <class Elem> void DList<Elem>::prev() {
  if (fence != head)  // Can't back up from list head
    { fence = fence->prev; leftcnt--; rightcnt++;}
}

// Here are the functions that are identical to their singly linked 
// list counterparts.
// Set the size of left partition to pos
template <class Elem> bool DList<Elem>::setPos(int pos) {
  if ((pos < 0) || (pos > rightcnt+leftcnt)) return false;
  fence = head;
  for(int i=0; i<pos; i++) fence = fence->next;
  rightcnt=rightcnt+leftcnt-pos;
  leftcnt=pos;
  return true;
}

template <class Elem> void DList<Elem>::print() const {
  DLink<Elem>* temp = head;
  cout << "< ";
  while (temp != fence) {
    cout << temp->next->element << " ";
    temp = temp->next;
  }
  cout << "| ";
  while (temp->next != NULL) {
    cout << temp->next->element << " ";
    temp = temp->next;
  }
  cout << ">\n";
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -