本文实例为大家分享了C++实现线性表链式存储的具体代码,供大家参考,具体内容如下
实现的功能:
1、定义了三中传入不同参数的构造函数,用于初始化创建不同的链表;
2、能实现增、删、查等基本功能;
存在的问题:
当创建一个已知大小的空链表后,链表中的数据并不为空,见下图:
下面是代码及测试结果:
singlelinklist.h
#pragma once #include "iostream" #include "exception" #include "stdlib.h" #include "malloc.h" using namespace std; //结点类 templateclass Node { public: T data; Node *next; Node() { this->next = NULL; } Node(T data, Node * next = NULL) { this->data = data; } ~Node() {}; }; //定义链表类 template class SLinkList { public: Node node; //结点 Node * 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 SLinkList ::SLinkList() { Node * p = new Node (); this->head = p; cout <<"finish !" < SLinkList ::SLinkList(int num, T elem[]) { try { if(num <1) throw length_error("输入初始化num参数必须大于1!"); else { Node * p = new Node (); this->head = p; Node * temp = this->head; for (int i = 0; i next = new Node (*(elem + i)); //这里犯了一个错,就是把elem[i]直接丢进去,()里面放的是T类型初始值,实参传进来的是T* elem[],无法用下标进行访问 temp = temp->next; } temp->next = NULL; cout <<"finish !" < SLinkList ::SLinkList(int num) { try { if (num <1) throw length_error("输入初始化参数num必须大于1!"); else { Node * p = new Node (); this->head = p; Node * temp = this->head; for(int i = 1; i <= num; i++) { temp->next = new Node (); temp = temp->next; } temp->next = NULL; cout <<"finish !" < SLinkList ::~SLinkList() { this->DeleteAllNode(); } */ template SLinkList ::~SLinkList() { Node * p = this->head; Node * temp = p->next; Node * q = NULL; while(temp) { q = temp; temp = temp->next; delete q; } delete p; delete temp; cout <<"finish ~SLinkList()" < int SLinkList ::LengthSLinkList() { int count = 0; Node * p = this->head; Node * temp = p->next; while (temp) { count++; temp = temp->next; } return count; } template void SLinkList ::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 * Elem = new Node (elem); Node * temp = this->head; for (int j = 1; j next; Node * p = temp->next; temp->next = Elem; Elem->next = p; } else InsertNode(elem); //在末尾插入 cout <<"finish !" < void SLinkList ::InsertNode(T elem) //末尾插入 { Node * Elem = new Node (elem); Node * temp = this->head; Node * p = NULL; while (temp) { p = temp; temp = temp->next; } p->next = Elem; Elem->next = NULL; cout <<"finnish !" < void SLinkList ::DeleteNode(int i) //返回的是被删除的值 { try { if (i<1 || i > LengthSLinkList()) throw out_of_range("输入的参数i值超出了链表的范围!"); else { Node * temp = this->head; for (int j = 1; j next; Node *p = temp->next; temp->next = p->next; cout <<"finish !" < void SLinkList ::DeleteAllNode() { Node * temp = this->head; int count = this->LengthSLinkList(); while(count) { DeleteNode(count); count--; } delete temp; cout <<"finish !" < T SLinkList ::GetElem(int i) { try { if(i<1 || i > LengthSLinkList()) throw out_of_range("输入参数i值超出了链表的范围!"); else { Node * temp = this->head; for(int j = 1; j <=i; j++) temp = temp->next; cout <<"finish!" < ! " < data; } } catch(out_of_range e) { cout <<"info:" < int* SLinkList ::GetNum(T elem) { Node * p = this->head; Node * 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 !" < void SLinkList ::OutputList() { Node * p = this->head; Node * temp = p->next; cout <<"output list:" ; for(int i = 1; i <= LengthSLinkList(); i++) { cout < data <<" " ; temp = temp->next; } cout <
main.cpp
#include "iostream" #include "singlelinklist.h" using namespace std; int main() { //Init:测试初始化构造函数 //operator:测试增、删、查 //标注行代码为异常测试,主要包括length_error和out_of_range //最后输出的是析构函数,下面代码行中未给出,见.h文件中的析构函数定义 cout <<"**************************************************" <" < sqlinklist1; cout <<"**************************************************" < " < sqlinklist2(num, a); cout <<"length:" < error1(0, a); cout <<"**************************************************" < " < slistlink3(num1); cout <<"length:" < errror(0); cout <<"**************************************************" < " < " < " < " < " < " <
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。