void Combine(SLList *L1, SLList *L2, int commond) /* commond: 1表示相加, 2表示相减 */ { SLList *L = NULL; SLInit(&L); SLList *head = L1; while (head->next != NULL) { head = head->next; SLAppend(L, head->tag, head->object, head->size); } head = L2; while (head->next != NULL) { head = head->next; SLList *find = SLWalk(L, Find, head->object); if (find == NULL) /* 链表中没有此项 */ { SLList *insertPos = SLWalk(L, FindInsertPosition, head->object); if (insertPos == NULL) /* 挂在链尾 */ { SLAppend(L, head->tag, head->object, head->size); } else /* instet before insertPos */ { SLAdd(SLGetPri(L, insertPos), head->tag, head->object, head->size); } } else /*链表中已存在此项 */ { Term *oldTerm = find->object; Term *newTerm = head->object; if (commond == 1) { oldTerm->c += newTerm->c; } else if (commond == 2) { oldTerm->c -= newTerm->c; } if (oldTerm->c == 0) { SLDeleteNext(SLGetPri(L, find)); } else { SLUpdate(L, 0, oldTerm, sizeof(Term)); } } } SLWalk(L, PrintItem, NULL); SLDestory(L); }
void AddTerm(SLList *L) { Term newTerm = { 0 }; printf("Please input the c: "); scanf("%d", &(newTerm.c)); printf("Please input the e: "); scanf("%d", &(newTerm.e)); if (newTerm.c != 0) { SLList *find = SLWalk(L, Find, &newTerm); if (find == NULL) /* 该项不存在链表中, 把该项插入到链表中, 并保持链表有序 */ { SLList *insertPos = SLWalk(L, FindInsertPosition, &newTerm); /* 寻找插入位置 */ if (insertPos == NULL) { SLAppend(L, 0, &newTerm, sizeof(Term)); } else { SLAdd(SLGetPri(L, insertPos), 0, &newTerm, sizeof(Term)); } } else /* 该项已存在于链表中, 则合并此项 */ { Term *oldTerm = SLGetData(find, NULL, NULL); newTerm.c += oldTerm->c; if (newTerm.c == 0) /* 如果合并后 系数为0, 则删除该项 */ { SLDeleteNext(SLGetPri(L, find)); } else { SLUpdate(find, 0, &newTerm, sizeof(Term)); } } } }
int main(void) { BOOK Book[] = { {"Expert C Programming", "van der Linden"}, {"C Programming FAQs", "Summit"}, {"C++ Programming Language", "Stroustrup"}, {"Algorithms in C", "Sedgewick"}, {"Teach Yourself BCB", "Reisdorph"}, {"The Standard C Library", "Plauger"}, {"C++ Unleashed", "Liberty"}, {"Data Structures & Algorithms", "Lafore"}, {"C Programming Language", "Kernighan & Ritchie"}, {"Linux Unleashed", "Husain and Parker"}, {"C Unleashed", "Heathfield & Kirby"}, {"C : A Reference Manual", "Harbison & Steele"}, {"DOS Programmers Reference", "Dettmann & Johnson"}, {"C: How to Program", "Deitel & Deitel"}, {"Builder Unleashed", "Calvert"}, {"UNIX Unleashed", "Burk and Horvath"} }; SLLIST *List = NULL; SLLIST *Removed = NULL; BOOK *Data; FIELD_INFO FldInfo = { 30, 30}; size_t NumBooks = sizeof Book / sizeof Book[0]; size_t i; /* Populate the list */ for(i = 0; i < NumBooks; i++) { if(SL_SUCCESS != SLFront(&List, 0, Book + i, sizeof(BOOK))) { puts("Couldn't allocate enough memory."); SLDestroy(&List); exit(EXIT_FAILURE); } } /* Print the list */ SLWalk(List, PrintBook, &FldInfo); /* Remove one item */ Removed = List; for(i = 0; i < NumBooks / 2; i++) { Removed = Removed->Next; } Data = SLGetData(Removed->Next, NULL, NULL); printf("\nRemoving title %s\n\n", Data->Title); SLDeleteNext(Removed); /* Print the list again to confirm deletion */ SLWalk(List, PrintBook, &FldInfo); /* Destroy the list */ SLDestroy(&List); return 0; }