저는 대학에서 C++ 첫 해에 있습니다. 그리고 내 교수는 한 달 안에 최종 테스트를위한 몇 가지 리뷰를 할당했습니다. 나는 내 지식으로, 다른 모든 질문을했다. 그러나이 두 가지 질문은 조금 이상하다. 본질적으로 교수님은 ListA
이라는 클래스를 만들었습니다.이 클래스는 동적으로 할당 된 배열을 기본 저장소 구조로 사용합니다.클래스에서 동적으로 할당 된 배열 사용하기 스토리지 구조
- 밖으로 쓰기에 필요한 private 변수
- 내가 쓴 생성자를 들어 클래스
ListA
의 생성자 쓰기 : 아래의 코드 감안할 때, 그는 두 가지 작업을 수행하기를 원하십니다 :
List::List(int size)
{
array = new ItemType[size];
length = 0;
head = array[0];
}
필요한 개인 변수에 대해 wro te :
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.
};
그럼 정확히 무엇이 문제입니까? 무엇이 작동하지 않습니까? btw,'head'는 순환 배열과 같은 것을하지 않는 한'array'가 이미 머리를 가리키고 있기 때문에 불필요한 것처럼 보입니다. –
일반적으로리스트를 호출하면리스트가되지 않습니다. 데이터 형은 그것의 행위, 메소드의 의미론에 의해 효과적으로 정의된다. 한 지점에서만 요소를 추가하고 제거 할 수있는 컨테이너를 일반적으로 _stack_이라고합니다. 귀하의 예제에서, 주석 처리 된'DeleteItem' 메소드 만이 스택 컨테이너의 메소드와 다릅니다. – MSalters