簡體   English   中英

鏈表排序問題

[英]linked list sort problems

我正在嘗試使用冒泡排序對鏈接列表進行排序。 我也不能只交換節點內部的值。 我一直在畫畫,試圖找出自己在沒有幫助的情況下該如何做,但是我開始感到頭疼,無法弄清為什么這種方法不起作用。

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);
  }
}

現在,我只是試圖對兩個節點進行排序。 在我可以使它工作之后,將其循環。 由於某種原因,它甚至沒有對前兩個元素進行排序。

以下是我的一些其他功能,有助於您理解:

結構定義:

struct node {
int val;
struct node *next;

};

其他功能:

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

};

void print(struct node * head, int length){
int x = 0;
printf("tried to print\n");
//struct node*temp = head;

//while(head->next != NULL){
while (x < length + 1){
    printf("ran loop\n");
    printf("%d\n", head->val);
    printf("got number\n");
    head = head->next;
    x++;
}
printf("done with loop\n");

}

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;

}

因此,讓我們總結一下。

功能長度以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;
    }
}

而且您的scanf損壞了內存。 帶有“%1s”的scanf期望指向至少兩個字符的指針,一個用於存儲,后跟一個空字節。 因此,您需要提供兩個字符( char ans[2]; ),或者更好的解決方案,只需讀取一個字符作為字符即可。

char ans;
scanf("%c", &ans);

另外, 如前所述,如果您使用C語言進行編程, 請不要轉換malloc的返回值

如果您想知道為什么我將您的后綴++(例如x++ )更改為前綴++(例如++x ):我是C ++程序員,對於C ++,出於性能方面的原因,建議使用前綴++而不是后綴++(不是)與int或指針相關,但與諸如迭代器之類的復雜類型相關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM