mirror of
https://github.com/Kaehvaman/OAIP.git
synced 2025-01-18 16:49:11 +04:00
fixed bug in swap_by_next
This commit is contained in:
parent
60b625c6b3
commit
dad0e62672
@ -21,11 +21,13 @@ void addToHead(int value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void printNodeList() {
|
void printNodeList() {
|
||||||
|
int i = 0;
|
||||||
printf("first -> ");
|
printf("first -> ");
|
||||||
NODE* ptr = first;
|
NODE* ptr = first;
|
||||||
while (ptr != NULL) {
|
while (ptr != NULL) {
|
||||||
printf("(%d) -> ", ptr->data);
|
printf("(%d)[%d] -> ", ptr->data, i);
|
||||||
ptr = ptr->next;
|
ptr = ptr->next;
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
printf("NULL\n");
|
printf("NULL\n");
|
||||||
}
|
}
|
||||||
@ -318,10 +320,10 @@ void swap_by_next(int i, int j) {
|
|||||||
NODE* prev_ptr = first;
|
NODE* prev_ptr = first;
|
||||||
NODE* ptr = first->next;
|
NODE* ptr = first->next;
|
||||||
|
|
||||||
NODE* i_prev = prev_ptr;
|
NODE* i_prev = first;
|
||||||
NODE* i_ptr = ptr;
|
NODE* i_ptr = first;
|
||||||
NODE* j_prev = prev_ptr;
|
NODE* j_prev = first;
|
||||||
NODE* j_ptr = ptr;
|
NODE* j_ptr = first->next;
|
||||||
|
|
||||||
int index = 1;
|
int index = 1;
|
||||||
while (ptr != NULL) {
|
while (ptr != NULL) {
|
||||||
@ -347,8 +349,13 @@ void swap_by_next(int i, int j) {
|
|||||||
ptr = ptr->next;
|
ptr = ptr->next;
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
if (i == 0) {
|
||||||
|
first = j_ptr;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
i_prev->next = j_ptr;
|
||||||
|
}
|
||||||
j_prev->next = i_ptr;
|
j_prev->next = i_ptr;
|
||||||
i_prev->next = j_ptr;
|
|
||||||
NODE* tmp = i_ptr->next;
|
NODE* tmp = i_ptr->next;
|
||||||
i_ptr->next = j_ptr->next;
|
i_ptr->next = j_ptr->next;
|
||||||
j_ptr->next = tmp;
|
j_ptr->next = tmp;
|
||||||
@ -404,7 +411,7 @@ void bubbleSort_by_next() {
|
|||||||
NODE* ptrj = first;
|
NODE* ptrj = first;
|
||||||
while (ptrj->next != NULL) {
|
while (ptrj->next != NULL) {
|
||||||
if (ptrj->data > ptrj->next->data) {
|
if (ptrj->data > ptrj->next->data) {
|
||||||
swap_by_data(j, j + 1);
|
swap_by_next(j, j + 1);
|
||||||
}
|
}
|
||||||
j++;
|
j++;
|
||||||
ptrj = getNode(j);
|
ptrj = getNode(j);
|
||||||
@ -436,7 +443,8 @@ void task12() {
|
|||||||
printf("\n====== task12 ======\n");
|
printf("\n====== task12 ======\n");
|
||||||
listTemplate();
|
listTemplate();
|
||||||
printNodeList();
|
printNodeList();
|
||||||
swap_by_next(1, 5);
|
swap_by_next(0, 2);
|
||||||
|
swap_by_next(3, 5);
|
||||||
printNodeList();
|
printNodeList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user