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;
}