Chinaunix首页 | 论坛 | 博客
  • 博客访问: 525248
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2009-09-16 18:34:12

有错误的话,请帮忙指正:
DoubleNode.h

#ifndef DOUBLENODE_H
#define DOUBLENODE_H

template<class T>
class DoubleNode {
 public:
  T data;
  DoubleNode<T>* next;
  DoubleNode<T>* pre;
  DoubleNode() {
    next = NULL;
    pre = NULL;
  }
  DoubleNode(const T& value, DoubleNode<T>* nxt = NULL,
     DoubleNode<T>* pre = NULL)
    : data(value), next(nxt), pre(pre) {}
};
#endif


Double.h

#ifndef DOUBLE_H
#define DOUBLE_H
#include <iostream>
#include <exception>
#include <stdexcept>
#include <cstddef>
#include <cstdlib>
#include "DoubleNode.h"


template<class T>
class Double {
  DoubleNode<T>* head;
  DoubleNode<T>* tail;
  int size;
 public:
  Double();
  ~Double();
  T& front();
  T& back();
  void push_front(const T&);
  void pop_front();
  void push_back(const T&);
  void pop_back();
  void insert(int , const T&);
  T remove(int k);
  DoubleNode<T>* find(const T&);
  int getSize();
  void erase();
  void printAll();
};

template<class T>
Double<T>::Double() {
  head = tail = NULL;
  size = 0;
}

template<class T>
Double<T>::~Double() {
  erase();
}

template<class T>
T& Double<T>::front() {
  if(size == 0)
    throw std::logic_error("List is empty!");
  else if(size == 1) {
    return head->data;
  }
  else {
    return head->data;
  }
}

template<class T>
T& Double<T>::back() {
  if(size == 0)
    throw std::logic_error("List is error!");
  else if(size == 1)
    return tail->data;
  else {
    return tail->data;
  }
}

template<class T>
void Double<T>::push_front(const T& value) {
  DoubleNode<T>* node = new DoubleNode<T>(value, 0, 0);
  if(size == 0) {
    head = tail = node;
    size++;
  }
  else if(size == 1) {
    node->next = head;
    head->pre = node;
    head = node;
    size++;
  }
  else {
    node->next = head;
    head->pre = node;
    head = node;
    size++;
  }
}

template<class T>
void Double<T>::pop_front() {
  if(size == 0)
    throw std::logic_error("Can't pop from a empty list!");
  else if(size == 1) {
    delete head;
    head = tail = 0;
    size--;
  }
  else {
    DoubleNode<T>* node = head->next;
    node->pre = 0;
    delete head;
    head = node;
    size--;
  }
}

template<class T>
void Double<T>::push_back(const T& value) {
  DoubleNode<T>* node = new DoubleNode<T>(value, 0, 0);
  if(size == 0) {
    head = tail = node;
    size++;
  }
  else if(size == 1) {
    node->pre = tail;
    tail->next = node;
    tail = node;
    size++;
  }
  else {
    node->pre = tail;
    tail->next = node;
    tail = node;
    size++;
  }
}

template<class T>
void Double<T>::pop_back() {

  if(size == 0)
    throw std::logic_error("Can't pop from a empty list!");
  else if(size == 1) {
    delete head;
    head = tail = 0;
    size--;
  }
  else {
  DoubleNode<T>* node = tail->pre;
  node->next = 0;
  delete tail;
  tail = node;
  size--;
  }
}

template<class T>
void Double<T>::insert(int k, const T& value) {
  DoubleNode<T>* node = new DoubleNode<T>(value, 0, 0);
  if(size == 0) {
    if(k != 0)
      return;
    else {
      head = tail = node;
      size++;
    }
  }
  else if(size == 1) {
    if(k == 0) {
      node->next = head;
      head->pre = node;
      head = node;
      size++;
    }
    else if(k == 1) {
      node->pre = tail;
      tail->next = node;
      tail = node;
      size++;
    }
    else {
      return;
    }
  }
  else {
      if(k == 0) {
    node->next = head;
    head->pre = node;
    head = node;
    size++;
      }
      else if(k == size) {
    node->pre = tail;
    tail->next = node;
    tail = node;
    size++;
      }
      else if(k > 0 && k < size) {
    DoubleNode<T>* p = head;
    int iter = 0;
    while(iter < k) {
     p = p->next;
     iter++;
    }
    DoubleNode<T>* ppre = p->pre;
    node->next = p;
    node->pre = ppre;
    p->pre = node;
    ppre->next = node;
    size++;
      }
      else {
    throw std::logic_error("You can't insert elements at place"
            "which is out of range!");
      }
  }
}

template<class T>
T Double<T>::remove(int k) {
  if(size == 0)
    throw std::logic_error("You can't remove an element"
         "from an empty list!");
  else if(size == 1) {
      if(k != 0)
    throw std::logic_error("You can only remove the head!");
      else {
    delete head;
    head = tail = 0;
    size--;
      }
    }
  else {
      if(k == 0) {
    DoubleNode<T>* node = head->next;
    node->pre = 0;
    delete head;
    head = node;
    size--;
      }
      else if(k == size - 1) {
    DoubleNode<T>* node = tail->pre;
    tail->next = 0;
    delete tail;
    tail = node;
    size--;
      }
      else if(k > 0 && k < size - 1) {
    DoubleNode<T>* node = head;
    int iter = 0;
    while(iter < k) {
     node = node->next;
     iter++;
    }
    DoubleNode<T>* pre = node->pre;
    DoubleNode<T>* nxt = node->next;
    pre->next = nxt;
    nxt->pre = pre;
    delete node;
    size--;
      }
      else {
    throw std::logic_error("You can't remove an elements that"
             "never exists");
      }
  }
}

template<class T>
DoubleNode<T>* Double<T>::find(const T& value) {
  if(size == 0) {
    return 0;
  }
  else if(size == 1) {
    if(head->data == value)
      return head;
    else
      return 0;
  }
  else {
    DoubleNode<T>* node = head;
    while(node != 0 && node-> data != value)
      node = node->next;
    if(node == 0)
      return 0;
    else
      return node;
  }
}

template<class T>
int Double<T>::getSize() {
  return size;
}

template<class T>
void Double<T>::erase() {
  if(size == 0)
    return;
  else if(size == 1) {
    delete head;
    head = tail = 0;
    size = 0;
  }
  else {
    DoubleNode<T>* node = head;
    while(node != 0) {
      node = node->next;
      delete node;
    }
    size = 0;
  }
}

template<class T>
void Double<T>::printAll() {
  DoubleNode<T>* iter = head;
  while(iter != 0) {
    std::cout << iter->data << ' ';
    iter = iter->next;
  }
}
#endif

阅读(851) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~