Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 链表排序不影响main(C+;+;)中的列表项?_C++_Sorting_Linked List - Fatal编程技术网

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

C++ 链表排序不影响main(C+;+;)中的列表项?,c++,sorting,linked-list,C++,Sorting,Linked List,我试图根据点到原点的距离对点的链接列表进行排序。离点越近,它应该是链接列表中的第一个/下一个。示例:如果用户输入(2,2)(4,4)(1,1)(3,3),则应使用指向(1,1)(2,2)(3,3)(4,4)的下一个指针对链表重新排序。除了主回路中的点不受排序影响且点不互换外,该方法有效,因此,如果(1,1)与示例中的(4,4)类似,则与(4,4)相比,如果(1,1)是第三个,它将再次成为循环中第二个最小的点 #包括 #包括 使用名称空间std; 班级名单{ 公众: int x; int-y; 列

我试图根据点到原点的距离对点的链接列表进行排序。离点越近,它应该是链接列表中的第一个/下一个。示例:如果用户输入(2,2)(4,4)(1,1)(3,3),则应使用指向(1,1)(2,2)(3,3)(4,4)的下一个指针对链表重新排序。除了主回路中的点不受排序影响且点不互换外,该方法有效,因此,如果(1,1)与示例中的(4,4)类似,则与(4,4)相比,如果(1,1)是第三个,它将再次成为循环中第二个最小的点

#包括
#包括
使用名称空间std;
班级名单{
公众:
int x;
int-y;
列表*下一步;
};
void init(列表*根){
int x,y;
列表*导线测量;
横移=根;
while(遍历!=0){
coutx;
横向->x=x;
库蒂;
横向->y=y;
遍历=遍历->下一步;
}
}
无效显示(列表*根){
列表*导线测量;
横移=根;
while(遍历->下一步!=0){

cout如果您想更改根目录,则需要将其作为**传递。 然而,即使这样,我也不认为排序函数会起作用,它只会返回一个截断的列表,在最小的元素之前丢失所有内容

它们是你使用自定义链表和排序算法的原因吗? 标准库负责容器和排序等工作:

#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;
    }
} 
#包括
#包括
#包括
类点
{
公众:
点(整数x,整数y):
x(x_),
y(y_)
{
}
//无需平方根来比较与原点的距离
整数距离平方()常数
{
返回(x*x)+(y*y);
}
//定义一个操作符<,这样我们就可以对包含此类的容器进行排序
布尔运算符<(常数点和rhs)常数
{
返回distanceSquared()friend std::ostream&operator如果您希望能够更改根目录,则需要将其作为**传递。
然而,即使这样,我也不认为排序函数会起作用,它只会返回一个截断的列表,在最小的元素之前丢失所有内容

它们是你使用自定义链表和排序算法的原因吗? 标准库负责容器和排序等工作:

#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;
    }
} 
#包括
#包括
#包括
类点
{
公众:
点(整数x,整数y):
x(x_),
y(y_)
{
}
//无需平方根来比较与原点的距离
整数距离平方()常数
{
返回(x*x)+(y*y);
}
//定义一个操作符<,这样我们就可以对包含此类的容器进行排序
布尔运算符<(常数点和rhs)常数
{
返回distanceSquared()friend std::ostream&Operator如果你有(1,4)(3,1)(2,2)和(1,1)你想做什么?用
void sort(List*root,int n)
替换
void sort(List*&root,int n)
如果你希望更改根节点。如果你想对链接列表排序,Wiki有示例伪代码。如果你有(1,4)你想做什么(3,1)(2,2)和(1,1)?将
无效排序(列表*根,int n)
替换为
无效排序(列表*&根,int n)
如果您希望能够更改根节点。如果您想对链表进行排序,Wiki提供了示例伪代码。我使用自定义链表是因为这是我课程的一部分,我们正在学习数据结构和算法的工作原理,但我的课程老师没有很好地解释这些内容,我们必须使用自定义链表和排序ng.我希望我可以使用标准的图书馆链接列表:))我使用自定义链接列表,因为这是我课程的一部分,我们正在学习数据结构和算法如何工作,但我的课程老师没有很好地解释这些事情,我们必须使用自定义链接列表和排序。我希望我可以使用标准的图书馆链接列表:))