bool GeneralMatrix::DelCols(int in)const { int i,j,n,index; if(in<0) return FALSE; if(pHead->nCol==0) return FALSE; index = in>=pHead->nCol ? pHead->nCol-1 : in; for (j=pHead->nCol-1,n=0;n<index;j--,n++,pHead->nCol--) for (i=pHead->nRow-1;i>=0;i--) delete GetElemP(i,j); for(j=pHead->nCol-1,i=0;i<pHead->nRow;i++) GetElemP(i,j)->pRight = NULL; if(index!=in) { for (i=pHead->nRow-1;i>=0;i--) delete GetElemP(i,0); pHead->pFirst = NULL; pHead->nCol = 0; pHead->nRow = 0; } return TRUE; }
bool GeneralMatrix::DelRows(int in)const { int i,j,n,index; if(in<0) return FALSE; if(pHead->nRow==0) return FALSE; index = in>=pHead->nRow ? pHead->nRow-1 : in; for (i=pHead->nRow-1,n=0;n<index;i--,n++,pHead->nRow--) for (j=0;j<pHead->nCol;j++) delete GetElemP(i,j); for(i=pHead->nRow-1,j=0;j<pHead->nCol;j++) GetElemP(i,j)->pDown = NULL; if(index!=in) { for (j=pHead->nCol-1;j>=0;j--) delete GetElemP(0,j); pHead->pFirst = NULL; pHead->nCol = 0; pHead->nRow = 0; } return TRUE; }
// 删除第pos个位置的元素 T SLinkList::DelElem(int pos){ SLNode *p; SLNode *pPrior; if( !(p=GetElemP(pos)) ) return -1; //pos位置不合法,第pos个元素不存在 if( !(pPrior=GetElemP(pos-1)) ) return -1; //pos-1位置的指针 if( p == tail ){//删除最后一个元素 tail = pPrior; tail->next = NULL; }else{ pPrior->next = p->next; } delete p; p = NULL; length--; return head->data; }
GeneralMatrix& GeneralMatrix::operator = (const GeneralMatrix& other) { int i,j; if(other.nRow()!=this->nRow()||other.nCol()!=this->nCol()) { for (i=0;i<this->nRow();i++) for (j=0;j<this->nCol();j++) GetElemP(i,j)->val = ERRORVAL; return (*this); } for(i=0;i<this->nRow();i++) for(j=0;j<this->nCol();j++) GetElemP(i,j)->val = other.GetElem(i,j); return (*this); }
ELEMTYPE& SMatrix::GetElem(int iRow,int iCol)const { ElemNode *p; p = GetElemP(iRow,iCol); if(p==NULL) throw ERRORVAL; return p->val; }
ELEMTYPE& GeneralMatrix::GetElem(int iRow,int iCol)const { ElemNode *p; ELEMTYPE val = ERRORVAL; p = GetElemP(iRow,iCol); if(p==NULL) return val; return p->val; }
Status ListDelete(DuLinkList L,int i,ElemType *e) { /* 删除带头结点的双链循环线性表L的第i个元素,i的合法值为1≤i≤表长+1 */ DuLinkList p; if(i<1||i>ListLength(L)) /* i值不合法 */ return ERROR; p=GetElemP(L,i); /* 在L中确定第i个元素的位置指针p */ if(!p) /* p=NULL,即第i个元素不存在 */ return ERROR; *e=p->data; p->prior->next=p->next; p->next->prior=p->prior; free(p); return OK; }
static Status ListDelete(DuLinkList L,int i,ElemType &e) // 算法2.19 { // 删除带头结点的双链循环线性表L的第i个元素,i的合法值为1≤i≤表长+1 DuLinkList p; if(i<1||i>ListLength(L)) // i值不合法 return ERROR; p=GetElemP(L,i); // 在L中确定第i个元素的位置指针p if(!p) // p=NULL,即第i个元素不存在 return ERROR; e=p->data; p->prior->next=p->next; p->next->prior=p->prior; free(p); return OK; }
GeneralMatrix::~GeneralMatrix() { ElemNode *p; int i,j; p = pHead->pFirst; if(p==NULL) return; for (i=pHead->nRow-1;i>=0;i--) for (j = pHead->nCol-1;j>=0;j--) delete GetElemP(i,j); delete pHead; }
bool GeneralMatrix::AddRows(int in,ELEMTYPE Val)const { int i,j,index=0; ElemNode *p,*pnew,*pL; if (in==0) return FALSE; if (in<0) return DelRows(in*-1); if(pHead->pFirst==NULL) { this->pHead->pFirst = new ElemNode; if(pHead->pFirst == NULL) return FALSE; pHead->nRow = 1; pHead->nCol = 1; pHead->pFirst->pDown = NULL; pHead->pFirst->pRight = NULL; pHead->pFirst->val = Val; index = 1; } i = pHead->nRow-1; for(;index<in;index++,i++,pHead->nRow++) for(pL=NULL,j=0;j<pHead->nCol;j++) { p = GetElemP(i,j); pnew = new ElemNode; if(pnew==NULL) return FALSE; pnew->pDown = NULL; pnew->pRight = NULL; pnew->val = Val; if(pL!=NULL) pL->pRight = pnew; pL = pnew; p->pDown = pnew; } return TRUE; }
bool GeneralMatrix::AddCols(int in,ELEMTYPE Val)const { int i,j,index=0; ElemNode *p,*pnew,*pU; if (in==0) return FALSE; if (in<0) return DelCols(in*-1); if(pHead->pFirst==NULL) { pHead->pFirst = new ElemNode; if(pHead->pFirst == NULL) return FALSE; pHead->nRow = 1; pHead->nCol = 1; pHead->pFirst->pDown = NULL; pHead->pFirst->pRight = NULL; pHead->pFirst->val = Val; index = 1; } j = pHead->nCol-1; for(;index<in;index++,j++,pHead->nCol++) for(pU=NULL,i=0;i<pHead->nRow;i++) { p = GetElemP(i,j); pnew = new ElemNode; if(pnew==NULL) return FALSE; pnew->pDown = NULL; pnew->pRight = NULL; pnew->val = Val; if(pU!=NULL) pU->pDown = pnew; pU = pnew; p->pRight = pnew; } return TRUE; }
Status ListInsert(DuLinkList L,int i,ElemType e) { /* 在带头结点的双链循环线性表L中第i个位置之前插入元素e,i的合法值为1≤i≤表长+1 */ DuLinkList p,s; if(i<1||i>ListLength(L)+1) /* i值不合法 */ return ERROR; p=GetElemP(L,i-1); /* 在L中确定第i-1个元素的位置指针p */ if(!p) /* p=NULL,即第i-1个元素不存在 */ return ERROR; s=(DuLinkList)malloc(sizeof(DuLNode)); if(!s) return OVERFLOW; s->data=e; /* 在第i-1个元素之后插入 */ s->prior=p; s->next=p->next; p->next->prior=s; p->next=s; return OK; }
static Status ListInsert(DuLinkList L,int i,ElemType e) // 改进算法2.18 { // 在带头结点的双链循环线性表L中第i个位置之前插入元素e,i的合法值为1≤i≤表长+1 DuLinkList p,s; if(i<1||i>ListLength(L)+1) // i值不合法 return ERROR; p=GetElemP(L,i-1); // 在L中确定第i-1个元素的位置指针p if(!p) // p=NULL,即第i-1个元素不存在 return ERROR; s=(DuLinkList)malloc(sizeof(DuLNode)); if(!s) return OVERFLOW; s->data=e; // 在第i-1个元素之后插入 s->prior=p; s->next=p->next; p->next->prior=s; p->next=s; return OK; }
SMatrix& SMatrix::operator -= (const SMatrix& other) { int i,j; if(other.nRow()!=this->nRow()||other.nCol()!=this->nCol()) { for (i=0;i<this->nRow();i++) for (j=0;j<this->nCol();j++) GetElemP(i,j)->val = ERRORVAL; return *this; } for(i=0;i<this->nRow();i++) for(j=0;j<this->nCol();j++) this->GetElem(i,j) -= other.GetElem(i,j); return *this; }