#include<iostream>
#include<stdio.h>
/**
*insert 的index越界处理:index = index<0?0:index;index = index >_size?_size:index;
*/
using namespace std;
template<class T>
class Node{
public:
T data;
Node<T>* next;
Node<T>(const T&d):data(d),next(NULL){}
Node<T>(const Node<T> ©Node);
Node<T>(){}
private:
Node<T>& operator=(const Node<T>&);
};
template<class T>
class LinkedList
{
private:
int _size;
public:
Node<T>* head;
Node<T>* tail;
LinkedList(const LinkedList & LL);
LinkedList & operator=(LinkedList byValList);
LinkedList():head(NULL),tail(NULL),_size(0){}
LinkedList(Node<T>* newNode):head(newNode),tail(newNode),_size(1){}
~LinkedList();
void insert(T val,int index);
void push_back(T val);
void updateListElement(T val,int index);
void removeListElement(int index);
void printAllElements();
Node<T>& get(int index);
int size();
};
template<class T>
LinkedList<T>::LinkedList(const LinkedList & LL):head(NULL),tail(NULL){
//const T* p;指向常量的指针,表示被指对象不能改变值
//T const *p;常量指针,表示指针指向地址不能被改变,但指向的对象值可变。
const Node<T>* cur = LL.head;
if(!head && cur){
head = new Node<T>(cur->data);
tail = head;
cur = cur->next;
}
while(cur){
Node<T>* newNode = new Node<T>(cur->data);
tail->next = newNode;
tail = newNode;
cur = cur->next;
}
cur = NULL;
}
template<class T>
LinkedList<T>& LinkedList<T>::operator=(LinkedList byValList){
std::swap(head,byValList.head);
return *this;
}
template<class T>
LinkedList<T>::~LinkedList(){
Node<T>* cur = this->head;
while(this->head){
//puts("deleting");
this->head = this->head->next;
delete cur ;
cur = this->head;
}
}
template<class T>
void LinkedList<T>::push_back(T val){
Node<T>* newNode = new Node<T>(val);
newNode->next = NULL;
if(!tail){
tail = newNode;
return;
}
if(!head){
head = newNode;
return;
}
tail->next = newNode;
tail = tail->next;
++_size;
}
template<class T>
void LinkedList<T>::insert(T val,int index){
if(index < 0 ) index = 0;
Node<T>* p,*cur = new Node<T>();
p = cur;
cur->next = head;
if(_size <= index) {
this.push_back(val);
return;
}else{
for(int i = 0;i<index;i++) cur = cur->next;
Node<T>* newNode = new Node<T>(val);
newNode->next = cur->next;
cur->next = newNode;
newNode = NULL;
}
cur = NULL;
delete p;
}
template<class T>
void LinkedList<T>::updateListElement(T val,int index){
Node<T>* cur = this->head;
if(index < 0) index = 0;
if(_size <= index) index = _size-1;
for(int i = 0;i<index;i++)cur = cur->next;
cur->data = val;
cur = NULL;
}
template<class T>
void LinkedList<T>::removeListElement(int index){
Node<T>* cur = this->head;
if(index < 0) index = 0;
if(_size <= index) index = _size-1;
for(int i = 0;i<index-1;i++)cur = cur->next;
Node<T>* p = cur->next;
cur->next = cur->next->next;
delete p;
cur = NULL;
--_size;
}
template<class T>
void LinkedList<T>::printAllElements(){
Node<T>* cur = this->head;
for(int i =0;i<_size;i++,cur=cur->next) cout<<cur->data<<" ";
cout<<endl;
cur = NULL;
}
template<class T>
Node<T>& LinkedList<T>::get(int index){
Node<T>* cur = this->head;
if(index < 0) index = 0;
if(_size <= index) index = _size-1;
for(int i = 0;i<index;i++)cur = cur->next;
return *cur;
}
typedef struct TestNode{
string str;
int x;
TestNode():str(""),x(0){}
TestNode(string s,int xx):str(s),x(xx){}
friend ostream& operator<<(ostream& os, const TestNode& node);
}TestNode;
ostream& operator<<(ostream& os, const TestNode& node)
{
os << node.str << " -- " << node.x << " " << endl;
return os;
}
int main(){
TestNode t("hello",33);
LinkedList<int> list(new Node<int>(5)) ;
list.push_back(1);
list.push_back(3);
list.push_back(2);
list.push_back(5);
list.printAllElements();
list.updateListElement(100,1);
list.printAllElements();
list.removeListElement(1);
list.printAllElements();
// LinkedList<TestNode> list(new Node<TestNode>(t));
// list.push_back(TestNode("a",1));
// list.push_back(TestNode("c",3));
// list.push_back(TestNode("b",2));
// list.push_back(TestNode("z",25));
// list.printAllElements();
// Node<TestNode>& cur = list.get(2);
// cout<<cur.data.str<<" "<<cur.data.x<<endl;
}
1.c++template基本使用
2.单链表基本实现