1. Sequential storage
//List.h#ifndef List_h_#defineList_h_#defineMAXSIZE 100typedefstruct_poly{intA; intN; BOOL operator==(_poly e) {if(A = = E.a && N = =E.N)return true; Else return false; }}poly;#defineElemtype Polytypedefstruct_node{Elemtype data[maxsize]; intLast ;} Node;classlist{Private: Node*data; Public: List (); ~List (); voidInsert (Elemtype E,intindex); voidRemove (intindex); intFind (Elemtype e); Elemtype findkth (intk); intLength ();};#endif//List.cpp#include"List.h"#include<iostream>list::list () {Data=NewNode (); Data->last =-1;} List::~List () {Deletedata;}voidList::insert (Elemtype E,intindex) { if(Data->last = = MAXSIZE-1) {Std::cout<<"the watch is full."<<Std::endl; return; } if(Index <0|| Index > (data->last +1) ) {Std::cout<<"Add location not legal"<<Std::endl; return; } for(inti = data->last; I >= index; i--) {Data->data[i +1] = data->Data[i]; } Data->data[index] =e; Data->last++;}voidList::remove (intindex) { if(Index <0|| Index > data->Last ) {Std::cout<<"Remove location not and method"<<Std::endl; return; } for(inti = index; I < data->last; i++) {Data->data[i] = data->data[i +1]; } Data->last--;}intList::find (Elemtype e) { for(inti =0; I < data->last; i++) { if(Data->data[i] = =e)returni; } return-1;} Elemtype list::findkth (intk) { if(K <0|| K > data->Last ) {Std::cout<<"find location is not legal"<<Std::endl; } returnData->data[k];}intlist::length () {return(Data->last +1);}
2, chain-type storage
//PList.h#ifndef Plist_h_#definePlist_h_#defineMAXSIZE 100typedefstruct_poly{intA; intN; BOOL operator==(_poly e) {if(A = = E.a && N = =E.N)return true; Else return false; }}poly;#defineElemtype Polytypedefstruct_pnode{elemtype data; _pnode*Next;} Pnode;classplist{Private: Pnode*head; Public: PList (); ~PList (); voidInsert (Elemtype E,intindex); voidRemove (intindex); intFind (Elemtype e); Elemtype findkth (intk); intLength ();};#endif//PList.cpp#include"PList.h"#include<iostream>PList::P list () {head=NewPnode (); Head->next =NULL;} PList::~PList () {Deletehead;}voidPlist::insert (Elemtype E,intindex) {Pnode*p =Head; Pnode*s =NewPnode (); S->data =e; S->next =NULL; if(Index >Length ()) Std::cout<<"Incorrect insertion position"<<Std::endl; inti =0; while(P && i <index) {P= p->Next; I++; } s->next = p->Next; P->next =s;}voidPlist::remove (intindex) {Pnode*p =Head; Pnode*s =NewPnode (); if(Index >Length ()) Std::cout<<"Remove Location Error"<<Std::endl; inti =0; while(P && i <index) {P= p->Next; I++; } s= p->Next; P->next = s->Next; Deletes;}intPlist::find (Elemtype e) {Pnode*p =Head; inti =0; while(p) {if(P->data = =e)returni; I++; P= p->Next; } return-1;} Elemtype plist::findkth (intk) {Pnode*p =Head; if(K <0|| K >Length ()) {Std::cout<<"find location is not legal"<<Std::endl; } inti =0; while(P && i <k) {p= p->Next; I++; } returnP->next->data;}intplist::length () {Pnode*p =Head; inti =0; while(p) {i++; P= p->Next; } returni; }
3. Test Cases
//main.cpp#include <iostream>#include<stdlib.h>#include"PList.h"using namespacestd;intMain () {PList L; for(inti =0; I <5; i++) {Poly T={i, i}; L.insert (t, i); }; cout<<"Last :"<< l.length () <<Endl; Poly e= {2,2 }; cout<<"Find return:"<< L.find (e) <<Endl; for(inti =0; I <4; i++) {cout<< l.findkth (i). a <<"X"<< l.findkth (i). N <<" + "; } cout<< l.findkth (4). A <<"X"<< l.findkth (4). N <<Endl; System ("Pause"); return 0;}
Data structure (1)--linear structure