C++チェーン構造のカスタムコンテナクラス(一)


#ifndef LINKED_H_
#define LINKED_H_
//---------------------------------------------
//     file:Linked.h
//               <        ,           ,  new>
//      :         ,       
//    1.      :             ,          int or double or     ..., 
//                 ,       .      :Linked<int> intlist;
//    2.         
//    3.            :push_front
//    4.          ,     :      Linked(),       
//    Linked(const Linked<T>& otherLinked),     operator=
//    Linked<int> intList1;      Linked()
//    Linked<int> intList2 = intList1;      Linked<int> intList2(intList1), 
//            ,           ,   explicit   
//    Linked<int> intList3(intList1);      
//    intList3 = intList2;     
//---------------------------------------------
#include <iostream>

using namespace std;

template<typename T>
class Linked{
protected:
    struct Node{
    T item;
    Node* next;
    };//    (  )  2 :         ,           
    Node* head;//   
    unsigned long length;//      
public:
    // Postcondition(    ):          
    Linked()
    {
        head = NULL;
        length = 0;
    } 
    // Postcondition:        
    unsigned long size() const
    {
        return length;
    } 
    // Postcondition:              
    void push_front(const T& newItem)
    {   
        Node* newHead = new Node;
        newHead->item = newItem;
        newHead->next = head;
        head = newHead;
        length++;
    }
    // Precondition(    ):       
    // Postcondition:               
    void pop_front()
    {
    if(NULL != head){
        Node* oldHead = head;
        head = head->next;
        delete oldHead;
        length--;
    }
        //      ,       2   :
        //head = head->next;
        //length--;
        //      ,         ,          ,     , 
        //         ,       ,       ,        delete 
    }
    // Postcondition:             (
    //              :Linked<int> intList1; Linked<int> intList2 = intList1;)
    void operator=(const Linked<T>& otherLinked)
    {
        Linked<T> tempLinked;
        Node* tempPtr;
        //          
        while (head != NULL)
            pop_front();
        for (tempPtr = otherLinked.head; tempPtr != NULL; tempPtr = tempPtr -> next)
            tempLinked.push_front (tempPtr -> item);
        for (tempPtr = tempLinked.head; tempPtr != NULL; tempPtr = tempPtr -> next)
            push_front (tempPtr -> item);
    } //       = 
    // Postcondition:             
    //              :Linked<int> intList1; Linked<int> intList2(intList1);
    explicit Linked(const Linked<T>& otherLinked)
    {
        head = NULL;
        length = 0;
        Linked<T> tempLinked;
        Node* tempPtr;
        for (tempPtr = otherLinked.head; tempPtr != NULL; tempPtr = tempPtr->next)
            tempLinked.push_front(tempPtr->item);
        for (tempPtr = tempLinked.head; tempPtr != NULL; tempPtr = tempPtr->next)
            push_front(tempPtr->item);
    } //       
    
    // Postcondition:       ,        
    virtual ~Linked()
    {
    cout << "~Linked() called." << endl;
        while (head != NULL)
            pop_front();
    } //     
};
#endif

//file:main.cpp
//-------------    ----------------
#include "Linked.h"
#include <iostream>

using namespace std;

int main(){
	Linked<int> intList1;
	for(int i=1; i<=100; ++i){
		intList1.push_front(i);
	}
	cout << "intList1 length:" << intList1.size() << endl;
	//        
	//Linked<int> intList2 = intList1;      ,        ,       explicit   
	Linked<int> intList2(intList1);
	cout << "intList2 length:" << intList2.size() << endl;
	
	int ch;
	cin >> ch;
	return 0;
}

//     ,                . 
//            ,        ,         .