Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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++,我的教授已经为我们的期末考试在一个月内安排了一些复习。据我所知,我已经正确地回答了所有其他问题,但这两个问题有点奇怪。本质上,我的教授创建了一个名为ListA的类,它使用动态分配的数组作为底层存储结构。根据下面的代码,他希望我们做两件事: 写出必要的私有变量 编写类ListA的构造函数_C++_Arrays_Dynamic Allocation - Fatal编程技术网

在类存储结构中使用动态分配的阵列 我在大学的第一年是C++,我的教授已经为我们的期末考试在一个月内安排了一些复习。据我所知,我已经正确地回答了所有其他问题,但这两个问题有点奇怪。本质上,我的教授创建了一个名为ListA的类,它使用动态分配的数组作为底层存储结构。根据下面的代码,他希望我们做两件事: 写出必要的私有变量 编写类ListA的构造函数

在类存储结构中使用动态分配的阵列 我在大学的第一年是C++,我的教授已经为我们的期末考试在一个月内安排了一些复习。据我所知,我已经正确地回答了所有其他问题,但这两个问题有点奇怪。本质上,我的教授创建了一个名为ListA的类,它使用动态分配的数组作为底层存储结构。根据下面的代码,他希望我们做两件事: 写出必要的私有变量 编写类ListA的构造函数,c++,arrays,dynamic-allocation,C++,Arrays,Dynamic Allocation,对于我编写的构造函数: List::List(int size) { array = new ItemType[size]; length = 0; head = array[0]; } 对于我编写的必要的私有变量: itemType* array; int length; //to keep track how many elements are filled itemType* head; //

对于我编写的构造函数:

    List::List(int size)
    {
        array = new ItemType[size];
        length = 0;
        head = array[0];
    }
对于我编写的必要的私有变量:

    itemType* array;
    int length; //to keep track how many elements are filled
    itemType* head; //to create pointer for first element in array
    itemType size;
我只是不确定这些行是否正确。我觉得我很接近,但我需要一些帮助。以下是.h文件:

typedef int itemType;
class ListA
{
public:
List(int size);
~List();
/*
pre: an instance of lists exists
post: true if list is empty, false otherwise
*/
bool IsEmpty();
/*
pre: an instance of list exists
post: returns len
gth of the list
*/
int GetLength();
/*
pre: an instance of list exists
post: newItem is at the head of the list
*/
void PutItemH(itemType newItem);
/*
pre: an instance of list exists and is not empty
post: Returns the contents of the head of the list.
*/
itemType GetItemH();
/*
pre: an instance of list exists and is not empty
post: head of the list is deleted
*/
void DeleteItemH();
/*
pre: an instance of list exists an
d is not empty
post: contents of list nodes are displayed on subsequent lines  from head to tail
*/
void Print();
/*
Pre: an instance of list exists
Post: returns true if the target is in the list, false otherwise
/*
bool Find(itemType
target)
/*
Pre: an instance of list exists
Post: if a node contains target, the node is deleted and true is returned, else false is returned.
/*bool DeleteItem (itemType target)
private:
#notice that there are no member variables
.  See problem 14.
};

您不需要这么多成员变量

itemType* array;
int length; //to keep track how many elements are filled
itemType* head; //to create pointer for first element in array - Why ?
itemType size;
您可以将其精简为以下内容

itemType* array;
int length;
int size;
构造函数应该如下所示:

List::List(int size):size(size), length(0)
{
    array = new ItemType[size];
}
这应足以满足所有成员功能的需要。例如:

bool ListA::IsEmpty() {return length == 0;}
int ListA::GetLength() {return length;}

length
将在向基础数组添加项时递增。

那么,您到底有什么问题?什么不起作用?顺便说一句,你的
head
似乎是多余的,因为
array
已经指向了head,除非你在做一些类似循环队列的事情,一般来说,将某个东西称为列表并不意味着它就是列表。数据类型由其行为、方法的语义有效地定义。只有在一个点上才能添加和删除元素的容器通常称为堆栈。在您的示例中,只有注释掉的
DeleteItem
方法与堆栈容器的方法不同。错误-如果列表方法允许头部更改,则确实需要头部。(我想到了pust_-front/pop_-front方法)。事实上,由于列表节点是独立解除分配的,因此不足以知道may是如何使用的,您需要确切地知道哪些节点在使用。