下面我有一些代码可以创建一个单链表,并在其中存储一些点。它编译,但对于我的生活,我不知道我在哪里以及为什么我会遇到分段错误。它在程序启动时就会发生。
另外,在你告诉我之前,我知道这个排序链表算法的效率非常低,但我一直试图让它工作一段时间,而我只是想在这一点上努力工作。
非常感谢您的帮助!
文件1:
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "point.h"
typedef struct node
{
Point p;
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;
node *head = NULL;
point_set(insertPoint,7.0,1.0);
insertAtFront(&head,insertPoint);
sort(head);
point_set(insertPoint,1.0,1.0);
insertAtFront(&head,insertPoint);
sort(head);
point_set(insertPoint,4.0,1.0);
insertAtFront(&head,insertPoint);
sort(head);
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;
double d1;
double d2;
if(node1 == NULL)
return;
do
{
swapped = 0;
node1 = start;
*tp1 = node1->p;
*tp2 = node1->next->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", point_getX(tp), point_getY(tp), distanceFromOrigin(tp));
temp = temp->next;
}
}
文件2:
#include <assert.h>
#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;
}
文件3:
#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
答案 0 :(得分:2)
好的,一开始就是正确的:
int main(int argc, char **argv)
{
Point *insertPoint; // this pointer points nowhere
node *head = NULL;
point_set(insertPoint,7.0,1.0); // still point_set dereferences it