The chain table description of a linear table.
Node header file chainnode. h:
#ifndef CHAIN_NODE_H#defineCHAIN_NODE_H#include "ChainList.h"template <typename T>class ChainNode{//friend ChainList<T>;public:ChainNode();~ChainNode(); template <typename T> friend class ChainList;private:T data;ChainNode<T> * link;};template <typename T>ChainNode<T>::ChainNode(){}template <typename T>ChainNode<T>::~ChainNode(){}#endif
Linked List header file: chainlist. h:
# Ifndef chain_list_h # define chain_list_h # include <exception> # include "chainnode. H "using namespace STD; Template <typename T> class chainlist {public: chainlist () {head = nullptr ;}; bool isempty () const {return (0 = head)}; int length () const; int search (const T & V) const; chainlist <t> & Delete (int K, T & V); chainlist <t> & insert (int K, const T & V); void output (ostream & out) const ;~ Chainlist (); Private: chainnode <t> * head; bool find (int K, T & V) const ;}; /** find an element in a certain position * Send the value of this position to V * If the K element does not exist, false is returned; otherwise, true */template <typename T> bool chainlist is returned. <t>:: Find (int K, T & V) const {If (k <1) return false; chainnode <t> * Current = head; int Index = 1; while (current & Index! = K) {current = Current-> link; index ++;} If (current) {// v = Current-> data; return true;} return false;} found ;} /* return the length of the linked list */template <typename T> int chainlist <t>: length () const {chainnode <t> * temp = head; int Len = 0; while (temp) {temp = temp-> link; Len ++;} return Len ;} /** search value v * if it is found, the location of V is returned * otherwise 0 */template <typename T> int chainlist <t>: Search (const T & V) is returned) const {chainnode <t> * Current = head; int Index = 1; whil E (current & Current-> data! = V) {current = Current-> link; index ++;} If (current) Return Index; return 0 ;} /** delete node at a certain position * pass the deleted element value to V */template <typename T> chainlist <t> & chainlist <t> :: delete (int K, T & V) {If (! Head | K <1) Throw STD: out_of_range ("the K element does not exist! "); Chainnode <t> * Current = head; if (1 = k) {// Delete the first node head = head-> link ;} else {chainnode <t> * dnode = head; // Delete intermediate nodes first traverse to K-1 nodes for (INT Index = 1; dnode & index <k-1; index ++) {dnode = dnode-> link;} If (! Dnode) {Throw out_of_range ("the K element does not exist");} current = dnode-> link; dnode-> link = Current-> link ;} V = Current-> data; delete current; return * This ;} /** Add a node * Add a node with the value of V after the K element * because the node is added, it must be dynamically created */template <typename T> chainlist <t> & chainlist <t>:: insert (int K, const T & V) {/** if (k <0) Throw out_of_range ("k <0 is not acceptable "); chainnode <t> * P = head; For (INT Index = 1; index <K & P; index ++) {P = p-> link ;} if (k> 0 &&! P) Throw out_of_range ("the K element does not exist"); chainnode <t> * Y = new chainnode <t>; y-> DATA = V; If (k) {Y-> link = p-> link; P-> link = y;} else {Y-> link = head; head = y;} return * this; **/chainnode <t> * Y; If (k <0) {Throw out_of_range ("k <0 is not acceptable");} else if (k = 0) {chainnode <t> * Y = new chainnode <t>; y-> link = head; y-> DATA = V; head = y ;} else {chainnode <t> * P = head; For (INT Index = 1; index <K & P; index ++) {P = p-> link;} If (k> 0 &&! P) {Throw out_of_range ("the K element does not exist");} else {Y = new chainnode <t>; y-> DATA = V; y-> link = p-> link; P-> link = y;} return * This ;} /** output the linked list to the output stream */template <typename T> void chainlist <t>: output (ostream & out) const {chainnode <t> * Current = head; for (current = head; current = Current-> link) {out <Current-> data <"" ;}out <Endl ;} /** overload <*/template <class T> ostream & operator <(ostream & O Ut, const chainnode <t> & X) {X. output (out); Return out;}/** destructor * Delete all nodes in the linked list */template <typename T> chainlist <t> ::~ Chainlist () {chainnode <t> * Next; while (head) {next = head-> link; Delete head; head = next ;}# endif
Test the main function source. cpp:
#include <iostream>#include <string>#include "ChainList.h"using namespace std;int main(){ChainList<char> myChain;string testData = "1234";for(int index = 0 ; index < testData.length(); index ++){myChain.Insert(index , testData[index]);}myChain.Output(cout);char data = 'x'; myChain.Delete(2 , data);myChain.Output(cout);cout << myChain.Search('3') << endl;system("pause");}