C 链表冒泡排序逻辑错误

标签 c linked-list bubble-sort

下面我有一个简单的 C 链表程序,它保存一个点及其距原点的距离。我的问题是,当调用排序函数时,它无法正确排序。我花了相当多的时间使用 gdb 进行调试,但没有成功。我对 C 和处理指针比较陌生,所以如果有人可以帮助我,我将不胜感激!

我本来打算发布输出的图像,但我似乎没有足够的声誉,所以输出如下所示:

点:(3,1) - 距离:3.16228

点:(4,1) - 距离:4.12311

点:(1,1) - 距离:1.41421

点:(7,1) - 距离:7.07107

测试.c文件:

#include <stdlib.h>
#include <stdio.h>
#include "point.h"

typedef struct node
{
    Point p;
    double distance;
    struct node *next;
} node;

void insertAtFront(node **start, Point *ip);
void sort(node *start);
void swap(node *a, node *b);
void printList(node *start);

int main(int argc, char **argv)
{
    Point *insertPoint = malloc(sizeof(Point));
    node *head = NULL;

    point_set(insertPoint,7.0,1.0);
    insertAtFront(&head,insertPoint);

    point_set(insertPoint,1.0,1.0);
    insertAtFront(&head,insertPoint);

    point_set(insertPoint,4.0,1.0);
    insertAtFront(&head,insertPoint);

    point_set(insertPoint,3.0,1.0);
    insertAtFront(&head,insertPoint);

    sort(head);

    printList(head);
}

void insertAtFront(node **start, Point *ip)
{
    node *node1 = (node*)malloc(sizeof(node));
    node1->p = *ip;
    node1->next = *start;
    *start = node1;
}

void sort(node *start)
{
    Point *tp1 = malloc(sizeof(Point));
    Point *tp2 = malloc(sizeof(Point));
    int swapped;
    node *node1;
    node *node2 = NULL; 
    node *tempNode; 
    double d1;
    double d2;
    if(start == NULL)
        return;
    do
    {
        swapped = 0;
        node1 = start;
        *tp1 = node1->p;        
        tempNode = node1->next;     
        *tp2 = tempNode->p;     
        d1 = distanceFromOrigin(tp1);
        d2 = distanceFromOrigin(tp2);
        while(node1->next != node2)
        {
            if(d1 > d2)
            {
                swap(node1,node1->next);
                swapped = 1;
            }
            node1 = node1->next;
        }
        node2 = node1;
    }
    while(swapped);
}

void swap(node *a, node *b)
{
    Point *tp;
    *tp = a->p;
    a->p = b->p;
    b->p = *tp;
}

void printList(node *start)
{
    node *temp = start;
    Point *tp = malloc(sizeof(Point));
    printf("\n");
    while(temp != NULL)
    {
        *tp = temp->p;
        printf("Point: (%g,%g) - Distance: %g\n", point_getX(tp), point_getY(tp), distanceFromOrigin(tp));
        temp = temp->next;
    }
}

point.c 文件:

#include <stdio.h>
#include "point.h"
#include <math.h>

void point_translate(Point *p, double x, double y)
{
    point_set(p,(point_getX(p)+x),(point_getY(p)+y));
}

double point_distance(const Point *p1, const Point *p2)
{
    double temp1 = (point_getX(p1) - point_getX(p2));
    double temp2 = (point_getY(p1) - point_getY(p2));
    double temp3 = (temp1*temp1)+(temp2*temp2);
    double dist = sqrt(temp3);
    return dist;
}

double distanceFromOrigin(const Point *p1)
{
    double x = point_getX(p1);
    double y = point_getY(p1);
    double temp = (x*x)+(y*y);
    double dist = sqrt(temp);
    return dist;
}

point.h 文件:

#ifndef _POINT_H_
#define _POINT_H_

typedef struct Point 
{
  double x;
  double y;
} Point;

void point_translate(Point *p, double x, double y);
double point_distance(const Point *p1, const Point *p2);
double distanceFromOrigin(const Point *p1);

static inline double point_getX(const Point *p)
{
  return p->x;
}
static inline double point_getY(const Point *p)
{
  return p->y;
}
static inline Point *point_set(Point *p, double x, double y)
{
  p->x = x; 
  p->y = y;
  return p;
}

#endif

最佳答案

“swap”函数中的局部变量 tp 未初始化。你的编译器应该警告你这一点。然后你无论如何都会访问它,这是未定义的行为......

关于C 链表冒泡排序逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32424455/

相关文章:

C QueueLinkedList 字符串变量不起作用

java - 如何通过将颜色更改为红色来显示正在排序的行?

c - C中switch中定义的变量

c - 在 C 中处理多个函数中的指针

c - 线性化攻击

algorithm - 在二叉树中找到最便宜的路径?

c++ - 遍历链表插入 STL vector 值

c++ - 复制数组和冒泡排序 C++

C++ 带指针的冒泡排序函数

c - c中非常大的循环计数