14.C++类模板和链表

template <typename Type = int>//此处Type还可以加默认类型
class A
{
    public:
        void func();
    private:
}
一个类定义为类模板,他的类名A将变为类模板名A<type>
void A<type>::func()
===============================================
友元类有两种默认方式
第一种先声明后定义
template <typename Type>
class B

template <typename Type>
class A
{
    public:
        void func();
        friend class B<Type>;
    private:
}
第二种声明的同时定义
template <typename Type>
class A
{
    public:
        void func();
        friend template <typename type> class B;    //此处参数Type改一下type
    private:
}

#include<iostream>
using namespace std;

template <typename type>
class Array {
private:
    int size;
    type* p;
public:
    Array(int size, type initValue);
    type& operator[](int index);
    void show();
};

template <typename type>
Array<type>::Array(int size, type initValue)
{
    this->size = size > 1 ? size : 1;
    p = new type[size];
    for (int i = 0; i < size; i++)
        p[i] = initValue;
}

template <typename type>
type& Array<type>::operator[](int index)
{
    return p[index];
}

template <typename type>
void Array<type>::show()
{
    for (int i = 0; i < size; i++)
        cout << p[i] << " ";
    cout << endl;
}

int main()
{
    Array<int> a(10, 6);
    a.show();
    cout << a[9] << endl;
    return 0;
}


/************************************************/
#include<iostream>
using namespace std;

//template <typename Type>
//class List;//类声明

template <typename Type>
class Node
{
private:
    Type data;
    Node<Type>* next;
public:
    Node(Type data=0,Node<Type>* n=NULL):data(data),next(n){};
    ~Node(){};
    //friend class List<Type>;//友元类
    template <typename type> friend class List;
};

/************************************************/

template <typename Type>
class List
{
private:
    Node<Type>* head;
    Node<Type>* last;
    int size;
public:
    List(int length = 0);
    bool InsetNode(Type data);
    void show();
    ~List();
};

template<typename Type>
void List<Type>::show()
{
    Node<Type>* temp = head->next;
    while (temp->next!=last->next)//如果不是最后一个元素
    {
        cout << temp->data << "-->";
        temp =temp->next;//切换下一个元素
    }
    cout<<last->data<<endl;
}

template<typename Type>
bool List<Type>::InsetNode(Type data)
{
    last->next = new Node<Type>(data);
    if(last->next == NULL)//节点空间分配失败,直接返回
        return 0;
    last = last->next;
    last->data = data;
    last->next = NULL;
    return 1;
}

template <typename Type>
List<Type>::List(int length)
{
    head = new Node<Type>;
    if(head == NULL)//检查是否创建成功
        cout << "initilize error" <<endl;
    else
    {
        last = head;
        last->next = NULL;
        this->size =0;
        length = length>0?length:0;
        while(length > 0)
        {
            if(InsetNode(size)==1)//插入成功,size++
                this->size++;
            else
            {
                cout << "Insert error" <<endl;
                break;
            }
            length = length -1;
        }
        cout << "构造列表:" << this->size << "元素" <<endl;
    }
}

template <typename Type>
List<Type>::~List()
{
    Node<Type>* temp = NULL;
    while(last!=head)
    {
        temp = head;
        head = head->next;
        delete temp;
    }
    delete head;
}

/************************************************/
int main()
{
    List<int> L1(15);
    L1.show();
    L1.InsetNode(10086);
    L1.show();
    return 0;
}
#include <iostream>
using namespace std;
class Node
{
private:
    int idata;
    Node* nextNode;

public:
    Node(int size = 0, Node* next = NULL) : idata(size), nextNode(next)
    {
        //cout << "构造" << idata << endl;
    };

    Node* CreateNode(int data)
    {
        nextNode = new Node(data);
        if (nextNode != NULL) //如果是空,则开辟空间失败了
            nextNode->nextNode = NULL;
        else
            cout << "申请节点内存失败" << endl;
        return nextNode;
    }
    /*
    ~Node1()
    {
        cout << "析构" << idata << endl;
        if (nextNode != NULL)//调用递归析构,如果链表巨大会导致内存占有量过大
            delete nextNode;
        nextNode = NULL;
    };
    ~Node2)
    {
        while (nextNode != NULL)//因为isLastNode->nextNode==NULL,所以进不来
        {
            Node* preLastNode = this;//只有一个节点时,preLastNode->nextNode= NULL,即要把头结点的下一个元素置空
            Node* LastNode = preLastNode->nextNode;
            while (LastNode->nextNode != NULL) {//如果一个节点的下一个节点是空,那他就是最后一个节点
                preLastNode = LastNode;
                LastNode = LastNode->nextNode;//让isLastNode等于下一个isLastNode
            }
            cout << "    析构节点:" << LastNode->idata << endl;
            preLastNode->nextNode = NULL;//将上一个节点的指针置空,代表这是最后一个数据
            delete LastNode;//删除最后一个节点
            LastNode = NULL;
        }
    };
*/
    ~Node()
    {
        cout << "析构" << idata << endl;
    };

    friend class List;//链表要访问设置next指针,申请为友元类
};

class List
{
private:
    Node* head;
    Node* last;

public:
    //构造初始化链表
    List(int initSize = 0)//initSize初始化给定的链表长度
    {
        head = last = new Node(0);//头结点包含链表长度数据
        if (head != NULL)
        {
            initSize = initSize > 0 ? initSize : 0;//防止给定的复数
            while (initSize > 0)
            {
                bool success = ListInsert(initSize);//判断节点有没有插入成功
                if (success == false)
                {
                    cout << "创建节点阶段异常,成功创建" << head->idata << "个节点,请尝试插入操作" << endl;
                    break;
                }
                initSize = initSize - 1;
            }
            cout << "初始化完成,列表包含" << head->idata << "个节点!" << endl;
        }
        else
        {
            cout << "系统异常,已创建终止列表" << endl;
        }
    }
    //项链表中插入元素
    bool ListInsert(int data = 0)
    {
        last->nextNode = last->CreateNode(data);
        if (last->nextNode != NULL)//判断节点有没有申请成功
        {
            head->idata++;
            last = last->nextNode;
            return true;
        }
        else
        {
            cout << "节点插入失败" << endl;
            return false;
        }

    }
    //显示链表
    void show()
    {
        cout << "遍历链表信息" << head->idata << "元素:";
        Node* forward = head;
        while (forward->nextNode != NULL) //如果不是最后一个元素
        {
            forward = forward->nextNode; //切换下一个元素
            cout << forward->idata << "->";
        }
        cout << "NULL" << endl;
    }

    /*
	//析构链表
	~List()//对应~Node1,2()函数
	{
		cout << "析构链表开始" << endl;
		if (head != NULL)
			delete head;//删除头结点,会调用节点的析构函数,调用节点的析构函数,会删除其他节点
		cout << "析构整个链表结束" << endl;
		head = last = NULL;
	}
    */

    ~List()
    {
        cout << "析构链表开始" << endl;
        Node* cur = NULL;
        while((cur = head) != NULL)
        {
                head = cur->nextNode;
                delete cur;
        }
        cout << "析构整个链表结束" << endl;
        head = last = NULL;
    }
};

int main()
{
    List list(10);
    list.ListInsert(10086);
    list.show();
}
#include <iostream>

using namespace std;

struct Node{
    int value;
    int key;
    Node *next;
};

class numberList{
private:
    Node *Head;

public:
    numberList(){
        Head = nullptr;
    }

public:
    Node* checkNodeExist(Node* obj)
    {
        Node *temp = nullptr;
        Node *ptr = Head;

        while(ptr != nullptr)
        {
            if(ptr->key == obj->key)
            {
                temp = ptr;
            }
            ptr = ptr->next;
        }
        return temp;
    }


    void appendNode(int value, int key)
    {
        Node* newNode = new Node;
        newNode->key = key;
        newNode->value = value;
        newNode->next = nullptr;

        if(checkNodeExist(newNode) != nullptr)
        {
            cout << "Error\nNode with Key " << newNode->key << " Exists.";
            free(newNode);
        }
        else
        {
            if(Head == nullptr)
            {
                Head = newNode;
            }
            else
            {
                Node *ptr = Head;
                while(ptr->next != nullptr)
                {
                    ptr=ptr->next;
                }
                ptr->next = newNode;
            }
        }
    }

    void displayNodes()
    {
        Node *ptr = Head;
        cout << "KEY   VALUE" << endl;
        while(ptr != nullptr)
        {
            cout << ptr->key <<  "     " << ptr->value << endl;
            ptr= ptr->next;
        }
    }

};

int main()
{
    numberList somenum;
    somenum.appendNode(23,1);
    somenum.appendNode(32,2);
    somenum.appendNode(21,3);
    somenum.appendNode(27,4);


    somenum.displayNodes();

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值