Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.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+中的链表对距原点的距离、x和y坐标进行排序+; 我在C++中有一些与链表排序相关的问题。我有一个任务要完成,我已经创建了逻辑,但我的链表没有排序_C++_Sorting_Linked List - Fatal编程技术网

使用C+中的链表对距原点的距离、x和y坐标进行排序+; 我在C++中有一些与链表排序相关的问题。我有一个任务要完成,我已经创建了逻辑,但我的链表没有排序

使用C+中的链表对距原点的距离、x和y坐标进行排序+; 我在C++中有一些与链表排序相关的问题。我有一个任务要完成,我已经创建了逻辑,但我的链表没有排序,c++,sorting,linked-list,C++,Sorting,Linked List,该程序要求用户输入名称、x和y坐标,以便在名为“auf2_euclidcalc”的函数中计算距原点的欧几里德距离。然后我在函数“auf5\u display()”中使用名为“auf2\u euclidcal”的函数来显示距离值 该程序完美地编译和显示用户的输入,但不按升序对距原点的距离进行排序(排序函数为“auf4\u sort()”) 我需要关于排序与原点的距离以及相应的x和y坐标的帮助。有人能帮忙吗?请更正排序函数中的错误以使其排序 提前谢谢! 请看下面我的节目 #include <i

该程序要求用户输入名称、x和y坐标,以便在名为“auf2_euclidcalc”的函数中计算距原点的欧几里德距离。然后我在函数“auf5\u display()”中使用名为“auf2\u euclidcal”的函数来显示距离值

该程序完美地编译和显示用户的输入,但不按升序对距原点的距离进行排序(排序函数为“auf4\u sort()”)

我需要关于排序与原点的距离以及相应的x和y坐标的帮助。有人能帮忙吗?请更正排序函数中的错误以使其排序

提前谢谢! 请看下面我的节目

#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>

using namespace std;

struct thenode  //auf 1
{
string nameofobstacle;
double x, y;
double distancetotheorigin;
thenode *next;

};

thenode *head = nullptr;
thenode *last = nullptr ;


void insertobstacle (string nameofobstacle, double x, double y);
double auf2_euclidcalc (double x1, double x2, double y1, double y2);
void auf4_sort ();
void auf5_display();
void outputobstacles();
void auf6_pointstobedeleted ();

void insertobstacle (string nameofobstacle, double x, double y) 
{
thenode *storenewnode = new thenode;          //   newstorefourdata->distancetotheorigin = distancetotheorigin;

storenewnode->nameofobstacle = nameofobstacle;
storenewnode->x = x;
storenewnode->y = y;
storenewnode->next = head;
head = storenewnode;
double x2, y2;
x2 = storenewnode->x;
y2 = storenewnode->y;
auf2_euclidcalc(0, x2, 0, y2);
}


double auf2_euclidcalc (double x1, double x2, double y1, double y2)   //Auf 2
{


thenode *ptr_storedx_y = new thenode;

double d;      //how to link this to the structure so that we can store x and y
double p1= x1-x2;
double p2= y1-y2;
ptr_storedx_y->distancetotheorigin= pow(p1, 2) + pow(p2, 2);
d= sqrt(ptr_storedx_y->distancetotheorigin); //obs.d

return d;

}


void auf5_display()
{
double d;
thenode *tempo=new thenode;
tempo=head;
while(tempo!=nullptr)
{
   double x1, y1, x2, y2;
   x1=0; //from origin
   y1=0; //from origin
   x2=tempo->x; //inputted dist
   y2=tempo->y; //inputted dist

  cout << "obstacle " << tempo->nameofobstacle << ": ( " <<setprecision(3) << tempo->x << " ,  " << 
  setprecision(3) <<tempo->y << " ) , ";
  tempo = tempo->next;



  cout<< "distance: " << auf2_euclidcalc(x1, x2, y1, y2) << endl;

}
}

void auf4_sort ()
{
double x_cor,y_cor;
string p_name;
double temproll;
thenode *temphead = head;

auf2_euclidcalc(0, x_cor, 0, y_cor);

int counter = 0;
while (temphead!=nullptr) //IT SHOULD CHANGE TO TEMP NULL
{
temphead = temphead->next;
counter++;
}
temphead = head;

for (int j=0; j<counter; j++)
{
while (temphead->next!=nullptr)  //iterate through list until next is null
{
if (temphead->distancetotheorigin > temphead->next->distancetotheorigin)
{

temproll = temphead->distancetotheorigin;
temphead->distancetotheorigin = temphead->next->distancetotheorigin;
temphead->next->distancetotheorigin = temproll;

             p_name = temphead->nameofobstacle;
             temphead->nameofobstacle = temphead->next->nameofobstacle;
             temphead->next->nameofobstacle = p_name;

             x_cor = temphead->x;
             temphead->x = temphead->next->x;
             temphead->next->x = x_cor;

             y_cor = temphead->y;
             temphead->y = temphead->next->y;
             temphead->next->y = y_cor;

             temphead = temphead->next;


}
else
temphead = temphead->next;//increment node

}
temphead = head;//reset temphead
}


}


void auf6_pointstobedeleted ()
{
   thenode *deletenow;
   while (head != nullptr)
   {
   deletenow = head;
   head = head->next;
   cout << "delete: " << deletenow->nameofobstacle << " :DELETED: " << endl;
   delete deletenow;
   }
}

int main ()
{

thenode *ptrstoring = new thenode;

while (cin)
{
cout<< "string describing obstacle ('end' for end of input): "<<endl;
cin>> ptrstoring->nameofobstacle;
if (ptrstoring->nameofobstacle=="end" || ptrstoring->nameofobstacle== "END")
{
    break;
}
else
{
    cout<< "x and y coordinate: " <<endl;
    cin>> ptrstoring->x;
    cin>> ptrstoring->y;
    insertobstacle (ptrstoring->nameofobstacle, ptrstoring->x, ptrstoring->y);


}




}


auf4_sort ();
auf5_display();
auf6_pointstobedeleted ();
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构thenode//auf 1
{
障碍物的字符串名称;
双x,y;
与原点的双重距离;
节点*下一个;
};
节点*head=nullptr;
thenode*last=nullptr;
void insertobstacle(abstacle的字符串名称,双x,双y);
双auf2_欧几里得(双x1,双x2,双y1,双y2);
void auf4_sort();
无效auf5_显示();
无效输出障碍();
无效auf6_点删除();
void insertobstacle(abstacle的字符串名称,双x,双y)
{
thenode*storenewnode=newthenode;//newstorefourdata->distancetotheorigin=distancetotheorigin;
storenewnode->NameOfAbstracle=NameOfAbstracle;
storenewnode->x=x;
storenewnode->y=y;
storenewnode->next=头部;
head=storenewnode;
双x2,y2;
x2=storenewnode->x;
y2=storenewnode->y;
auf2_欧几里得(0,x2,0,y2);
}
双auf2_欧几里得(双x1,双x2,双y1,双y2)//Auf 2
{
thenode*ptr\u storedx\u y=新的thenode;
double d;//如何将其链接到结构,以便存储x和y
双p1=x1-x2;
双p2=y1-y2;
ptr_storedx_y->到原点的距离=功率(p1,2)+功率(p2,2);
d=sqrt(ptr_storedx_y->到原点的距离);//obs.d
返回d;
}
无效auf5_显示()
{
双d;
thenode*tempo=新的thenode;
节奏=头;
while(节奏!=nullptr)
{
双x1,y1,x2,y2;
x1=0;//从原点开始
y1=0;//从原点开始
x2=节奏->x;//输入的距离
y2=节奏->y;//输入的距离
cout next->distance to the origin=temproll;
p_name=临时头->障碍物名称;
temphead->nameofAbstracle=temphead->next->nameofAbstracle;
临时头->下一步->障碍物名称=p_名称;
x_cor=临时头->x;
临时头->x=临时头->下一步->x;
temphead->next->x=x\u cor;
y_cor=临时头->y;
临时头->y=临时头->下一步->y;
temphead->next->y=y\U cor;
temphead=temphead->next;
}
其他的
temphead=temphead->next;//增量节点
}
temphead=头部//重置临时磁头
}
}
无效auf6_点已删除()
{
node*deletenow;
while(head!=nullptr)
{
deletenow=头部;
头部=头部->下一步;
cout>ptrstoring->y;
插入障碍物(ptrstoring->障碍物名称,ptrstoring->x,ptrstoring->y);
}
}
auf4_排序();
auf5_显示();
auf6_pointstobedeleted();
返回0;
}

我没有扫描整个代码,但是我可以提出几点

我想到的问题是,为什么在auf2_euclidcal函数中创建一个新节点,而以后不使用它来产生内存泄漏

关于你的排序算法。在第二个循环中,您将一次又一次地遍历整个列表,但在运行之后,您知道最后一个元素已被交换或位于正确的位置。因此,您可以稍后在其他迭代中忽略它。因此,列表是从尾部排序的

此外,一些实现使用布尔标志而不是计数器。该标志在节点违反其顺序时设置,并且只要存在交换,算法就可以工作。请注意,对列表中的所有节点进行计数是O(n)

为方便起见,可以为节点结构创建交换函数

void swap(thenode *lhs, thenode *rhs)
{
    swap(lhs->distancetotheorigin, rhs->distancetotheorigin);
    swap(lhs->nameofobstacle, rhs->nameofobstacle);
    swap(lhs->x, rhs->x);
    swap(lhs->y, rhs->y);
}
有关链接列表中气泡排序的更多说明,请参阅CKCKOUT


无意冒犯,我想说的是,您应该养成一种习惯,拥有一致的表达代码风格。请检查C++关于这个问题的指导方针。

你是否已经测试了链表类,看看它是否可以排序简单的东西,比如一个链表的整数?如果它不能做到这一点,那么它就无法对更复杂的内容进行排序。当你使用调试器一次运行一行程序时,你做了哪些观察?这是一个很好的机会,可以学习如何使用它一次运行一行程序,检查所有变量及其变化时的值,并分析程序的逻辑执行流。了解如何使用调试器是每个C++开发人员所需的技能,没有例外。在调试器的帮助下,您应该能够在本程序和所有未来编写的程序中快速找到错误,而无需向任何人寻求帮助。Sam刚刚给了您一些最好的建议。
auf2_euclidcal(0,x_cor,0,y_cor)在给定值之前使用
x_cor
y_cor
。这会导致各种奇怪的错误。大多数现代编译器都会警告您此类问题。不要忽视警告。建议:学习如何使用调试器。使用它执行排序功能,一次一行,链表只包含两个值。看看它是如何工作的。如果没有,这是一个足够简单的测试用例,您可以看到问题。如果没有,请使用三元素链表重复此操作。等等Stackoverflow不是一个网站,人们可以在这里找到其他人为他们调试程序。