链表排序问题

时间:2014-06-06 06:53:17

标签: 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(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 个答案:

答案 0 :(得分:0)

所以,让我们总结一下。

函数长度偏离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破坏了内存。 scanf with&#34;%1s&#34;期望一个指向至少两个字符的指针,一个用于存储,另一个用于存储空字节。所以你需要提供两个字符(char ans[2];),或者更好的解决方案,只需将一个字符作为一个字符读取。

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

另外,如上所述don't cast the return value of malloc,如果你是用C编程。

如果你想知道为什么我将你的postfix ++(比如x++)更改为前缀++(如++x):我是C ++程序员,对于C ++,建议更喜欢前缀++因为性能原因而不是postfix ++(与int或指针无关,但对于像迭代器这样的复杂类型)。