void Graph::insertEdge(int i, int j, double w) { if(curEdges >= e){ throw GraphException("edge number is not correct!"); } bool match = false; Edge* temp = new Edge(i,j,w); for(int k = 0; k < EdgeList.size(); ++k){ if((EdgeList[k]->vertex_i == i && EdgeList[k]->vertex_j == j) || (EdgeList[k]->vertex_i == j && EdgeList[k]->vertex_j == i)){ match = true; temp = EdgeList[k]; } } EdgeList.push_back(temp); DListNode<Vertex>* tempi = new DListNode<Vertex>(j, Vertex(i)); DListNode<Vertex>* tempj = new DListNode<Vertex>(i, Vertex(j)); tempi->getElemt().edge = temp; tempj->getElemt().edge = temp; AdjacencyList[i]->getTrailer()->setNext(tempj); AdjacencyList[i]->setTrailer(AdjacencyList[i]->getTrailer()->getNext()); AdjacencyList[j]->getTrailer()->setNext(tempi); AdjacencyList[j]->setTrailer(AdjacencyList[j]->getTrailer()->getNext()); if(!match) curEdges++; return; }
double SparseMatrix::get_element_at(int i, int j) const{ DListNode<Element> *temp = row(i).getFirst(); while(temp != NULL && temp->getElem().col != j){ temp = temp->getNext(); } if(temp != NULL && temp->getElem().col == j) return temp->getElem().val; else return 0; }
// return the list length int DoublyLinkedListLength(DoublyLinkedList& dll) { //O(n) DListNode *current = dll.getFirst(); int count = 0; while(current != dll.getAfterLast()) { count++; current = current->getNext(); //iterate } return count; }
bool DListNode::insertNode(int ID_insert,bool f1,bool f2,int succ_id)//插入结点ID_insert,且f1=true说明此结点为hull的表层结点 { //在f1=true的情况下,f2=true说明插入的结点为顺时针方向hull表层的下一结点。 DListNode* newnode=new DListNode(ID_insert); //succ_id为要插入的结点的后一结点标识,且保证这种succ_id存在,否则不做事 DListNode* next=m_ptrNext; DListNode* pre=m_ptrPre; //此双向链表从前到后组织按顺时针方向的结点 DListNode* p; if(ID_insert == this->getData()){//如果插入节点为头节点,返回false cout << "Waring: inserting head node: " << this->getData() << endl; return false; } else if(searchNode(ID_insert)){ // cout << "Warning: Node " << ID_insert << " already exit in Point:" << this->getData() << endl; //方案一:如果节点存在则不插入。 // return false; //方案二:先删除原节点再插入——效果较方案一好。 delNode(ID_insert); } if(f1) { if(f2)//将结点插在this指向的结点的后面一个位子 { // cout << "Into true true! newId = " << newnode->getData() << endl; newnode->m_ptrNext=m_ptrNext; m_ptrNext=newnode; newnode->m_ptrPre=this; next->m_ptrPre=newnode; } else//将结点插在this指向的结点的前面一个位子 { // cout << "Into true false! newId = " << newnode->getData() << endl; newnode->m_ptrNext=this; newnode->m_ptrPre=pre; m_ptrPre=newnode; pre->m_ptrNext=newnode; } } else { p=this;//绝对不在this节点的前一或后一位置?? do { if(p->getData()==succ_id) { newnode->m_ptrNext=p; newnode->m_ptrPre=p->getPre(); p->getPre()->m_ptrNext=newnode; p->m_ptrPre=newnode; return true; } p=p->getNext(); } while(p!=this); cout << "Warning: inserting " << ID_insert << " to " << this->getData() << " before " << succ_id << " failed!" << endl; return false; } return true; }
int DListNode::succ(int id)//返回id的下一个结点的id号 { DListNode * ptr; ptr=this; do{ if(ptr->getData()==id) return ptr->getNext()->getData(); ptr=ptr->getNext(); }while(ptr!=this); return this->id;//-1//如果仅一个节点则返回头节点id??? }
int DListNode::pred(int id)//返回id的前一个结点的id号 { DListNode * ptr; ptr=this; do{ if(ptr->getData()==id) return ptr->getPre()->getData(); ptr=ptr->getPre(); }while(ptr!=this); return this->id;//如果仅一个节点则返回头节点id??? }
// output operator ostream& operator<<(ostream& out, const DoublyLinkedList& dll) { //0(n) DListNode *current = dll.getFirst(); while(current != dll.getAfterLast()) { cout<<current->getElem()<<" "; current=current->getNext(); } cout<<endl; return out; }
int DListNode::getSize(void) { DListNode * ptr = this; int count = 0;//默认头节点必然存在且无法删除。 do{ count ++; // cout << count << ":ID=" << ptr->getData() << ' '; ptr = ptr->getNext(); }while(ptr!=this); // cout << endl; return count; }
double SparseMatrix::operator()(int i, int j) const{ double num; DListNode<Element> *temp = row(i).getFirst(); while(temp != NULL && temp->getElem().col != j){ temp = temp->getNext(); } if(temp != NULL && temp->getElem().col == j){ num = (temp->getElem().val); }else num=0; return num; }
// copy constructor DoublyLinkedList::DoublyLinkedList(const DoublyLinkedList& dll) //O(n) { // Initialize the list header.next = &trailer; trailer.prev = &header; DListNode *current = dll.getFirst(); while(current != dll.getAfterLast()) { insertFirst(current->getElem()); current=current->getNext(); } }
// copy constructor DoublyLinkedList::DoublyLinkedList(DoublyLinkedList& dll) { // Initialize the list header.next = &trailer; trailer.prev = &header; if (!dll.isEmpty()){ DListNode* node; node=dll.getFirst(); while (node!=dll.getAfterLast()){ insertLast(node->getElem());//insert new element node=node->getNext();//set node to next node } } }
void SparseMatrix::set_element_at(int i, int j, double n){ Element temp = Element(j,n); bool exist = false; DListNode<Element> *trav = row(i).getFirst(); while(trav != NULL && trav != row(i).getAfterLast()){ if(trav->getElem().col == j){ exist = true; trav->getElemRef()->setVal(n); } trav = trav->getNext(); } if(!exist) row(i).insertLast(temp); }
int main(int argc, char **argv) { try { DisjointSet<char> a(5); DListNode<char>* s1 = a.MakeSet(1, 'a'); DListNode<char>* s2 = a.MakeSet(2, 'b'); DListNode<char>* s3 = a.MakeSet(3, 'c'); DListNode<char>* s4 = a.MakeSet(4, 'd'); DListNode<char>* s5 = a.MakeSet(5, 'e'); cout<<"Sets: "<<endl; cout<<a<<endl; cout<<"a.Union(c, d)"<<endl; a.Union(*s3, *s4); cout<<"Sets: "<<endl; cout<<a<<endl; cout<<"a.Union(d, e)"<<endl; a.Union(*s4, *s5); cout<<"Sets: "<<endl; cout<<a<<endl; DListNode<char>* setA = a.FindSet(1); DListNode<char>* setB = a.FindSet(*s2); DListNode<char>* setD = a.FindSet(*s4); cout<<"a.find(a): "<< setA->getElem() <<endl; cout<<"a.find(d): "<< setD->getElem() <<endl; cout<<"a.Union(a, b)"<<endl; a.Union(*setA, *setB); cout<<"Sets: "<<endl; cout<<a<<endl; cout<<"a.Union(a, e) "<<endl; a.Union(*setA, *s5); cout<<"Sets: "<<endl; cout<<a<<endl; cout<<"a.find(a): "<< a.FindSet(1)->getElem() <<endl; cout<<"a.find(e): "<< a.FindSet(5)->getElem() <<endl; cout<<"listSize(a): "<< s1->getListSize() <<endl; cout<<"listSize(e): "<< a.FindSet(5)->getListSize() <<endl; } catch(exception &e) { cerr << e.what() << endl; return -1; } }
// output operator ostream& operator<<(ostream& out, const DoublyLinkedList& dll) { DListNode *node = dll.getFirst(); const DListNode *last = dll.getAfterLast(); if (dll.isEmpty()) cout<<" list is empty"<<endl; else { while(node != last) { cout << node->getElem() << " "; node = node->getNext(); } } cout << " \n"; return out; }
// assignment operator DoublyLinkedList& DoublyLinkedList::operator=(const DoublyLinkedList& dll) //O(n) { while (!isEmpty()){ removeFirst(); } header.next = &trailer; trailer.prev = &header; DListNode *current = dll.getFirst(); while(current != dll.getAfterLast()) { insertFirst(current->getElem()); current=current->getNext(); } return *this; }
double& SparseMatrix::operator()(int i, int j){ Element temp = Element(j,0); bool exist = false; DListNode<Element> *trav = row(i).getFirst(); while(trav != NULL && trav != row(i).getAfterLast()){ if(trav->getElem().col == j){ exist = true; break; } trav = trav->getNext(); } if(!exist) row(i).insertLast(temp); trav = row(i).getFirst(); while(trav != NULL && trav != row(i).getAfterLast()){ if(trav->getElem().col == j){ break; } trav = trav->getNext(); } return trav->getElemRef()->val; }
//Function to search through the PhoneBook void search(vector<DoublyLinkedList<Record>> phoneBook) { string lastname=" ", firstname=" ", UIN=" ",ph=" ",space=" "; cout<<"\nEnter the last name\n"; cin>>lastname; check_case(lastname); cout<<lastname<<endl; int num = get_index(lastname[0]); int j=0; int yum = DoublyLinkedListLength(phoneBook[num]); vector<DListNode<Record>> rec; DListNode<Record>* node; if(yum==0) { cout<<"\nThere is no person with that Last name"; search(phoneBook); } //SEARCH for LASTNAME node = phoneBook[num].getFirst(); int i=0; for(j=0;j<yum-1;j++) { if(node->getElem().get_last_name()==lastname) { rec.push_back(node->getElem()); cout<<rec[i].getElem()<<endl; i++; } node =node->getNext(); } if(i==0) { cout<<"There is no person with that last_name\n"; search(phoneBook); } else if (i==1) { cout<<"\nThe details of the person that you asked for:\n"; cout<<rec[0].getElem(); } //SEARCH for FIRSTNAME else { cout<<"\nPlease Enter the First name:\n"; cin>>firstname; check_case(firstname); vector <DListNode<Record>> rec1; string f_itr; i=0; for(j=0;j<rec.size();j++) { f_itr = rec[j].getElem().get_first_name(); if(f_itr==firstname) { i++; rec1.push_back(rec[j].getElem()); } } if(i==0) { cout<<"\nThere is no person with that first_name"; search(phoneBook); } else if (i==1) { cout<<"\nThe details of the person that you asked for:\n"; cout<<rec1[0].getElem(); } //SEARCH for UIN else { cout<<"\nPlease Enter the UIN:\n"; cin>>UIN; string UIN_itr; for(j=0;j<rec1.size();j++) { UIN_itr = rec1[j].getElem().get_UIN(); if(UIN_itr==UIN) { cout<<"\nThe details of the person that you asked for:\n"<<rec1[j].getElem(); return; } } cout << "We couldn't find anythin.\n"; search(phoneBook); } } }