C 链表排序问题
我正在尝试使用冒泡排序对链接列表进行排序。我也不能仅仅交换节点内的值。我一直在画画,试图在没有帮助的情况下自己解决问题,但我开始感到头痛,不明白为什么这样做行不通C 链表排序问题,c,sorting,linked-list,C,Sorting,Linked List,我正在尝试使用冒泡排序对链接列表进行排序。我也不能仅仅交换节点内的值。我一直在画画,试图在没有帮助的情况下自己解决问题,但我开始感到头痛,不明白为什么这样做行不通 void sort_ascending(struct node ** head){ int x; struct node*temp; struct node*temp2; x = length(*head)+1; //checks if more than one node is in the list if
void sort_ascending(struct node ** head){
int x;
struct node*temp;
struct node*temp2;
x = length(*head)+1; //checks if more than one node is in the list
if(x < 2){
printf("1 or less\n");
//free(temp);
return;
}
printf("longer than 1\n");
printf("%d %d\n", (*head)->val, (*head)->next->val);
if((*head)->val > (*head)->next->val){
printf("needs to sort!\n");
temp = (*head)->next->next; //sets temp to the node after the two nodes being swapped
printf("test1\n");
temp2 = (*head); //sets temp2 to the node1
printf("test2\n");
*head = (*head)->next; //changes head to point at node2 instead of node1
printf("test3\n");
(*head)->next = temp2; //sets node2 to point to node1
(*head)->next->next = temp; //sets node2 to point back into the list
printf("test4\n");
//free(temp);
}
}
})
其他职能:
void push(struct node ** headRef, int data){
struct node* newNode = malloc(sizeof(struct node)); //alocates space on heap
printf("pushed node\n");
newNode->val = data;//sets data value
printf("%d\n", newNode->val);
newNode->next = *headRef; // The '*' to dereferences back to the real head
*headRef = newNode; // ditto
})
}
}所以,让我们总结一下
函数长度被关闭1
int length(struct node* head){
int length = 0;
while (head != NULL){
++length;
head = head->next;
}
return length;
}
打印功能打印太多
void print(struct node * head, int length){
int x;
for(x = 0; x < length; ++x){
printf("%d\n", head->val);
head = head->next;
}
}
另外,如前所述,如果您使用C语言编程
如果你想知道为什么我把你的后缀+ +(比如<代码> x++<代码>)改为前缀++(比如<代码> ++x> >):我是C++程序员,对于C++,建议使用前缀+ +过后缀+ +,因为性能原因(与int或指针无关,但对于迭代器等复杂类型)。你如何测试它是否有效?在我创建了两个节点后,比如一个值为5,另一个值为6,我调用这个函数对我创建的节点进行排序。如果它能重新排列它们,我知道它能工作。我知道你了解排序的工作原理。:)我想知道1)您是如何构造节点的(向我们展示您的代码),以及2)您是在调试程序中检查值还是将值打印到屏幕上以测试其是否有效。@WernerHenze虽然您的malloc语句是真的,但OP确实声明他现在只尝试对前两个元素进行排序,稍后将添加循环。感谢malloc信息,我将删除它们。
int main(){
char ans;
int num;
struct node *head = NULL;
do {
do {
printf("Enter a number: ");
scanf("%d", &num);
push(&head, num);//Can change to append for back
printf("Do you want another num (y or n): ");
scanf("%1s", &ans);
} while (ans == 'y');
printf("Sort ascending or descending (a or d)? ");
scanf("%1s", &ans);
if(ans == 'a') sort_ascending(&head);
//else if(ans == 'd') sort_descending(&head);
print(head, length(head));
printf("Do you want to do this again (y or n)? ");
scanf("%1s", &ans);
if (ans == 'y') clear(&head);
} while (ans == 'y');
return 0;
int length(struct node* head){
int length = 0;
//struct node*temp = head;
printf("tried to find length\n");
while (head->next != NULL){
length++;
head = head->next;
}
printf("%d\n", length + 1);
return length;
int length(struct node* head){
int length = 0;
while (head != NULL){
++length;
head = head->next;
}
return length;
}
void print(struct node * head, int length){
int x;
for(x = 0; x < length; ++x){
printf("%d\n", head->val);
head = head->next;
}
}
char ans;
scanf("%c", &ans);