C++线性表链式存储 C++实现线性表链式存储(单链)
Eric Wang@1996 人气:0实现的功能:
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; }
加载全部内容