• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            DraculaW

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              19 隨筆 :: 0 文章 :: 7 評論 :: 0 Trackbacks
            #ifndef _LINKEDLIST_H_

            #define _LINKEDLIST_H_



            #include <stdexcept>



            using namespace std;



            class EmptyListException : public logic_error {



            public:

                EmptyListException(const string& what_arg ) throw() :

                  logic_error ("Empty list exception: " + what_arg) {}}

            ;



            template <class T>

            class Node {

            private:

                T data;

                Node* next;



            public:

                Node(T d, Node* n = NULL) : data(d), next(n) {}

                T& getData() { return data;}

                Node*& getNext() { return next;}



            };



            template <class T>

            class LinkedList {



            protected:



                Node<T>* head; // Beginning of list

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

                int count; // Number of nodes in list



            public:



                LinkedList(void) : head(NULL), tail(NULL), count(0) {}

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

                virtual ~LinkedList(void); // Destructor



                virtual T& front(void) {



                    if (head == NULL) {

                        throw EmptyListException("front()");

                    }

                    return head->getData();

                }

                virtual T& back(void) {

                    if (tail == NULL) {

                        throw EmptyListException("back()");

                    }

                    return tail->getData();

                }

                virtual int size(void) {

                    return count;

                }

                virtual bool empty(void) {

                    return count == 0;

                }



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

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

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

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



                virtual void dump(void); // Output contents of list

            };



            // Copy constructor

            template <class T>

            LinkedList<T>::LinkedList(const LinkedList<T>& src) :

            count(0), head(NULL), tail(NULL) {



                Node<T>* current = src.head;

                while (current != NULL) {

                    this->push_back(current->getData());

                    current = current->getNext();

                }



            }



            // Destructor

            template <class T>

            LinkedList<T>::~LinkedList(void) {



                while (!this->empty()) {

                    this->pop_front();

                }

            }



            // Insert an element at the beginning

            template <class T>

            void LinkedList<T>::push_front(T d) {



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



                if (this->empty()) {

                    head = tail = new_head;

                }

                else {

                    head = new_head;

                }

                count++;

            }



            // Insert an element at the end

            template <class T>

            void LinkedList<T>::push_back(T d) {



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



                if (this->empty()) {

                    head = new_tail;

                }

                else {

                    tail->getNext() = new_tail;

                }



                tail = new_tail;

                count++;

            }



            // Remove an element from the beginning

            template <class T>

            void LinkedList<T>::pop_front(void) {



                if (head == NULL) {

                    throw EmptyListException("pop_front()");

                }



                Node<T>* old_head = head;



                if (this->size() == 1) {

                    head = NULL;

                    tail = NULL;

                }

                else {

                    head = head->getNext();

                }



                delete old_head;

                count--;

            }



            // Remove an element from the end

            template <class T>

            void LinkedList<T>::pop_back(void) {



                if (tail == NULL) {

                    throw EmptyListException("pop_back()");

                }



                Node<T>* old_tail = tail;



                if (this->size() == 1) {

                    head = NULL;

                    tail = NULL;

                }

                else {



                    // Traverse the list

                    Node<T>* current = head;

                    while (current->getNext() != tail) {

                        current = current->getNext();

                    }



                    // Unlink and reposition

                    current->getNext() = NULL;

                    tail = current;

                }



                delete old_tail;

                count--;

            }



            // Display the contents of the list

            template <class T>

            void LinkedList<T>::dump(void) {



                cout << "(";



                if (head != NULL) {

                    Node<T>* current = head;

                    while (current->getNext() != NULL) {

                        cout << current->getData() << ", ";

                        current = current->getNext();

                    }

                    cout << current->getData();

                }



                cout << ")" << endl;

            }



            #endif



            #ifndef _ENHANCELINKLIST_H_

            #define _ENHANCELINKLIST_H_



            #include "LinkedList.h"



            template<typename T>

            class EnhancedLinkedList: public LinkedList<T>

            {

            public:

                T& find_first (const T& key);

                //Method find_first should search the EnhancedLinkedList for the first

                //occurrence of an item that matches the value in the parameter key.

                //It should return a reference to the first matching item.

                //If the invoking EnhancedLinkedList object is empty or no item is found

                //that matches the parameter, a ListItemNotFoundException should be thrown.

                //You will have to define this exception

                //(Hint: define this exception much the same way that the

                //EmptyListException exception is defined in LinkedList.h).



                EnhancedLinkedList find_all (const T& key);

                //Method find_all should search the invoking EnhancedLinkedList

                //for all elements that match the value in the parameter key.

                //It should return an EnhancedLinkedList object containing

                //copies of all the items that match the parameter key.

                //If the invoking EnhancedLinkedList object is empty or

                //no item is found that matches the parameter,

                //this function should return an empty EnhancedLinkedList.



                void remove_first (const T& key);

                //Method remove_first should remove the first element from the

                //invoking EnhancedLinkedList whose data item matches the parameter key.

                //If the invoking EnhancedLinkedList object is empty or no item is found

                //that matches the parameter, this function should do nothing.

                //Remember to leave no memory leaks.



                void remove_all (const T& key);

                //Method remove_all should remove all elements from the invoking

                //EnhancedLinkedList whose data items match the parameter key.

                //If the invoking EnhancedLinkedList object is empty or no item is found

                //that matches the parameter, this function should do nothing.

                //Remember to leave no memory leaks.

            };



            template<typename T>

            T& EnhancedLinkedList<T>::find_first(const T& key)

            {

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

                if(temp == NULL)

                    throw EmptyListException("Find first emptylist");



                while(NULL != temp->getNext())

                {

                    if(temp->getData()==key)

                        return temp->getData();

                    else

                        temp=temp->getNext();

                }



                throw EmptyListException("Find first not found");

            }



            template<typename T>

            EnhancedLinkedList<T>

            EnhancedLinkedList<T>::find_all(const T& key)

            {

                EnhancedLinkedList<T> resualt;



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



                while(NULL != temp)

                {

                    if(temp->getData()==key)

                        resualt.push_back(temp->getData());

                    temp=temp->getNext();

                }


            end:
                return resualt;

            }



            template<typename T>

            void

            EnhancedLinkedList<T>::remove_first(const T& key)

            {

                EnhancedLinkedList<T> list;



                while(NULL!=this->head)

                {

                    if(this->head->getData()!=key)

                        list.push_front(this->head->getData());

                    else{

                        T* temp = this->front();

                        this->pop_front();

                        delete temp;

                        break;

                    }

                    this->pop_front();

                }



                while(list.head!=NULL)

                {

                    this->push_front(list.front());

                    list.pop_front();

                }

            }



            template<typename T>

            void

            EnhancedLinkedList<T>::remove_all(const T& key)

            {

                EnhancedLinkedList<T> list;



                while(NULL!=this->head)

                {

                    if(this->head->getData()!=key){

                        list.push_front(this->head->getData());

                        this->pop_front();

                    }

                    else{

                        T* temp = this->front();

                        this->pop_front();

                        delete temp;

                    }

                }



                while(list.head!=NULL)

                {

                    this->push_front(list.front());

                    list.pop_front();

                }

            }



            #endif //_ENHANCELINKLIST_H_
            posted on 2007-11-15 20:29 DraculaW 閱讀(281) 評論(0)  編輯 收藏 引用
            欧美无乱码久久久免费午夜一区二区三区中文字幕 | 久久久久免费看成人影片| 久久夜色精品国产噜噜噜亚洲AV| 77777亚洲午夜久久多人| 久久久亚洲欧洲日产国码aⅴ| 成人妇女免费播放久久久| 青青草原综合久久大伊人精品| 久久国产三级无码一区二区| 久久精品中文字幕一区| 久久99精品国产麻豆宅宅| 久久综合伊人77777麻豆| 久久久久人妻精品一区| 久久久久无码精品国产app| 欧洲人妻丰满av无码久久不卡| 久久99免费视频| 99精品国产综合久久久久五月天| 中文字幕亚洲综合久久2| 久久久久久精品免费看SSS| 国产精品毛片久久久久久久| 亚洲国产精品无码久久九九 | 国产精品久久久久蜜芽| 国产精品久久久久久| 久久久无码精品亚洲日韩京东传媒| 日韩亚洲欧美久久久www综合网| 麻豆av久久av盛宴av| 久久国产影院| 国内精品免费久久影院| 中文字幕一区二区三区久久网站| 久久综合噜噜激激的五月天| 蜜桃麻豆www久久国产精品| 99久久国产亚洲高清观看2024 | 久久天天躁狠狠躁夜夜网站| 久久福利资源国产精品999| 国产精品久久久天天影视香蕉| 国产三级久久久精品麻豆三级 | 国产福利电影一区二区三区,免费久久久久久久精 | 久久激情五月丁香伊人| 亚洲乱亚洲乱淫久久| 国产精品久久久久久久午夜片| 91久久精品电影| 久久天天躁狠狠躁夜夜2020|