C链接列表冒泡排序逻辑错误

时间:2015-09-06 14:12:06

标签: c linked-list bubble-sort

下面我在C中有一个简单的链表程序,它包含一个点及其与原点的距离。我的问题是,当调用sort函数时,它没有正确排序。我花了很多时间用gdb调试,但没有运气。我是C的新手并且处理指针,所以如果有人可以帮助我,我会非常感激!

我打算发布一个输出图像,但我看起来没有足够的声誉,所以输出看起来如下:

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

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

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

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

test.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

2 个答案:

答案 0 :(得分:1)

您的&#39; swap&#39; -function中的局部变量tp未初始化。您的编译器应该警告您这一点。然后你无论如何都要访问它是未定义的行为......

答案 1 :(得分:0)

当node1与node1->交换时,你需要更改指向node1的上一个节点的下一个指针,或者你可以在node1和node1->之间交换数据,然后保持下一个指针不变。

如果你要交换指针,这是交换之前的样子:

node0->next = node1, node1->next = node2, node2->next = node3

并在交换后:

node0->next = node2, node2->next = node1, node1->next = node3

这仅适用于相邻节点。如果节点不相邻,则在swap之前:

nodea->next = node1, node1->next = nodeb
nodec->next = node2, node2->next = noded
交换后

nodea->next = node2, node2->next = nodeb
nodec->next = node1, node1->next = noded