NextersStudy1

Exercise

Correct!
False!

규태

  • 규태는 C++ 를 좋아한다. 그런데 잘한다
  • C++ tempalate 구현을 했다

구현

//GList.h #ifndef __LinkedList__GList__ #define __LinkedList__GList__ #include <iostream> #define USING_NS_GL using namespace glist using namespace std; namespace glist { template <class T> class GList; // template <class T> class GStack; // template <class T> class GQueue; // GElement template <class T> class GElement { public: GElement(const T& value): next(NULL), data(value) {} GElement(const GElement<T> &elem) { next = elem.next; data = elem.data; } ~GElement() {}; GElement& operator=(const GElement<T> &elem) { next = elem.next; data = elem.data; return *this; } public: template <class TP> friend class GList; // template <class TP> friend class GStack; // template <class TP> friend class GQueue; private: GElement *next; T data; }; // GList template <class T> class GList { public: GList() : head(NULL), rear(NULL), elem_cnt(0) {} ~GList() {} bool isEmpty() const { return elem_cnt == 0; } unsigned int getLength() const { return elem_cnt; } void insertElementInFront(const T& value); void insertElementInEnd(const T& value); bool insertElementAtIndex(const T& value, unsigned int index); bool deleteElementInFront(); bool deleteElementInEnd(); bool deleteElementAtIndex(unsigned int index); void deleteAllElements(); const T& getElementInFront() const;// { return head->data; } const T& getElementInEnd() const;// { return rear->data; } const T& getElementAtIndex(unsigned int index) const; T& operator[](unsigned int index) const; private: GElement<T> *head; GElement<T> *rear; int elem_cnt; }; class OutOfRangeException { public: OutOfRangeException (const string& what_arg) { msg = string(what_arg); } string& what() { return msg; } private: string msg; }; // // GStack // template <class T> // class GStack { // // }; // // // GQueue // template <class T> // class GQueue { // // }; } // Implement of methods USING_NS_GL; template <class T> void GList<T>::insertElementInFront(const T& value) { GElement<T> *elem = new GElement<T>(value); if (isEmpty()) { head = elem; rear = elem; } else { elem->next = head; head = elem; } elem_cnt++; } template <class T> void GList<T>::insertElementInEnd(const T& value) { GElement<T> *elem = new GElement<T>(value); if (isEmpty()) { head = elem; rear = elem; } else { rear->next = elem; rear = elem; } elem_cnt++; } template <class T> bool GList<T>::insertElementAtIndex(const T& value, unsigned int index) { if ((int)index < 0 || (int)index > elem_cnt) return false; if (index == 0) { insertElementInFront(value); } else if (index == elem_cnt) { insertElementInEnd(value); } else { GElement<T> *p = head; for (int i = 0; i < index - 1; i++) { p = p->next; } GElement<T> *elem = new GElement<T>(value); elem->next = p->next; p->next = elem; elem_cnt++; } return true; } template <class T> bool GList<T>::deleteElementInFront() { if (isEmpty()) return false; GElement<T> *p = head; head = head->next; delete p; elem_cnt--; if (isEmpty()) head = rear = NULL; return true; } template <class T> bool GList<T>::deleteElementInEnd() { if (isEmpty()) return false; GElement<T> *target = head; while (target->next != rear) target = target->next; rear = target; // *target(pointer): previous node of target target = target->next; delete target; rear->next = NULL; elem_cnt--; if (isEmpty()) head = rear = NULL; return true; } template <class T> bool GList<T>::deleteElementAtIndex(unsigned int index) { if (isEmpty()) return false; if ((int)index < 0 || (int)index > elem_cnt-1) return false; if (index == 0) { deleteElementInFront(); } else if (index == elem_cnt-1) { deleteElementInEnd(); } else { GElement<T> *target = head; GElement<T> *prev = NULL; for (int i = 0; i < index; i++) { prev = target; target = target->next; } prev->next = target->next; delete target; elem_cnt--; } return true; } template <class T> void GList<T>::deleteAllElements() { GElement<T> *p = head; while (p != NULL) { GElement<T> *p2 = p; p = p->next; delete p2; elem_cnt--; } head = rear = NULL; } template <class T> const T& GList<T>::getElementInFront() const { if (head == NULL) { throw OutOfRangeException("(GList)Out of range: getElementInFront"); } return head->data; } template <class T> const T& GList<T>::getElementInEnd() const { if (rear == NULL) { throw OutOfRangeException("(GList)Out of range: getElementInEnd"); } return rear->data; } template <class T> const T& GList<T>::getElementAtIndex(unsigned int index) const { if ((int)index < 0 || (int)index > elem_cnt-1) { throw OutOfRangeException("(GList)Out of range: getElementAtIndex"); } return (*this)[index]; } template <class T> T& GList<T>::operator[](unsigned int index) const { if ((int)index < 0 || (int)index > elem_cnt-1) { throw OutOfRangeException("(GList)Out of range: operator[]"); } GElement<T> *target = head; for (int i = 0; i < index; i++) { target = target->next; } return target->data; } #endif /* defined(__LinkedList__GList__) */