16,548
社区成员




#include <iostream>
#include <assert.h>
#include <stdafx.h>
using namespace std;
template <class type>
class ListNode{
public:
ListNode(){next = NULL;}
ListNode(const type & item, ListNode<type> *next1 = NULL){
data = item;
next = next1;
}
type data;
ListNode<type> * next;
};
template <class type>
class LinkList{
public:
LinkList(){
head = new ListNode<type>;
length = 0;
}
LinkList(LinkList<type> & L){
Copy(1);
}
~LinkList(){
MakeEmpty();
delete head;
}
ListNode<type> * GetHead() {return head;} //获得表头节点指针
ListNode<type> * GetNext(ListNode<type> & n) {return n.next == head?n.next->next:n.next;} //获得节点n的下一个节点位置
type GetData(int i); //取出链表中的第i个元素
bool SetData(type value, int i); //将链表中的第i个元素赋值为value
ListNode<type> * FindByVal(type value); //找到值为value的节点指针
ListNode<type> * FindByPos(int pos); //找到链表中第pos个节点指针
void MakeEmpty(); //清空整个链表
bool Insert(type value, int i); //在第i个位置插入值为value的节点
type RemoveByPos(int pos); //删除掉位置pos处的节点
type RemoveByVal(type value); //删除掉值为value的节点
LinkList<type> & Copy(LinkList<type> &L); //拷贝函数
LinkList<type> & operator = (LinkList<type> &L); //重载赋值运算符,同类型链表赋值
friend ostream & operator <<(ostream &out, LinkList<type> &L); //重载输出运算符
protected:
ListNode<type> * head;
int length;
};
//类模板中函数的实现如下:
template <class type>
ListNode<type> * LinkList<type>::FindByVal(type value){
ListNode<type> *p = head->next;
int i = 1;
while(i++ <= length && value != p->data)
p = p->next;
return p;
}
template <class type>
ListNode<type> * LinkList<type>::FindByPos(int pos){
if(pos < 0 || pos > length) return NULL;
if(pos == 0) return head;
ListNode<type> *p = head->next;
int i = 1;
while(i++ < pos && p!= NULL)
p = p->next;
return p;
}
template <class type>
type LinkList<type>::GetData(int pos){
ListNode<type> *p = FindByPos(pos);
assert(p && p != head);
return p->data;
}
template <class type>
bool LinkList<type>::SetData(type value, int pos){
ListNode<type> *p = FindByPos(pos);
if(!p || p == head)
return false;
else
p->data = value;
return true;
}
template <class type>
void LinkList<type>::MakeEmpty(){
ListNode<type> *p = head->next;
int i = 1;
while(i++ <= length){
head->next = p->next;
delete p;
p = head->next;
}
length = 0;
}
template <class type>
bool LinkList<type>::Insert(type value, int i){
ListNode<type> *p = FindByPos(i-1);
if(!p) return false;
ListNode<type> *node = new ListNode<type> (value,p->next);
assert(node);
p->next = node;
length++;
return true;
}
template <class type>
type LinkList<type>::RemoveByPos(int pos){
ListNode<type> *p = FindByPos(pos-1), *q;
assert(p && p->next != NULL);
q = p->next;
type val = q->data;
p->next = q->next;
delete q;
length--;
return val;
}
template <class type>
type LinkList<type>::RemoveByVal(type val){
ListNode<type> *p, *q = head;
p = q->next;
while((p->next != NULL) && (p->next->data != val)){
p = p->next;
}
q = p->next;
p->next = q->next;
delete q;
length--;
return val;
}
template <class type>
LinkList<type> & LinkList<type>::Copy(LinkList<type> &L){
if(! L.head)
return *this;
ListNode<type> *p = NULL, *q = NULL, *r = NULL;
this->length = L.length;
head = new ListNode<type>;
assert(head);
head->data = L.head->data;
head->next = NULL;
p = head;
q = L.head->next;
while(q){
r = new ListNode<type>;
if(r == NULL)
return *this;
r->data = q->data;
r->next = NULL;
p->next = r;
q = q->next;
p = p->next;
}
return *this;
}
template <class type>
LinkList<type> & LinkList<type>::operator = (LinkList<type> &L){
if(head)
MakeEmpty();
Copy(L);
return *this;
}
template <class type>
ostream & operator <<(ostream &out, LinkList<type> & L){
ListNode<type> *p = L.head->next;
out<<"length:"<<L.length<<"\ndata:";
while(p){
out<<p->data<<" ";
p = p->next;
}
out<<"\n";
return out;
}
#include <iostream>
#include "LinkList.h"
#include <stdafx.h>
using namespace std;
int main()
{
LinkList< int > m_list;
m_list.Insert(3,1);
m_list.Insert(2,2);
m_list.Insert(4,3);
cout<<m_list<<endl;
return 0;
}
friend ostream& operator<< <> (ostream &out, LinkList<type>& L);
//等同于:
friend ostream& operator<< <type> (ostream &out, LinkList<type>& L);
你可以试试!
<>之所以必不可少,是因为<>表明此友元函数是函数模板,在模板类中重装<<操作符时必不可少的
另外,<>中啥也不写你可以理解为此模板函数使用的模板类型参数为当前模板类的类型参数class type// LinkList.h 文件
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
template <class type>
class LinkList;
template <class type>
ostream& operator<<(ostream &out, LinkList<type>& L);
template <class type>
class ListNode{
public:
ListNode(){next = NULL;}
ListNode(const type & item, ListNode<type> *next1 = NULL){
data = item;
next = next1;
}
type data;
ListNode<type> * next;
};
template <class type>
class LinkList{
public:
LinkList(){
head = new ListNode<type>;
length = 0;
}
LinkList(LinkList<type> & L){
Copy(1);
}
~LinkList(){
MakeEmpty();
delete head;
}
ListNode<type> * GetHead() {return head;} //获得表头节点指针
ListNode<type> * GetNext(ListNode<type> & n) {return n.next == head?n.next->next:n.next;} //获得节点n的下一个节点位置
type GetData(int i); //取出链表中的第i个元素
bool SetData(type value, int i); //将链表中的第i个元素赋值为value
ListNode<type> * FindByVal(type value); //找到值为value的节点指针
ListNode<type> * FindByPos(int pos); //找到链表中第pos个节点指针
void MakeEmpty(); //清空整个链表
bool Insert(type value, int i); //在第i个位置插入值为value的节点
type RemoveByPos(int pos); //删除掉位置pos处的节点
type RemoveByVal(type value); //删除掉值为value的节点
LinkList<type> & Copy(LinkList<type> &L); //拷贝函数
LinkList<type> & operator= (LinkList<type> &L); //重载赋值运算符,同类型链表赋值
friend ostream& operator<< <type> (ostream &out, LinkList<type>& L); //重载输出运算符
protected:
ListNode<type> * head;
int length;
};
//类模板中函数的实现如下:
template <class type>
ListNode<type> * LinkList<type>::FindByVal(type value){
ListNode<type> *p = head->next;
int i = 1;
while(i++ <= length && value != p->data)
p = p->next;
return p;
}
template <class type>
ListNode<type> * LinkList<type>::FindByPos(int pos){
if(pos < 0 || pos > length) return NULL;
if(pos == 0) return head;
ListNode<type> *p = head->next;
int i = 1;
while(i++ < pos && p!= NULL)
p = p->next;
return p;
}
template <class type>
type LinkList<type>::GetData(int pos){
ListNode<type> *p = FindByPos(pos);
assert(p && p != head);
return p->data;
}
template <class type>
bool LinkList<type>::SetData(type value, int pos){
ListNode<type> *p = FindByPos(pos);
if(!p || p == head)
return false;
else
p->data = value;
return true;
}
template <class type>
void LinkList<type>::MakeEmpty(){
ListNode<type> *p = head->next;
int i = 1;
while(i++ <= length){
head->next = p->next;
delete p;
p = head->next;
}
length = 0;
}
template <class type>
bool LinkList<type>::Insert(type value, int i){
ListNode<type> *p = FindByPos(i-1);
if(!p) return false;
ListNode<type> *node = new ListNode<type> (value,p->next);
assert(node);
p->next = node;
length++;
return true;
}
template <class type>
type LinkList<type>::RemoveByPos(int pos){
ListNode<type> *p = FindByPos(pos-1), *q;
assert(p && p->next != NULL);
q = p->next;
type val = q->data;
p->next = q->next;
delete q;
length--;
return val;
}
template <class type>
type LinkList<type>::RemoveByVal(type val){
ListNode<type> *p, *q = head;
p = q->next;
while((p->next != NULL) && (p->next->data != val)){
p = p->next;
}
q = p->next;
p->next = q->next;
delete q;
length--;
return val;
}
template <class type>
LinkList<type> & LinkList<type>::Copy(LinkList<type> &L){
if(! L.head)
return *this;
ListNode<type> *p = NULL, *q = NULL, *r = NULL;
this->length = L.length;
head = new ListNode<type>;
assert(head);
head->data = L.head->data;
head->next = NULL;
p = head;
q = L.head->next;
while(q){
r = new ListNode<type>;
if(r == NULL)
return *this;
r->data = q->data;
r->next = NULL;
p->next = r;
q = q->next;
p = p->next;
}
return *this;
}
template <class type>
LinkList<type> & LinkList<type>::operator = (LinkList<type> &L){
if(head)
MakeEmpty();
Copy(L);
return *this;
}
template <class type>
ostream& operator<<(ostream &out, LinkList<type>& L){
ListNode<type> *p = L.head->next;
out<<"length:"<<L.length<<"\ndata:";
while(p){
out<<p->data<<" ";
p = p->next;
}
out<<"\n";
return out;
}
// main.cpp
#include <iostream>
#include "LinkList.h"
using namespace std;
int main()
{
LinkList< int > m_list;
m_list.Insert(3,1);
m_list.Insert(2,2);
m_list.Insert(4,3);
cout<<m_list<<endl;
return 0;
}