资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
正交链表结点类enum TagFieldELEMENT,HEAD;templatestruct Entryint row,col;T value;templatestruct EntryNodeEntryNode *right,*down;TagField tag;unionEntry element;EntryNode* next;EntryNode(TagField,Entry *); ;templateEntryNode :EntryNode(TagField tf,Entry *x)tag=tf;if(tag) right=down=next=this;else element=*x;正交链表类templateclass LinkTriplepublic:LinkTriple ()head=NULL; LinkTriple ();private:EntryNode* head;friend istream& operator(istream &,LinkTriple &);friend ostream& operator(ostream &,LinkTriple &);建立正交链表templateistream& operator(istream & in,LinkTriple & a)Entry s;int max;cout”ROWS,COLS,NONZEROS:”s.rows.cols.value;if(s.rows.col) max=s.row;else max=s.col;a.head=new EntryNode(ELEMENT,&s);if(!max)a.head-right=a.head;return in;EntryNode *hd=new EntryNode *max;for(int i=0;imax;i+) hdi=new EntryNode(HEAD,0);int currentRow=0;EntryNode *last=hd0;cout”row,col,value:”endl;for(i=0;is.value;i+)Entry x;inx.rowx.colcurrentRow)last-right=hdcurrentRow;currentRow=x.row;last=hdcurrentRow;last=last-right=new EntryNode(ELEMENT,&x);hdx.col-next=hdx.col-next-down=last;last-right= hdcurrentRow;for(i=0;inext-down=hdi;for(i=0;inext=hdi+1;hdmax-1-next=a.head;a.head-right= hd0;delete hd;return in;打印正交链表templateostream& operator(ostream & out,LinkTriple & a)EntryNode *p,*q;Entry s;out”SparseLinkTriple:”element;out”ROW=”s.row”,COLS=”s.col” NONZEROS=”s.valueright;while(p!=a.head)q=p-right;while(q!=p)s=q-element;outs.row,s.col,s.valueright;outnext;return out;Void main()LinkTriple a;Cina;cout=pos,j-)strj+strlen(p.str)=strj;for(int j=0,j=strlen(p.str)-1,j+)strj+pos=p.strj;strlength=0;return *this;int String: Index(int k,String &p)char *t=p.str,*p=str+k;if(klength-1) throw OutOfBounds;int pos=1;while(*t!=0&*s!=0)if(*s+!=*t+)t=p.str;s=str+(+pos);if(*t=0) return pos;return -1;int String: IndexKMP(int k,String &p)if(klength-1) throw OutOfBounds;int i=k;j=0;n=length,m=p.length;while(in&jm)if(j=-1|stri=p.strj)i+;j+;else j=p.fj;return(j=m)?i-m:-1);二叉树结点类temple struct BTNodeBTNode()lChild=rChild=NULL;BTNode(const T& x)element=x;lChild=rChild=NULL;BTNode(const &x,BTNode * l,BTNode * r)element=x;lChild=l;rChild=r;T element;BTNode * lChild,*rChild;二叉树类temple class BinaryTreepublic:BinaryTree( )root=NULL;BinaryTree( )Clear();bool IsEmpty( )const;bool Root(T &x)const;void MakeTree(const T &e, BinaryTree&left, BinaryTree&right);void BreakTree(T &e, BinaryTree&left, BinaryTree&right);void PreOrder(void(*Visit)(T &x);void TneOrder(void(*Visit)(T &x);void PostOrder(void(*Visit)(T &x);void Clear();protected:BTNode * root;private:void Clear (BTNode *t);void PreOrder(void(*Visit)(T &x), BTNode * t);void TneOrder(void(*Visit)(T &x), BTNode * t);void PostOrder(void(*Visit)(T &x) BTNode * t);部分二叉树运算temple bool BinaryTree:Root(T& x)constif(root)x=root-element;return true;else return false;temple void BinaryTree: MakeTree(const T &x, BinaryTree&left, BinaryTree&right)if(root|&left=&right) return;root=new BTNode(x,left.root,right.root);left.root=right.root-NULL;temple void BinaryTree: BreakTree(const T &x, BinaryTree&left, BinaryTree&right)if(!root|&left=&right|left.root|right.root)return;x=root-element;left.root=root-lChild; ight.root=root-rChild;delet root; root
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号