简体   繁体   English

双向链表,我似乎可以创建一个新节点,我离我有多远?

[英]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.您的代码的另一个不寻常之处是您拥有这些countsum变量。 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.此外,如果您继续这样做,那么您需要将countsum添加到您的列表类中。 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?那么你将如何保持countsum变量的更新? Personally I would just remove the code dealing with count and sum , just calculate these quantities when you need them.就我个人而言,我只想删除处理countsum的代码,只需在需要时计算这些数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在双向链接列表中创建新节点? (C ++) - How do I create a new Node in a Doubly Linked List? (C++) 当我将双向链表的头部分配给我创建的新节点时出现分段错误 - I am getting a segmentation error when I assign the head of my doubly linked list to a new node I created 如何删除双向链接列表中的第一个节点? - How do I delete the first node in a doubly-linked list? 如何从双向链表中获得节点内部的有效负载 - How can I get the payload inside of a node from a doubly linked list 当我试图在双循环链表之间输入一个节点时,它给出了错误的 output - When I am trying to enter a node between doubly circular linked list it gives wrong output 尝试在双向链表的中间插入新节点时是否接线错误? - Have i miswired when trying to insert a new node at the middle of doubly linked list? 如何为双向链表 C++ 创建 insertAfter 函数 - How do I create an insertAfter function for a doubly linked list c++ 双链表 - 导致我的代码抛出编译错误的原因是什么?如何解决? - Doubly Linked List - What is causing my code to throw a compiler error and how can I fix it? 如何将双向链接列表中的头部初始化为NULL? - How do I initiliaze a head in a doubly-linked list to be NULL? 如何在不使用额外空间的情况下检查双向链表是否是回文? - How can I check if a doubly-linked list is a palindrome without using extra space?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM