《正交链表结点类.doc》由会员分享,可在线阅读,更多相关《正交链表结点类.doc(10页珍藏版)》请在金锄头文库上搜索。
1、正交链表结点类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;
2、正交链表类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.r
3、ows.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.
4、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;打印正交链表templateostr
5、eam& 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.
6、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
7、)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
8、;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
9、 &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 Bin
10、aryTree: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