这篇文章主要讲解了C++如何实现线性表链式存储,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
创新互联建站是一家集网站建设,市北企业网站建设,市北品牌网站建设,网站定制,市北网站建设报价,网络营销,网络优化,市北网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
实现的功能:
1、定义了三中传入不同参数的构造函数,用于初始化创建不同的链表;
2、能实现增、删、查等基本功能;
存在的问题:
当创建一个已知大小的空链表后,链表中的数据并不为空,见下图:
下面是代码及测试结果:
singlelinklist.h
#pragma once #include "iostream" #include "exception" #include "stdlib.h" #include "malloc.h" using namespace std; //结点类 template<class T> class Node { public: T data; Node<T> *next; Node() { this->next = NULL; } Node(T data, Node<T>* next = NULL) { this->data = data; } ~Node() {}; }; //定义链表类 template<class T> class SLinkList { public: Node<T> node; //结点 Node<T>* head; //头指针 SLinkList(); //创建链表 SLinkList(int num, T elem[]); SLinkList(int num); ~SLinkList(); int LengthSLinkList(); //表长 void InsertNode(int i, T elem); //插入结点 void InsertNode(T elem); void DeleteNode(int i); //删除结点 void DeleteAllNode(); //删除表 T GetElem(int i); //按序号查找 int* GetNum(T elem); //按元素查找 void OutputList(); //输出 }; template<class T> SLinkList<T>::SLinkList() { Node<T>* p = new Node<T>(); this->head = p; cout << "finish<SLinkList()>!" << endl; } template<class T> SLinkList<T>::SLinkList(int num, T elem[]) { try { if(num < 1) throw length_error("输入初始化num参数必须大于1!"); else { Node<T>* p = new Node<T>(); this->head = p; Node<T>* temp = this->head; for (int i = 0; i < num; i++) { temp->next = new Node<T>(*(elem + i)); //这里犯了一个错,就是把elem[i]直接丢进去,()里面放的是T类型初始值,实参传进来的是T* elem[],无法用下标进行访问 temp = temp->next; } temp->next = NULL; cout << "finish<SL]inkList(int num, T elem[])>!" << endl; } } catch(length_error e) { cout << "info:" << e.what() << endl; exit(1); } } template<class T> SLinkList<T>::SLinkList(int num) { try { if (num < 1) throw length_error("输入初始化参数num必须大于1!"); else { Node<T>* p = new Node<T>(); this->head = p; Node<T>* temp = this->head; for(int i = 1; i <= num; i++) { temp->next = new Node<T>(); temp = temp->next; } temp->next = NULL; cout << "finish<SLinkList(int num)>!" << endl; } } catch(length_error e) { cout << "info:" << e.what() << endl; exit(0); } } //调用函数析构 /* template<class T> SLinkList<T>::~SLinkList() { this->DeleteAllNode(); } */ template<class T> SLinkList<T>::~SLinkList() { Node<T>* p = this->head; Node<T>* temp = p->next; Node<T>* q = NULL; while(temp) { q = temp; temp = temp->next; delete q; } delete p; delete temp; cout << "finish ~SLinkList()" << endl; } template<class T> int SLinkList<T>::LengthSLinkList() { int count = 0; Node<T>* p = this->head; Node<T>* temp = p->next; while (temp) { count++; temp = temp->next; } return count; } template<class T> void SLinkList<T>::InsertNode(int i, T elem) { try { if(i<1 || i>LengthSLinkList() + 1) throw out_of_range("输入的参数i值超出了链表的范围!"); else { if (i != LengthSLinkList()+1) //1-len之间插入 { Node<T>* Elem = new Node<T>(elem); Node<T>* temp = this->head; for (int j = 1; j < i; j++) temp = temp->next; Node<T>* p = temp->next; temp->next = Elem; Elem->next = p; } else InsertNode(elem); //在末尾插入 cout << "finish<Insert(int i, T elem)>!" << endl; } } catch(out_of_range e) { cout << "info:" << e.what() << endl; exit(0); } } template<class T> void SLinkList<T>::InsertNode(T elem) //末尾插入 { Node<T>* Elem = new Node<T>(elem); Node<T>* temp = this->head; Node<T>* p = NULL; while (temp) { p = temp; temp = temp->next; } p->next = Elem; Elem->next = NULL; cout << "finnish<InsertNode(T elem)>!" << endl; } template<class T> void SLinkList<T>::DeleteNode(int i) //返回的是被删除的值 { try { if (i<1 || i > LengthSLinkList()) throw out_of_range("输入的参数i值超出了链表的范围!"); else { Node<T>* temp = this->head; for (int j = 1; j < i; j++) temp = temp->next; Node<T> *p = temp->next; temp->next = p->next; cout << "finish<DeleteNode(int i)>!" << endl; } } catch(out_of_range e) { cout << "info:" << e.what() << endl; exit(1); } } template<class T> void SLinkList<T>::DeleteAllNode() { Node<T>* temp = this->head; int count = this->LengthSLinkList(); while(count) { DeleteNode(count); count--; } delete temp; cout << "finish<DeleteAllNode()>!" << endl; } template<class T> T SLinkList<T>::GetElem(int i) { try { if(i<1 || i > LengthSLinkList()) throw out_of_range("输入参数i值超出了链表的范围!"); else { Node<T>* temp = this->head; for(int j = 1; j <=i; j++) temp = temp->next; cout << "finish!" << endl; cout << "finish<GetElem(int i)>! " << endl; return temp->data; } } catch(out_of_range e) { cout << "info:" << e.what() << endl; exit(1); } } template<class T> int* SLinkList<T>::GetNum(T elem) { Node<T>* p = this->head; Node<T>* temp = p->next; int* count = new int[LengthSLinkList()]; int j = 0; for(int i = 1; i <= LengthSLinkList(); i++) { if(temp->data == elem) { count[j] = i; j++; } else temp = temp->next; } cout << "finish<GetNum(T elem)>!" << endl; return count; } template<class T> void SLinkList<T>::OutputList() { Node<T>* p = this->head; Node<T>* temp = p->next; cout << "output list:" ; for(int i = 1; i <= LengthSLinkList(); i++) { cout << temp->data << " " ; temp = temp->next; } cout << endl; }
main.cpp
#include "iostream" #include "singlelinklist.h" using namespace std; int main() { //Init:测试初始化构造函数 //operator:测试增、删、查 //标注行代码为异常测试,主要包括length_error和out_of_range //最后输出的是析构函数,下面代码行中未给出,见.h文件中的析构函数定义 cout << "**************************************************" << endl; cout << "test Innit<SLinkList()>" << endl; SLinkList<int> sqlinklist1; cout << "**************************************************" << endl; cout << "test Init<SLinkList(int num, T elem[])>" << endl; int num = 5; int a[] = {1, 2, 3, 4, 5}; cout << "input list:" ; for(int i = 0; i < num; i++) cout << a[i] << " "; cout << endl; SLinkList<int> sqlinklist2(num, a); cout << "length:" << sqlinklist2.LengthSLinkList() << endl; sqlinklist2.OutputList(); cout << endl; //cout << "test error" << endl; //SLinkList<int> error1(0, a); cout << "**************************************************" << endl; cout << "test Init<SLinkList(int num)>" << endl; int num1 = 5; SLinkList<int> slistlink3(num1); cout << "length:" << slistlink3.LengthSLinkList() << endl; slistlink3.OutputList(); cout << endl; //cout << "test error" << endl; //SLinkList<int> errror(0); cout << "**************************************************" << endl; cout << "test operation<InsertNode(int i, T elem)>" << endl; sqlinklist2.InsertNode(2, 1996); sqlinklist2.OutputList(); cout << endl; //cout << "test error" << endl; //error.InsertNode(0, 0); //error.InsertNode(7, 0); cout << "test operation<InsertNode(T elem)>" << endl; sqlinklist2.InsertNode(256); sqlinklist2.OutputList(); cout << endl; //cout << "test error" << endl; //error.InsertNode(0); //error.InsertNode(7); cout << "**************************************************" << endl; cout << "test operation<DeleteNode(int i)>" << endl; sqlinklist2.DeleteNode(1); sqlinklist2.OutputList(); cout << endl; //cout << "test error" << endl; //error.DeleteNode(18); //cout << "test operation<DeleteAllNode()>" << endl; //sqlinklist2.DeleteAllNode(); cout << "**************************************************" << endl; cout << "test operation<GetElem(int i)>" << endl; cout << "num '2' is:" << sqlinklist2.GetElem(2) << endl; //cout << "test error" << endl; //error.DeleteNode(18); cout << "test operation<GetNum(T elem)>" << endl; cout << "elem '2' is :" << *(sqlinklist2.GetNum(2)) << endl; cout << endl; return 0; }
看完上述内容,是不是对C++如何实现线性表链式存储有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。
本文题目:C++如何实现线性表链式存储
文章转载:https://www.cdcxhl.com/article4/jpsgie.html
成都网站建设公司_创新互联,为您提供网站设计、App开发、做网站、虚拟主机、面包屑导航、微信公众号
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联