数据结构C++,线性表学习
#include <iostream> #include <sstream> #include <algorithm> #include <iterator> #include <cmath> template<class T> struct chainNode{ T element; chainNode<T> *next; chainNode() {} chainNode(const T& element) { this->element = element; } chainNode(const T& element, chainNode<T>* next) { this->element = element; this->next = next; } }; template<class T> class linearList{ public: virtual ~linearList() {} virtual bool empty() const = 0; virtual int size() const = 0; virtual T& get(int theIndex) const = 0; virtual int indexOf(const T& theElement) const = 0; virtual void erase(int theIndex) = 0; virtual void insert(int theIndex, const T& theElement) = 0; virtual void output(std::ostream& out) const = 0; }; template<class T> class chain:public linearList<T> { public: chain(int initialCapacity = 10); chain(const chain<T>&); ~chain(); bool empty() const {return listSize == 0;} int size() const {return listSize;} T& get(int theIndex) const; int indexOf(const T& theElement) const; void erase(int theIndex); void insert(int theIndex, const T& theElement); void output(std::ostream& out) const; protected: void checkIndex(int theIndex) const; chainNode<T>* firstNode; int listSize; }; template<class T> chain<T>::chain(int initialCapacity) { if(initialCapacity < 1) { std::ostringstream s; s<<"Initial capacity = "<<initialCapacity<<" must be > 0"; return; } firstNode = NULL; listSize = 0; } template<class T> chain<T>::chain(const chain<T>& theList) { listSize = theList.listSize; if(listSize == 0) { firstNode = NULL; return; } chainNode<T>* sourceNode = theList.firstNode; firstNode = new chainNode<T>(sourceNode->element); sourceNode = sourceNode->next; chainNode<T>* targetNode = firstNode; while(sourceNode != NULL) { targetNode->next = new chainNode<T>(sourceNode->element); targetNode = targetNode->next; sourceNode = sourceNode->next; } targetNode->next = NULL; } template<class T> chain<T>::~chain() { while(firstNode != NULL) { chainNode<T>* nextNode = firstNode->next; delete firstNode; firstNode = nextNode; } } template<class T> void chain<T>::checkIndex(int theIndex) const { if(theIndex<0 || theIndex>=listSize) { std::ostringstream s; s<<"index = "<<theIndex<<" size = "<<listSize; return; } } template<class T> T& chain<T>::get(int theIndex) const { checkIndex(theIndex); chainNode<T>* currentNode = firstNode; for(int i=0; i<theIndex; i++) { currentNode = currentNode->next; } return currentNode->element; } template<class T> int chain<T>::indexOf(const T& theElement) const { chainNode<T>* currentNode = firstNode; int index=0; while(currentNode != NULL && currentNode->element != theElement) { currentNode = currentNode->next; index++; } if(currentNode == NULL) return -1; else return index; } template<class T> void chain<T>::erase(int theIndex) { checkIndex(theIndex); chainNode<T>* deleteNode; if(theIndex == 0) { deleteNode = firstNode; firstNode = firstNode->next; } else { chainNode<T>* p = firstNode; for(int i=0; i<theIndex-1; i++) p = p->next; deleteNode = p->next; p->next = p->next->next; } listSize--; delete deleteNode; } template<class T> void chain<T>::insert(int theIndex, const T& theElement) { if(theIndex<0 || theIndex>listSize) { std::ostringstream s; s<<"index = "<<theIndex<<" size = "<<listSize; return; } if(theIndex == 0) { firstNode = new chainNode<T>(theElement, firstNode); } else { chainNode<T>* p=firstNode; for(int i=0; i<theIndex; i++) p = p->next; p->next = new chainNode<T>(theElement, p->next); } listSize++; } template<class T> void chain<T>::output(std::ostream& out) const { for(chainNode<T>* currentNode=firstNode; currentNode != NULL; currentNode = currentNode->next) out<<currentNode->element<<" "; } template<class T> std::ostream& operator<<(std::ostream& out, const chain<T>& x) { x.output(out); return out; } int main() { int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; chain<int> mychain(10); for(int i=0; i<sizeof(a)/sizeof(a[0]); i++) { std::cout<<i<<": "<<a[i]<<"\n"; mychain.insert(0, a[i]); } std::cout<<mychain; return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现