基本单链表的增删改C++
来源:互联网 发布:悠仁亲王 弱智 知乎 编辑:程序博客网 时间:2024/06/06 00:47
#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.单链表基本实现
0 0
- 基本单链表的增删改C++
- 单链表的增删改查基本操作
- 线性表增删改的基本算法C实现
- 基本的增删改查
- 【C++】单链表的增删查改实现
- 【C#】DataGridView的增删改
- 基本的增删改查流程
- sql基本的增删查改
- Hibernate基本的增删改查
- hibernate基本增删改查的应用
- mysql 增删改查的基本语句
- 基本模型的增删改查
- ASP.NET基本的增删改查
- 基本的数据库增删改查
- hibernateDAO层基本的增删改查
- php最基本的增删改查
- EF最基本的增删改查
- MyBatis的增删改查基本操作
- CodeForces - 582A GCD Table (map大数操作&gcd)好题
- 算法(四)
- Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境
- 如何修改Linux开机启动logo并使其全屏显示?
- 十一、图的存储---(1)图的常用概念
- 基本单链表的增删改C++
- 对当前状况的思考————记4.30
- 数组&字符串&结构体&共用体&枚举
- 微信学习_05_图文消息
- 3D游戏引擎数学基础1--3D坐标系
- 【作品】时光计算软件(开源 Windows Android)
- jsp 语法 (一)
- quick-cocos 3.3 整理(持续更新)
- Program3_L