免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: fibbery
打印 上一主题 下一主题

我需要一个使用stl库中的异常类的例子,谢谢! [复制链接]

论坛徽章:
0
1 [报告]
发表于 2008-08-11 15:35 |显示全部楼层

  1. #ifndef _LINKEDLIST_H_
  2. #define _LINKEDLIST_H_

  3. #include <stdexcept>

  4. using namespace std;

  5. class EmptyListException : public logic_error {
  6. public:

  7.     EmptyListException(const string& what_arg ) throw() :

  8.       logic_error ("Empty list exception: " + what_arg) {}
  9. };

  10. template <class T>
  11. class Node {
  12. private:
  13.     T data;
  14.     Node* next;

  15. public:
  16.     Node(T d, Node* n = NULL) : data(d), next(n) {}

  17.     T& getData() { return data;}

  18.     Node*& getNext() { return next;}
  19. };

  20. template <class T>
  21. class LinkedList {
  22. protected:
  23.     Node<T>* head; // Beginning of list

  24.     Node<T>* tail; // End of list

  25.     int count; // Number of nodes in list

  26. public:
  27.     LinkedList(void) : head(NULL), tail(NULL), count(0) {}

  28.     LinkedList(const LinkedList<T>& src); // Copy constructor

  29.     virtual ~LinkedList(void); // Destructor

  30.     virtual T& front(void) {
  31.         if (head == NULL) {
  32.             throw EmptyListException("front()");
  33.         }

  34.         return head->getData();
  35.     }

  36.     virtual T& back(void) {
  37.         if (tail == NULL) {
  38.             throw EmptyListException("back()");
  39.         }
  40.         return tail->getData();
  41.     }

  42.     virtual int size(void) {
  43.         return count;
  44.     }

  45.     virtual bool empty(void) {
  46.         return count == 0;
  47.     }

  48.     virtual void push_front(T); // Insert element at beginning

  49.     virtual void push_back(T); // Insert element at end

  50.     virtual void pop_front(void); // Remove element from beginning

  51.     virtual void pop_back(void); // Remove element from end

  52.     virtual void dump(void); // Output contents of list
  53. };

  54. // Copy constructor
  55. template <class T>
  56. LinkedList<T>::LinkedList(const LinkedList<T>& src) :
  57. count(0), head(NULL), tail(NULL) {
  58.     Node<T>* current = src.head;
  59.     while (current != NULL) {
  60.         this->push_back(current->getData());
  61.         current = current->getNext();
  62.     }
  63. }

  64. // Destructor
  65. template <class T>
  66. LinkedList<T>::~LinkedList(void) {
  67.     while (!this->empty()) {
  68.         this->pop_front();
  69.     }
  70. }

  71. // Insert an element at the beginning
  72. template <class T>
  73. void LinkedList<T>::push_front(T d) {

  74.     Node<T>* new_head = new Node<T>(d, head);

  75.     if (this->empty()) {
  76.         head = tail = new_head;
  77.     }
  78.     else {
  79.         head = new_head;
  80.     }

  81.     count++;
  82. }

  83. // Insert an element at the end
  84. template <class T>
  85. void LinkedList<T>::push_back(T d) {

  86.     Node<T>* new_tail = new Node<T>(d, NULL);

  87.     if (this->empty()) {
  88.         head = new_tail;
  89.     }
  90.     else {
  91.         tail->getNext() = new_tail;
  92.     }

  93.     tail = new_tail;
  94.     count++;
  95. }

  96. // Remove an element from the beginning
  97. template <class T>
  98. void LinkedList<T>::pop_front(void) {

  99.     if (head == NULL) {
  100.         throw EmptyListException("pop_front()");
  101.     }

  102.     Node<T>* old_head = head;

  103.     if (this->size() == 1) {
  104.         head = NULL;
  105.         tail = NULL;
  106.     }
  107.     else {
  108.         head = head->getNext();
  109.     }

  110.     delete old_head;
  111.     count--;
  112. }

  113. // Remove an element from the end
  114. template <class T>
  115. void LinkedList<T>::pop_back(void) {
  116.     if (tail == NULL) {
  117.         throw EmptyListException("pop_back()");
  118.     }

  119.     Node<T>* old_tail = tail;

  120.     if (this->size() == 1) {
  121.         head = NULL;
  122.         tail = NULL;
  123.     }

  124.     else {
  125.         // Traverse the list
  126.         Node<T>* current = head;
  127.         while (current->getNext() != tail) {
  128.             current = current->getNext();
  129.         }

  130.         // Unlink and reposition
  131.         current->getNext() = NULL;
  132.         tail = current;
  133.     }

  134.     delete old_tail;
  135.     count--;
  136. }

  137. // Display the contents of the list
  138. template <class T>
  139. void LinkedList<T>::dump(void) {
  140.     cout << "(";

  141.     if (head != NULL) {
  142.         Node<T>* current = head;
  143.         while (current->getNext() != NULL) {
  144.             cout << current->getData() << ", ";
  145.             current = current->getNext();
  146.         }
  147.         cout << current->getData();
  148.     }

  149.     cout << ")" << endl;
  150. }
  151. #endif



  152. #ifndef _ENHANCELINKLIST_H_
  153. #define _ENHANCELINKLIST_H_

  154. #include "LinkedList.h"

  155. template<typename T>
  156. class EnhancedLinkedList: public LinkedList<T>
  157. {
  158. public:
  159.     T& find_first (const T& key);

  160.     //Method find_first should search the EnhancedLinkedList for the first
  161.     //occurrence of an item that matches the value in the parameter key.
  162.     //It should return a reference to the first matching item.
  163.     //If the invoking EnhancedLinkedList object is empty or no item is found
  164.     //that matches the parameter, a ListItemNotFoundException should be thrown.
  165.     //You will have to define this exception
  166.     //(Hint: define this exception much the same way that the
  167.     //EmptyListException exception is defined in LinkedList.h).

  168.     EnhancedLinkedList find_all (const T& key);
  169.     //Method find_all should search the invoking EnhancedLinkedList
  170.     //for all elements that match the value in the parameter key.
  171.     //It should return an EnhancedLinkedList object containing
  172.     //copies of all the items that match the parameter key.
  173.     //If the invoking EnhancedLinkedList object is empty or
  174.     //no item is found that matches the parameter,
  175.     //this function should return an empty EnhancedLinkedList.

  176.     void remove_first (const T& key);
  177.     //Method remove_first should remove the first element from the
  178.     //invoking EnhancedLinkedList whose data item matches the parameter key.
  179.     //If the invoking EnhancedLinkedList object is empty or no item is found
  180.     //that matches the parameter, this function should do nothing.
  181.     //Remember to leave no memory leaks.

  182.     void remove_all (const T& key);
  183.     //Method remove_all should remove all elements from the invoking
  184.     //EnhancedLinkedList whose data items match the parameter key.
  185.     //If the invoking EnhancedLinkedList object is empty or no item is found
  186.     //that matches the parameter, this function should do nothing.
  187.     //Remember to leave no memory leaks.
  188. };

  189. template<typename T>
  190. T& EnhancedLinkedList<T>::find_first(const T& key)
  191. {
  192.     Node<T>* temp = this->head;

  193.     if(temp == NULL)
  194.         throw EmptyListException("Find first emptylist");

  195.     while(NULL != temp->getNext())
  196.     {
  197.         if(temp->getData()==key)
  198.             return temp->getData();
  199.         else
  200.             temp=temp->getNext();
  201.     }

  202.     throw EmptyListException("Find first not found");
  203. }

  204. template<typename T>
  205. EnhancedLinkedList<T>
  206. EnhancedLinkedList<T>::find_all(const T& key)

  207. {
  208.     EnhancedLinkedList<T> resualt;

  209.     Node<T>* temp = this->head;

  210.     while(NULL != temp)
  211.     {
  212.         if(temp->getData()==key)
  213.             resualt.push_back(temp->getData());
  214.         temp=temp->getNext();
  215.     }

  216. end:
  217.     return resualt;
  218. }

  219. template<typename T>
  220. void
  221. EnhancedLinkedList<T>::remove_first(const T& key)

  222. {
  223.     EnhancedLinkedList<T> list;
  224.     while(NULL!=this->head)
  225.     {
  226.         if(this->head->getData()!=key)
  227.             list.push_front(this->head->getData());
  228.         else{
  229.             T* temp = this->front();
  230.             this->pop_front();
  231.             delete temp;
  232.             break;
  233.         }
  234.         this->pop_front();
  235.     }

  236.     while(list.head!=NULL)
  237.     {
  238.         this->push_front(list.front());
  239.         list.pop_front();
  240.     }
  241. }

  242. template<typename T>

  243. void

  244. EnhancedLinkedList<T>::remove_all(const T& key)
  245. {
  246.     EnhancedLinkedList<T> list;

  247.     while(NULL!=this->head)
  248.     {
  249.         if(this->head->getData()!=key){
  250.             list.push_front(this->head->getData());
  251.             this->pop_front();
  252.         }
  253.         else{
  254.             T* temp = this->front();
  255.             this->pop_front();
  256.             delete temp;
  257.         }
  258.     }

  259.     while(list.head!=NULL)
  260.     {
  261.         this->push_front(list.front());
  262.         list.pop_front();
  263.     }
  264. }
  265. #endif //_ENHANCELINKLIST_H_
复制代码


大学时候的一道作业题 有你想要的东西

[ 本帖最后由 DraculaW 于 2008-8-11 16:55 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-08-11 16:41 |显示全部楼层
继承你想要继承的那个异常
重写what();
在异常的构造时 将你要打印的信息传入
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP