单向链表类模板
/*1 设计一个单向链表类模板,节点数据域中数据从小到大排列,并设计插入、删除节点的成员函数。*/
using namespace std;
template < typename T>
class People
{
public:
void setX(T x);
T getX(void);
friend ostream & operator<<<>(ostream & out, const People & x);
friend istream & operator>><>(istream & in, People & x);
private:
T m_x;
};
template < typename T>
struct Node
{
People<T> peo;
struct Node<T>* next;
};
template < typename T>
void People<T>::setX(T x)
{
m_x = x;
}
template < typename T>
istream& operator>><>(istream & in, People<T> & x)
{
in >> x.m_x;
return in;
}
template < typename T>
ostream & operator<<<>(ostream & out, const People<T> & x)
{
out << x.m_x << " ";
return out;
}
template < typename T>
T People<T>::getX(void)
{
return m_x;
}
template < typename T>
Node<T>* creatNode(Node<T>* head)//Node<T>* head
{
head = new struct Node<T>;
head->peo.setX(0);
//head->peo.setX(0);
head->next = NULL;
return head;
}
template < typename T>
void insertNode(const People<T> & man, Node <T>* head)
{
Node <T>* p = head;
while (p->next)
{
p = p->next;
}
Node <T>* newNode = new Node<T>;
newNode->peo = man;
p->next = newNode;
newNode->next = NULL;
}
template<typename T>
void deleteNode(Node<T>* head,const T x)
{
Node<T>* pR;
Node<T>* pN;
bool flag = false;
pN = head->next;
pR = head;
while (pN)
{
if (pN->peo.getX() == x)
{
flag = true;
pR->next = pN->next;
delete pN;
pN = pR->next;
continue;
}
pR = pN;
pN = pN->next;
}
if (!flag)
{
cout << "no such num" << endl;
}
}
template<typename T>
void sort(Node<T>* head)//冒泡排序
{
Node<T>* pR = head;
Node<T>* p = pR->next;
Node<T>* pTail = NULL;
while (head->next->next != pTail)
{
pR = head;
p = head->next;
while (p->next != pTail)
{
if (p->peo.getX() > p->next->peo.getX())
{
pR->next = p->next;
p->next = p->next->next;
pR->next->next = p;
p = pR->next;
}
p = p->next;
pR = pR->next;
}
pTail = p;
}
}
template < typename T>
void displayNode(Node<T>* head)
{
while (head->next)
{
head = head->next;//.的优先级比*高
cout << head->peo;
}
}
template < typename T>
void deleteSpace(Node<T>* head)
{
Node<T>* p = head, *pR = NULL;
while (p)
{
pR = p->next;
delete p;
p = pR;
}
}
int main()
{
Node <int> *head = NULL;
head = creatNode(head);
People<int> man;
cin >> man;
while (man.getX() != -1)
{
insertNode(man, head);
cin >> man;
}
sort(head);
displayNode(head);
cout << "\ninput which one you want to delete :";
int x;
cin >> x;
deleteNode(head, x);
displayNode(head);
deleteSpace(head);//删除堆分配
system("pause");
return 0;
}
类String的原型
#include <iostream>
using namespace std;
class String
{
public:
String(const char *str = NULL);
String(const String &other);
~String(void);
String & operator = (const String &other);
friend ostream & operator<<(ostream & out,const String & s);
String & operator+(const String &s);
private:
char *m_data;
};
String::String(const char *str)
{
m_data = new char[strlen(str) + 1];
int i = 0;
while (str[i])
{
m_data[i] = str[i];
i++;
}
m_data[i] = 0;
}
String::String(const String &other)
{
m_data = new char[strlen(other.m_data) + 1];
int i = 0;
while (other.m_data[i])
{
m_data[i] = other.m_data[i];
i++;
}
m_data[i] = 0;
}
String & String::operator= (const String &other)
{
int i = 0;
while (other.m_data[i])
{
this->m_data[i] = other.m_data[i];
i++;
}
this->m_data[i] = 0;
return *this;
}
ostream & operator<<(ostream & out, const String & s)
{
int i = 0;
while (s.m_data[i])
{
out << s.m_data[i];
i++;
}
return out;
}
String & String::operator+(const String &s)
{
int len = strlen(s.m_data) + 1 + strlen(this->m_data) + 1;
int i = 0;
char* spy1 = new char[strlen(s.m_data) + 1];
while (s.m_data[i])
{
spy1[i] = s.m_data[i];
i++;
}
spy1[i] = 0;
int j = 0;
char* spy2 = new char[strlen(this->m_data) + 1];
while (this->m_data[j])
{
spy2[j] = this->m_data[j];
j++;
}
spy2[j] = 0;
this->m_data = new char[len];
int k = 0;
while (spy1[k])
{
this->m_data[k] = spy1[k];
k++;
}
int t = 0;
while (spy2[t])
{
this->m_data[k] = spy2[t];
t++;
k++;
}
this->m_data[k] = 0;
delete[] spy1;
delete[] spy2;
return *this;
}
String::~String(void)
{
delete[] m_data;
}
int main()
{
String s1("zhang");
cout << s1<<endl;
String s2("chen");
cout << s2 << endl;
String s3(s1 = s2);
cout << s3 << endl;
String s5("wang");
String s6(s1 + s5);
cout << s6 << endl;
system("pause");
return 0;
}