数据结构与算法分析——C语言描述3.3

来源:互联网 发布:期货软件哪个好 编辑:程序博客网 时间:2024/04/29 17:05

通过只调整指针(而不是数据)来交换两个相邻的元素

//3.3 a.单链表void OneListSwapByAdd(ElemType B, ElemType C, List L) {Position a_pos, b_pos, c_pos;b_pos = Find(B, L);c_pos = Find(C, L);a_pos = FindPrevious(B, L);Position Tmp = (List)malloc(sizeof(Node));Tmp->next = a_pos->next;a_pos->next = b_pos->next;b_pos->next = c_pos->next;c_pos->next = Tmp->next;}//3.3 b.双链表void TwoListSwapByAdd(ElemType X1, List L, ElemType X2, List P) {List Tmp = (List)malloc(sizeof(Node));List a1, b1, a2, b2;a1 = FindPrevious(X1, L);a2 = FindPrevious(X2, P);b1 = Find(X1, L);b2 = Find(X2, P);Tmp->next = b1->next;b1->next = b2->next;a2->next = b1;b2->next = Tmp->next;a1->next = b2;}

int main(){List L,P;ElemType a, b;L = CreatedList();P = CreatedList();PrintList(L);PrintList(P);printf("请输入需要交换的两个数:");scanf_s("%d%d", &a, &b);TwoListSwapByAdd(a, L, b, P);PrintList(L);PrintList(P);    return 0;}


阅读全文
0 0
原创粉丝点击