/** * 算法2.21 */ Status MergeList(LinkList &La, LinkList &Lb, LinkList &Lc, int (*compare)(ElemType, ElemType)) { Link ha, hb, pa, pb, q; ElemType a, b; if (!InitList(Lc)) return ERROR; ha = GetHead(La); hb = GetHead(Lb); pa = NextPos(La, ha); pb = NextPos(Lb, hb); while (pa && pb) { a = GetCurElem(pa); b = GetCurElem(pb); if (compare(a, b) <= 0) { // a<=b DelFirst(ha, q); Append(Lc, q); pa = NextPos(La, ha); } else { // a>b DelFirst(hb, q); Append(Lc, q); pb = NextPos(Lb, hb); } } // while if (pa) Append(Lc, pa); else Append(Lc, pb); FreeNode(ha); FreeNode(hb); return OK; }
void SortPolyn(polynomail *p) { Link *p1, *p2; //采用直接插入排序 for (p1 = p->head->next; p1; p1 = p1->next) { for (p2 = p->head->next; p2 != p1; p2 = p2->next) { if (p2->data->expn == p1->data->expn) { //若后边待排序的节点的指数跟前面已排序 Link *pri; //节点的指数相同,则应该合并两项,即删除后者,并入前者 p2->data->coef += p1->data->coef; pri = PriorPos(*p, p1); DelFirst(pri, &p1); FreeNode(p1); --p->len; if (fabs(p2->data->coef) < 1e-6) { //如果合并后系数为0,那也要把这个节点删除 pri = PriorPos(*p, p2); DelFirst(pri, &p2); FreeNode(p2); --p->len; } p1 = pri; break; } else if (p1->data->expn < p2->data->expn) { Link *pri1, *pri2; pri1 = PriorPos(*p, p1); DelFirst(pri1, &p1); pri2 = PriorPos(*p, p2); InsFirst(pri2, p1); p1 = pri1; break; } } } p->tail = GetLast(*p); }
list DeleteAll(list l) { while (empty(l) == false) { l = DelFirst(l); } return l; }
main() { DList *plist = NULL; PNode p = NULL; plist = InitList(); p = InsFirst(plist,MakeNode(1)); InsBefore(plist,p,MakeNode(2)); InsAfter(plist,p,MakeNode(3)); printf("p前驱位置的值为%d\n",GetItem(GetPrevious(p))); printf("p位置的值为%d\n",GetItem(p)); printf("p后继位置的值为%d\n",GetItem(GetNext(p))); printf("遍历输出各节点数据项:\n"); ListTraverse(plist,print); printf("除了头节点该链表共有%d个节点\n",GetSize(plist)); FreeNode(DelFirst(plist)); printf("删除第一个节点后重新遍历输出为:\n"); ListTraverse(plist,print); printf("除了头节点该链表共有%d个节点\n",GetSize(plist)); DestroyList(plist); printf("链表已被销毁\n"); }
void DelCertainElement(int x) { NODE* PrevNODE=NULL;//we need to memorize a previous element for deleting if(head!=NULL) { NODE* CurrNODE; CurrNODE=head; while (CurrNODE!=NULL) { if(CurrNODE->data==x) { if(CurrNODE==head) { DelFirst(); } else { if(CurrNODE==tail) { DelLast(); } else { PrevNODE->next=CurrNODE->next; } } } PrevNODE=CurrNODE; CurrNODE=CurrNODE->next; } } }
void Bank_simulation(int CloseTime){ //银行业务模拟,统计一天内客户在银行的逗留时间 //全局变量 static Event en;//事件表 static EvenList ev;//事件 static LinkQueue q[5];//4个客户列队 static QElemType customer;//客户记录 static int TotalTime,CustomerNum;//累计逗留时间,客户数 //变量结束 EvenList p,e; OpenForDay(TotalTime,CustomerNum,en,ev,q); while(!ListEmpty(ev)){ e=GetHead(ev); DelFirst(e,p); en=GetCurElem(p); if(en.NType==0) CustomerArrived(CloseTime,CustomerNum,en,ev,q); else CustomerDeparture(en,ev,q,customer,TotalTime); if(TotalTime>CloseTime)break; } printf("%0.2f\n",(float)TotalTime/CustomerNum); }
void DeleteCertainElement(int x) { NODE *CurrNODE=head; if(head->data==x) DelFirst(); else { if(tail->data==x) { DelLast(); } else { while(CurrNODE!=NULL) { if(CurrNODE->data==x) { CurrNODE->prev->next=CurrNODE->next; CurrNODE->next->prev=CurrNODE->prev; } CurrNODE=CurrNODE->next; } } } }
list search_and_destroy(element e, list l) { list r = l; if (l == NULL) return NULL; if (detect(e, l) == false) return l; list tmp = l; if (IsEqual(head(l), e) == true) { l = DelFirst(l); return l; } while (llenght(l) != 1) { if (IsEqual(head(tail(l)), e) == true) { DeleteElement(l->next->value); tmp = l->next; l->next = l->next->next; free(tmp); return r; } l = tail(l); } return NULL; }
void DelVFirst(List *L,infotype *X) { /* I.S. List L tidak kosong */ /* F.S. Elemen pertama list dihapus. nilai info disimpan pada X */ /*dan alamat elemen pertama didealokasi */ address P; DelFirst(L,&P); (*X) = Info(P); Dealokasi(&P); }
/*** PENGHAPUSAN ELEMEN ***/ void DelVFirst(List *L, infotypeList *X) /* I.S. List L tidak kosong */ /* F.S. Elemen pertama list dihapus: nilai info disimpan pada X */ /* dan alamat elemen pertama di-dealokasi */ { addressList P; DelFirst(L, &P); *X = Info(P); Dealokasi(&P); }
Status MergeLinkList(LinkList *La, LinkList *Lb, LinkList *Lc, int (*compare)(ElemType, ElemType)) /* 已知单链表La和Lb的元素按值非递减排列 * 归并La和Lb得到新的单链表Lc, Lc的元素也按值非递减排列. */ { Link pNode_La = La->head, ha = GetHead(*La), pNode_Lb = Lb->head, hb = GetHead(*Lb); int iLen_La = La->len, iLen_Lb = Lb->len; ElemType a = 0, b = 0; Link pTemp = NULL; if (!InitList(Lc)) // Initialize link list Lc. return ERROR; while (pNode_La && pNode_Lb) { a = GetCurElem(pNode_La); b = GetCurElem(pNode_Lb); if (0 >= (*compare)(a, b)) { DelFirst(&ha, &pTemp); Append(Lc, pTemp); pNode_La = NextPos(*La, ha); } else { DelFirst(&hb, &pTemp); Append(Lc, pTemp); pNode_Lb = NextPos(*Lb, hb); } } if (pNode_La) Append(Lc, pNode_La); else Append(Lc, pNode_Lb); FreeNode(&ha); FreeNode(&hb); return OK; }
void DelP (List *L, infotype X) { /* Kamus Lokal */ address P, Prec; /* Algoritma*/ P = Search(*L,X); if (P == First(*L)) { DelFirst(L,P); } else { Prec = SearchPrec(*L,X); DelAfter(L,P,Prec); } }
void DelAnElement(int x) { node *prevNode=NULL, *currentNode; if (head!=NULL&&head->data==x) DelFirst(); prevNode=head; currentNode=head; currentNode=currentNode->next; if (head!=NULL) { while (currentNode!=NULL) { if (currentNode->data==x) { if (currentNode==head) { DelFirst(); } else { if(currentNode==tail) { DelLast(); } else { currentNode->prev->next=currentNode->next; currentNode->next->prev=currentNode->prev; free(currentNode); } } } } } }
int main() { FILE *pf_in=fopen("input.dat", "r"); FILE *pf_out=fopen("output.dat", "w"); char action[20]; int value; while(fscanf(pf_in,"%s %d", &action, &value)!=EOF) { if((strcmp(action,"AF"))==0) { AddFirst(value); } if((strcmp(action,"AL"))==0) { AddLast(value); } if((strcmp(action,"DF"))==0) { DelFirst(); } if((strcmp(action,"DL"))==0) { DelLast(); } if((strcmp(action,"DOOM_THE_LIST"))==0) { DoomList(); } if((strcmp(action,"DE"))==0) { DeleteCertainElement(value); } if((strcmp(action,"PRINT_ALL"))==0) { PrintAll(pf_out); } if((strcmp(action,"PRINT_F"))==0) { PrintFirst(value, pf_out); } if((strcmp(action,"PRINT_L"))==0) { PrintLast(value, pf_out); } } fclose(pf_in); return 0; }
void pop(list l, size_t element_n) { if (element_n == 0) //This function cannot delete the first element because it doesn't have return, so we would lost all the list; DelFirst(l); if (element_n > llenght(l)) { fprintf(stderr, "Tryng to delete an element outside the list!"); abort(); } for (size_t i = 0; i < element_n - 1; i++) l = tail(l); //The l pointer is incremented untill it points to the item before the item to delete; list cancellable = tail(l); //the pointer to the next item of list l, the item to delete, is saved in "cancellable" variable; l->next = tail(cancellable); //The pointer to the next element of list l, is set to the next item of cancellable; the list l is now not linked to the item to delete; Del(l, cancellable); //The element to delete is deleted; }
void Bank_Simulation() { Link p; OpenForDay(); // 初始化 while(!ListEmpty(ev)) { DelFirst(ev,GetHead(ev),p); en.OccurTime=GetCurElem(p).OccurTime; en.NType=GetCurElem(p).NType; if(en.NType==Qu) CustomerArrived(); // 处理客户到达事件 else CustomerDeparture(); // 处理客户离开事件 } // 计算并输出平均逗留时间 printf("顾客总数:%d, 所有顾客共耗时:%d分钟, 平均每人耗时: %d分钟\n",CustomerNum,TotalTime,TotalTime/CustomerNum); }
void DelAfter(List *L,address *Pdel,address Prec) { /* I.S. List tidak kosong. Prec adalah anggota list. F.S. Menghapus Next(Prec). Pdel adalah alamat elemen list yang dihapus */ //Kamus Lokal address Pt; //Algoritma if(Prec==Last(*L)) DelFirst(L,Pdel); else if(Prec==Prev(First(*L))) DelLast(L,Pdel); else { (*Pdel) = Next(Prec); Pt = Next(*Pdel); Next(Prec) = Pt; Prev(Pt) = Prec; } }
list Del(list l, item *ToDelete) { list r = l; size_t list_size = llenght(r); if (l == ToDelete) DelFirst(l); for (size_t i = 0; i < list_size; ++i) { if (tail(r) == ToDelete) { r->next = ToDelete->next; break; } r = tail(r); } DeleteElement(head(ToDelete)); free(ToDelete); return r; }
void DelLast (List *L,address *P) { /* I.S. List tidak kosong */ /* F.S. P adalah alamat elemen terakhir sebelum dummy pada list sebelum penghapusan */ /*Elemen list berkurang satu (mungkin menjadi kosong) */ //Kamus Lokal address Pt; //Algoritma (*P) = Last(*L); if((*P) == First(*L)) DelFirst(L,P); else { Pt = Prev(*P); Next(Pt) = First(*L); Last(*L) = Pt; Pt = First(*L); Prev(Pt) = Last(*L); } }
void Bank_Simulation() { // 银行业务模拟函数 Link p; OpenForDay(); // 初始化事件表ev且插入第1个到达事件,初始化队列 while(!ListEmpty(ev)) // 事件表ev不空 { DelFirst(ev,ev.head,p); // 删除事件表ev的第1个结点,并由p返回其指针,在bo2-6.cpp中 // if(p->data.OccurTime<50) // 输出前50分钟内发生的事件到文件d.txt中 // fprintf(fp,"p->data.OccurTime=%3d p->data.NType=%d\n",p->data.OccurTime,p->data.NType); en.OccurTime=GetCurElem(p).OccurTime; // GetCurElem()在bo2-6.cpp中,返回p->data(ElemType类型) en.NType=GetCurElem(p).NType; if(en.NType==Qu) // 到达事件 CustomerArrived(); // 处理客户到达事件 else // 由某窗口离开的事件 CustomerDeparture(); // 处理客户离开事件 } // 计算并输出平均逗留时间 printf("窗口数=%d 两相邻到达的客户的时间间隔=0~%d分钟 每个客户办理业务的时间=1~%d分钟\n",Qu,Khjg,Blsj); printf("客户总数:%d, 所有客户共耗时:%ld分钟,平均每人耗时:%d分钟,",CustomerNum,TotalTime,TotalTime/CustomerNum); printf("最后一个客户离开的时间:%d分\n",en.OccurTime); }
int main() { char strng[20]; int value; FILE *d; d= fopen("input.dat", "r"); while(fscanf(d, "%s %d", &strng, &value)!=EOF) { if (strcmp(strng,"AF")==0) AddFirst(value); if (strcmp(strng,"AL")==0) AddLast(value); if (strcmp(strng,"PRINT_ALL")==0) print_list(); if (strcmp(strng,"DF")==0) DelFirst(); if (strcmp(strng,"DL")==0) DelLast(); if (strcmp(strng,"DOOM_THE_LIST")==0) DoomTheList(); if (strcmp(strng,"DE")==0) DelAnElement(value); if (strcmp(strng,"PRINT_F")==0) PRINT_F(value); if (strcmp(strng,"PRINT_L")==0) PRINT_L(value); } fclose(d); return 0; }