我正在尝试创建链接列表并同时对链接列表进行排序,但我无法弄清楚如何让代码完全对列表进行排序。
以下是我的课程:
module Option =
let apply fOpt xOpt =
match fOpt,xOpt with
| Some f, Some x -> Some (f x)
| _ -> None
let resultOption =
let (<*>) = Option.apply
Some (fun x y z -> sprintf "%i %s %A" x y z)
<*> Some 5
<*> Some "hi"
<*> Some true
};
class PayRoll{
private:
string EmployeeName; // employee name
double PayRate; // employee pay rate
double HoursWorked; // employee hours worked
public:
void setRate(double); // set rate
void setName(string); // set name
void setHours(double); // set hours worked
string getName(){return EmployeeName;};
double getHours(){return HoursWorked;};
double getRate() {return PayRate;};
double getPaid(){return (PayRate*HoursWorked);};
我认为的主要问题是插入功能。数据的实现成功完成,所以我忽略了这一点。我不能让这部分工作。
class PayRollList{
private:
struct ListNode {
PayRoll p;
ListNode* next;
};
ListNode *head;
public:
PayRollList() { head = nullptr; };
void insert(string, double, double); // name, rate, hours
void printPayChecks(); // print name and total pay for all employees
};
}
非常感谢任何帮助!
答案 0 :(得分:0)
指向指针的技巧是保持向后引用。您可以使用单深度指针完成相同的操作,并始终引用 - &gt; next元素进行比较,但是您需要插入链接列表的第一个元素的特殊情况。
void PayRollList::insert(string EmpName, double rate, double hours){
ListNode* newNode = new ListNode;
newNode->p.setName(EmpName);
newNode->p.setRate(rate);
newNode->p.setHours(hours);
newNode->next = nullptr;
ListNode** current = &(this->head);
while (*current != nullptr && (*current)->p.getRate() < newNode->p.getRate()) {
current = &((*current)->next);
}
newNode->next = *current;
*current = newNode;
}