下面我用C编写了一个简单的链表程序,它保存了一个点及其到原点的距离。我的问题是,当调用排序函数时,它不能正确排序。我花了相当多的时间调试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: %gn", 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没有初始化。你的编译器应该就此警告你。然后你就可以访问它了这是未定义行为。
当与node1->next交换node1时,您需要更改指向node1的前一个节点的next指针,或者您可以在node1和node1->next之间交换数据,使next指针保持不变。
如果你要交换指针,下面是交换之前的样子:
node0->next = node1, node1->next = node2, node2->next = node3
和swap后:
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