previously learned, the data description method has a formulation description, linked list description, indirect addressing, and analog pointers, which have been presented in the form of the formulation description and the list description through the code, and now post the code of the introduction addressing. The introduction of the address is a mixture of the formulation description and the list of the description of a bit, using a pointer table to record the location of the data, the pointer table is equivalent to an array, so that at the time of insertion, deletion, where the position of the data is not changed, but just the pointer table point has changed, At the same time, many operations can be manipulated as a formulation through the complexity of O (1). Paste My code below:
Please refer to the previous code for Exception.h:
#ifndef _indirect_h_#define _INDIRECT_H_#include "Exception.h"#include <iostream>Template<classT>classindirectlist{ Public: Indirectlist (intMaxlistsize=Ten); ~indirectlist ();/ * If length=0, the list is empty * / BOOLIsEmpty ()Const{returnLength = =0;}/ * Returns the length of the list * / intLength ()Const{returnLength;}/ * See if you can find the K element, and then assign the value to x * * BOOLFind (intk,t& x)Const;/ * Locate the element where the element is X * / intSearch (Constt& x)Const;/ * Remove the k element from the list and assign to x*/indirectlist<t>& Delete (intk,t& x);/ * Insert Element x*/at the K-position of the linked listindirectlist<t>& Insert (intKConstt& x);/ * Print out the entire list * / voidPrint ();Private:intLength//The length of the list intMaxSize;//The maximum length of the listT **table;//Analog pointer table};Template<classT>indirectlist<t>::indirectlist (intMaxlistsize) { This->maxsize = maxlistsize;//Get maximum lengthTable =NewT*[maxsize];//Initialize analog pointerLength =0;//Set current length is 0}Template<classT>indirectlist<t>::~indirectlist () { for(inti =0; i < length;i++)DeleteTable[i];//delete analog pointer Delete[] table;}Template<classT>BOOLIndirectlist<t>::find (intK, T &x)Const{if(K <1|| K > Length)ThrowOutofbounds ();//Out of boundsx = *table[k-1];return true;}Template<classt>indirectlist<t>& indirectlist<t>::D elete (intK, T &x) {if(Find (k,x)) { for(inti = K;i < length;i++) {/ * Elements after k elements move forward one * /table[i-1] = Table[i]; } length--;//Current length minus one return* This; }Else{ThrowOutofbounds (); }}Template<classt>indirectlist<t>& Indirectlist<t>::insert (intKConstT &x) {if(K <0|| K > Length)ThrowOutofbounds ();//cross-border if(length = = MaxSize)ThrowNomem ();//The maximum length has been reached for(inti = length-1; I >= k;i--) {/ * Elements after the k element move backward one * /table[i+1] = Table[i]; }/** * Create a new indirect address * /TABLE[K] =NewT *TABLE[K] = x; length++;return* This;}Template<classT>intIndirectlist<t>::search (ConstT &x)Const{ for(inti =0; i < length;i++) {if(*table[i] = = x) {returni+1; } }return-1;}/** * Print out the contents of the entire list */Template<classT>voidIndirectlist<t>::p rint () { for(inti =0; i < length;i++) {STD::cout<< *table[i] <<" "; }STD::cout<<STD:: Endl;}#endif
The code is relatively simple, but one of the more difficult to understand is
t** table, where table is pointing to t**,
table points to T, which is the address of the data T, so that table points to the address of the address of the data, an indirect addressing, which is similar to the computer composition principle of the introduction addressing.
Have a good experience, you will understand!! Come on!!
Indirect addressing for C + + implementations