int SLAppend(SLList *L, int tag, void *object, size_t size) { int result = SL_SUCCESS; SLList *endSeeker = L; while (endSeeker->next != NULL) { endSeeker = endSeeker->next; } result = SLAdd(endSeeker, tag, object, size); return result; }
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 main() { int i; SLType SL; DATA data; DATA *pdata; printf("顺序表操作演示!\n"); SLInit(&SL); printf("初始化顺序表完成!\n"); do{ printf("输入添加的节点(学号 姓名 年龄):"); fflush(stdin); scanf("%s %s %d",&data.key,&data.name,&data.age); if(data.age) { if(!SLAdd(&SL,data)) { break; } } else { break; } }while(1); printf("\n顺序表中的结点顺序为:\n"); SLAll(&SL); fflush(stdin); printf("\n要取出节点的序号:"); scanf("%d",&i); pdata = SLFindByNum(&SL,i); if(pdata) { printf("第%d个结点为:(%s,%s,%d)\n",i,pdata->key,pdata->name,pdata->age); } }
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)); } } } }