[英]doubly linked list, i can seem to create a new Node, How far off am i?
the program is suppose to be a doubly linked list that prompts the user to either该程序假设是一个双向链表,提示用户
1.Insert" "2. Delete"<<endl<< "3. Display"<<endl<< "4. Sum"<<endl<< "5. Average"<<endl<< "6. Exit" From the list above what would you like to do next?"
problem: at execution, after i enter a double for "value", the program freeze for a little bit then exits问题:在执行时,在我为“值”输入双精度值后,程序冻结了一点然后退出
//-- input prompt //- 输入提示
cout<<"\\nWhat value would you like to insert to the list: "; cin>>value; l.insertAtEnd(value); break;
//--- function definition --- //--- 函数定义 ---
void List::insertAtEnd(double & x) { List::NodePointer ptr; ptr = new List::Node(last, x ); last->next = ptr; last = ptr; count ++; sum += x; }
//--- prototypes //--- 原型
include
包括
// #include"Node.h"
>
> using namespace std;
>
> #ifndef LIST
> #define LIST
>
> typedef double ElementType;
>
> class List
> {
> public:
> ...
> void insertAtEnd(ElementType & x); //insert a value x on the end of the list
> ...
>
> private:
> ...
>
> class Node
> {
> public:
> ElementType data;
> Node *prev;
> Node *next;
> //--- Node constructor
> /*-------------------------------------------------------------------
> Precondition: None.
> Postcondition: A Node has been constructed with value in its data
> part and its next part set to link (default 0).
> -------------------------------------------------------------------*/
> Node(Node *prevNodePtr,ElementType value, Node *link = 0)
>
> : prev(prevNodePtr) ,data(value), next(link)
> {}
> };
>
> typedef Node * NodePointer;
> NodePointer last;
> NodePointer first;
>
> };
>
> ostream & operator<<(ostream & out, const List & s);
>
> #endif // !LIST
please help...请帮忙...
I don't think you are far off.我不认为你离得很远。 The main problem I see is that you don't deal with the
first
pointer in your List
class.我看到的主要问题是您没有处理
List
类中的first
指针。
When you are adding a value to the end of a list like this the first value to be added to the list is a special case because you must also update the first
pointer.当您像这样向列表末尾添加一个值时,要添加到列表中的第一个值是一种特殊情况,因为您还必须更新第
first
指针。 Your code doesn't do this.您的代码不会这样做。
The other unusual thing about your code is that you have those count
and sum
variables.您的代码的另一个不寻常之处是您拥有这些
count
和sum
变量。 It seems like you are trying to keep a running total of the list length and list sum.似乎您正在尝试保持列表长度和列表总和的运行总数。 That's not wrong but it's an unusual thing to do.
这并没有错,但这是一件不寻常的事情。 Normally you would calculate those when needed, not all the time.
通常,您会在需要时计算这些值,而不是一直计算。 Also if you do keep doing things that way then you need to add
count
and sum
to your list class.此外,如果您继续这样做,那么您需要将
count
和sum
添加到您的列表类中。 At the moment you have a list in one variable and it's length and sum in different variables.目前,您在一个变量中有一个列表,它是不同变量中的长度和总和。 That's bad design because there's no close association between the variables.
这是糟糕的设计,因为变量之间没有密切关联。 Imagine you had two lists.
想象一下,你有两个列表。 How would you keep the
count
and sum
variables updated then?那么你将如何保持
count
和sum
变量的更新? Personally I would just remove the code dealing with count
and sum
, just calculate these quantities when you need them.就我个人而言,我只想删除处理
count
和sum
的代码,只需在需要时计算这些数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.