chunklist——实现链表单节点存储多个数据

来源:互联网 发布:java 坐标 编辑:程序博客网 时间:2024/06/06 07:18

自己动手实现了单链表,链表的节点中能够存储变长数据,在实现中借用了STL的vector来实现。并且实现了一些单链表的简单的接口操作,如果您感觉代码有任何的问题,欢迎大家能够批评指正,共同学习进步。

下面是头文件的代码:

#ifndef CHUNKLIST_H_#define CHUNKLIST_H_#include <vector>#include <iostream>#include <iterator>#define MAXLEN 1000template<typename T>class Chunklist{private:struct LNode{std::vector<T> value;LNode* next;};LNode* header;public:Chunklist(): header(nullptr){}~Chunklist(){while (header != nullptr){LNode* tempNode = header->next;delete header;header = tempNode;}}//insert one node into the list,followed by the chunksizevoid insertNode(T* nodeData,size_t chunksize);//get the header of the list so we can traverse the listLNode* getHeader();//deleteNodebool deleteNode(LNode* delNOde);//find the first node have the keydata in the listLNode* searchNode(T keydata){LNode* dstNode = this->header;std::vector<T>::iterator findit = find(dstNode->value.begin(), dstNode->value.end(),keydata);while (findit == dstNode->value.end()){dstNode = dstNode->next;if (dstNode == nullptr){return nullptr;}findit = find(dstNode->value.begin(), dstNode->value.end(),keydata);}return dstNode;}//show the detail of the listvoid display();};template<typename T>void Chunklist<T>::insertNode(T* nodeData, size_t chunksize){LNode* newNode = new LNode;for (size_t i = 0; i < chunksize; i++){newNode->value.push_back(nodeData[i]);}if (header == nullptr){header = newNode;newNode->next = nullptr;}else    {//put the newNode into the tail of the listLNode* temp = new LNode;temp = header;while (temp->next){temp = temp->next;}temp->next = newNode;newNode->next = nullptr;}}template<typename T>bool Chunklist<T>::deleteNode(LNode* delNode){if (delNode == nullptr){return false;}LNode* tempNode = header;if (header == delNode){header = header->next;delete tempNode;return true;}while (tempNode->next != delNode){tempNode = tempNode->next;if (tempNode == nullptr){return false;}}if (delNode->next != nullptr){tempNode->next = delNode->next;}else{tempNode->next = nullptr;}delete delNode;return true;}//experience two kind of function define,it is weiredtemplate<typename T>typename Chunklist<T>::LNode* Chunklist<T>::getHeader(){return header;}template<typename T>void Chunklist<T>::display(){LNode* tempNode = header;int nodeNo = 0;while (tempNode != nullptr){std::cout << "the"<< nodeNo+1 <<" node's value is: ";for (size_t i = 0; i < tempNode->value.size(); i++){std::cout << tempNode->value[i]<<"  ";}nodeNo++;std::cout << std::endl;tempNode = tempNode->next;}}#endif

然后就是测试文件,简单的测试了一下。


#include "stdafx.h"#include "chunklist.h"int main(){Chunklist<int> mylist;int array[10][10];int * array_one_data = new int(8);int array_more_data[20] = {1,2,3,4,5,6,7,8,9,7,8,9,1,2,3,4,5,6,7,3};int numer;for (int j = 0; j < 10; j++){for (int i = 0; i < 10; i++){array[j][i] = j;}}mylist.insertNode(array[1],10);mylist.insertNode(array[2],10);mylist.insertNode(array[3],10);mylist.insertNode(array[4],10);mylist.insertNode(array_more_data,20);mylist.insertNode(array_one_data,1);numer = mylist.deleteNode(mylist.searchNode(8));mylist.display();delete array_one_data;system("pause");return 0;}


原创粉丝点击