【发布时间】:2020-07-08 21:21:40
【问题描述】:
该程序被假设为一个双向链表,提示用户选择任一
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?"
问题:在执行时,当我为“值”输入双精度后,程序冻结了一点然后退出
//--输入提示
cout<<"\nWhat value would you like to insert to the list: "; cin>>value; l.insertAtEnd(value); break;
//--- 函数定义---
void List::insertAtEnd(double & x) { List::NodePointer ptr; ptr = new List::Node(last, x ); last->next = ptr; last = ptr; count ++; sum += x; }
//--- 原型
包括
// #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
请帮忙...
【问题讨论】:
-
如果您提供mwe,您获得帮助的机会会大大增加。
-
除非这是一个纯粹的学习练习,否则你真的想要std::list。如果是,那么基本的插入和删除示例将是 Doubly-Linked List of Integers
-
@DavidC.Rankin ... 或 ... 根本没有列表。
-
这是一个学习任务
标签: c++ nodes doubly-linked-list