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

时间:2016-04-18 19:05:37

标签: c++ sorting linked-list

我试图根据它们与原点的距离对点的链接列表进行排序。点越接近,它应该是链表中的第一个/下一个。示例:如果用户输入(2,2)(4,4)(1,1)(3,3),则应使用指向(1,1)(2,2)的下一个指针重新排序链表(3, 3)(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;
}

1 个答案:

答案 0 :(得分:0)

如果您希望能够更改root,则需要将其作为**传递。 然而,即便如此,我也不认为你的sort函数会起作用,它会返回一个截断的列表,在最小元素之前缺少所有内容。

他们有什么理由使用自定义链表和排序算法吗? 标准库负责容器和分类之类的事情:

#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;
    }
}