简体   繁体   English

链接列表排序不影响main(C ++)中的列表项?

[英]Linked list sorting not affecting list items in main (C++)?

I am attempting to sort a linked list of points based on their distance to the origin. 我试图根据它们与原点的距离对点的链表进行排序。 The closer the point, it should be the first/next in the linked list. 点越接近,它应该是链表中的第一个/下一个。 Example: If a user inputs (2,2) (4,4) (1,1) (3,3) the linked list should be reordered using the next pointer to (1,1) (2,2) (3,3) (4,4). 示例:如果用户输入(2,2)(4,4)(1,1)(3,3),则应使用指向(1,1)(2,2)的下一个指针重新排序链表(3, 3)(4,4)。 It works except that the points in main are not affected by the sorting and the points are not interchanged, so if (1,1) is 3rd like in the example it will be the smallest point the 2nd time around the loop again when compared to (4,4). 除了主要的点不受排序的影响并且点不相互交换之外,它的工作方式是正常的,所以如果(1,1)是第3个,就像在示例中那样,它将是第二次环绕循环时的最小点。 (4,4)。

#include <iostream>
#include <math.h>
using namespace std;

class List {
public:
    int x;
    int y;
    List *next;
};
void init(List *root) {
    int x, y;
    List *traverse;
    traverse = root;
    while(traverse != 0) {
        cout<<"Enter coordinate x: ";
        cin>>x;
        traverse->x = x;
        cout<<"Enter coordinate y: ";
        cin>>y;
        traverse->y = y;
        traverse = traverse->next;
    }
}
void display(List *root) {
    List *traverse;
    traverse = root;
    while(traverse->next != 0) {
        cout<<"("<<traverse->x<<","<<traverse->y<<") ";
        traverse = traverse->next;
    }
    cout<<"("<<traverse->x<<","<<traverse->y<<")"<<endl;
}
void sort(List *root, int n) {
    List *traverse;
    List *stable;
    traverse = root;
    stable = root;
    double d1, d2;
    for(int i = 0; i < n; i++) {
        d1 = sqrt(pow(traverse->x, 2) + pow(traverse->y, 2));
        d2 = sqrt(pow(stable->x, 2) + pow(stable->y, 2));
        if(d1 < d2) {
            root = traverse;
        }
        traverse = traverse->next;
    }
}
void collinear(List *root) {
    int x[3] = {0}, y[3] = {0};
    int value;
    List *traverse;
    traverse = root;
    for(int i = 0; i < 3; i++) {
        if(traverse != 0) {
            x[i] = traverse->x;
            y[i] = traverse->y;
            traverse = traverse->next;
        }
    }
    if(x[2] != 0) {
        value = x[0] * (y[1] - y[2]) + x[1] * (y[2] - y[0]) + x[2] * (y[0] - y[1]);
        if(value == 0) {
            traverse = root;
            for(int i = 0; i < 3; i++) {
                cout<<"("<<traverse->x<<","<<traverse->y<<") ";
                traverse = traverse->next;
            }
            cout<<"collinear!"<<endl;
        }
        else {
            traverse = root;
            for(int i = 0; i < 3; i++) {
                cout<<"("<<traverse->x<<","<<traverse->y<<") ";
                traverse = traverse->next;
            }
            cout<<"non-collinear!"<<endl;
        }
    }
    else {
        cout<<"Not a group of 3 points cannot calculate collinearity!"<<endl;
    }
}
int main() {
    List *root;
    root = new List;
    List *traverse;
    traverse = root;
    List *node1;
    node1 = new List;
    List *node2;
    node2 = new List;
    List *node3;
    node3 = new List;
    root->next = node1;
    node1->next = node2;
    node2->next = node3;
    node3->next = 0;
    init(traverse);
    display(traverse);
    /*for(int i = 0; i < 2; i++) {
        collinear(traverse);
        traverse = traverse->next;
    }
    traverse = root;*/
    traverse = root;
    int n = 4;
    for(int i = 0; i < 4; i++) {
        List *traverse2;
        traverse2 = traverse;
        sort(traverse2, n);
        traverse = traverse->next;
        n--;
    }
    traverse = root;
    display(traverse);
    return 0;
}

If you want to be able to change the root you'll need to pass it as a **. 如果您希望能够更改根,则需要将其作为**传递。 However even then I dont think your sort function will work it will just return a truncated list missing everything before the smallest element. 然而,即便如此,我也不认为你的排序函数会起作用,它会返回一个截断的列表,在最小元素之前缺少所有内容。

Is their any reason your using a custom linked list and sort alogorithm? 他们有什么理由使用自定义链表和排序算法吗? The standard library takes care of things like containers and sorting: 标准库负责容器和排序之类的事情:

#include <vector>
#include <algorithm>
#include <iostream>

class Point
{
public:
    Point(int x_, int y_) :
        x(x_),
        y(y_)
    {
    }

    // no need to square root to compare distance from origin
    int distanceSquared() const
    {
        return (x*x)+(y*y);
    }

    // define an operator< so we can sort containers holding this class
    bool operator< ( const Point& rhs ) const
    {
        return distanceSquared() < rhs.distanceSquared();
    }

    // declare a friend function for outputing the values in a user friendly way
    friend std::ostream& operator<<(std::ostream& os, const Point& p);

    int x;
    int y;    
};

// and define the friend function outside the class
std::ostream& operator<<(std::ostream& os, const Point& p)
{
    os << "(" << p.x << ", " << p.y << ")";
    return os;
}

int main()
{
    // std::vector to store user entered points
    std::vector<Point> points;
    // temp storage for user input 
    int x, y;
    // entering anything non-numberic will exit this loop
    while( true )
    {
        std::cout << "Enter X coordinate : ";
        std::cin >> x;
        if( std::cin.fail() )
            break;
        std::cout << "Enter Y coordinate : ";
        std::cin >> y;
        if( std::cin.fail() )
            break;
        // we've read in a valid x,y so add a new point to vector
        points.push_back( Point(x, y) );
    }

    // sort will use the operator< function in Point class
    std::sort(points.begin(), points.end());   

    // use the stream operator to debug out our point
    for( auto it = points.begin(); it != points.end(); ++it )
    {
        std::cout << *it << std::endl;
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM