C ++에 경험이 없다. 나는 나의
LinkedList
를 얻을 수 있었다
완벽하게 작동합니다. 잘못된 관행을 따랐는지 여부와 코드와 관련된 위험이 있는지 여부에 대한 피드백을 받기를 희망했습니다.
LinkedList.h
#pragma once
#include <string>
#include "Node.h"
class LinkedList
{
private:
Node *head;
int count;
public:
LinkedList();
~LinkedList();
int get_count();
void AddNode(int);
std::string GetList();
bool Contains(int);
void Remove(int);
int GetValue(int);
};
LinkedList.cpp
#include <iostream>
#include "LinkedList.h"
LinkedList::LinkedList()
{
head = new Node(0);
}
LinkedList::~LinkedList()
{
}
int LinkedList::get_count(){
return count;
}
void LinkedList::AddNode(int _value){
Node *node = new Node(_value);
Node *previousNode = head;
while (previousNode->next_node && previousNode->next_node->get_value() <= node->get_value()){
previousNode = previousNode->next_node;
}
if (previousNode->next_node){
node->next_node = previousNode->next_node;
}
previousNode->next_node = node;
count++;
}
std::string LinkedList::GetList(){
std::string list;
Node *currentNode = head;
while (currentNode->next_node){
currentNode = currentNode->next_node;
list += std::to_string(currentNode->get_value()) + " ";
}
return list;
}
bool LinkedList::Contains(int number){
Node *currentNode = head;
while (currentNode->next_node){
currentNode = currentNode->next_node;
if (currentNode->get_value() == number){
return true;
}
if (currentNode->get_value() > number){
return false;
}
}
return false;
}
void LinkedList::Remove(int number){
Node *previousNode = head;
Node *currentNode = head;
while (currentNode->next_node){
previousNode = currentNode;
currentNode = currentNode->next_node;
if (currentNode->get_value() == number){
previousNode->next_node = currentNode->next_node;
delete currentNode;
count--;
return;
}
}
}
int LinkedList::GetValue(int index){
Node *currentNode = head;
int currentIndex = -1;
while (currentNode->next_node){
currentNode = currentNode->next_node;
currentIndex++;
if (index == currentIndex){
return currentNode->get_value();
}
}
return -1;
}
Node.h
#pragma once
class Node
{
private:
int value;
public:
Node(int);
~Node();
Node *next_node;
int get_value();
void set_value(int);
};
Node.cpp
#include "Node.h"
Node::Node(int _value)
{
value = _value;
}
Node::~Node()
{
}
int Node::get_value(){
return value;
}
void Node::set_value(int _value){
value = _value;
}
- 답변 # 1
- 답변 # 2
@Jamal의 훌륭한 제안 외에도 ...
물리적 레이아웃
와이즈 비즈 클래스의 섹션이 먼저 표시되고 그 뒤에
public
가 나타납니다. 섹션 및protected
섹션이 마지막에 나타납니다. 그 이유는private
클래스의 사용자에게 클래스의 섹션은 가장 눈에 띄는 인터페이스 인public
입니다. 섹션은 다음으로 보이는 인터페이스이며,protected
섹션이 가장 눈에 띄지 않는 인터페이스입니다.private
LinkedList :: Remove의 논리 오류
게시 된 코드는 주어진 입력이class LinkedList { public: LinkedList(); ... private: Node *head; int count; };
에 해당 할 가능성을 고려하지 않습니다. 목록의.head
- 답변 # 3
가장 중요한 점이 이미 언급되었습니다. 하지만 몇 가지 사소한 것들이 있습니다 :
이동성
void LinkedList::Remove(int number){ Node *previousNode = head; Node *currentNode = head; // You need this. if ( head->get_value() == number ) { Node* temp = head->next_node; delete head; head = temp; return; } while (currentNode->next_node){ previousNode = currentNode; currentNode = currentNode->next_node; if (currentNode->get_value() == number){ previousNode->next_node = currentNode->next_node; delete currentNode; count--; return; } } }
실제로는 비표준이므로 ExoticCompilerFoo를 지원하려면 일반적인 포함 가드 사용을 고려해야합니다.공백 공백
와이즈 비즈 함수는 목록 끝에 공백을 추가하지만 이미 알고있을 수도 있습니다.
유틸리티 기능
와이즈 비즈 실제로 클래스의 공용 인터페이스를 사용하여 함수를 구현할 수 있습니다. 공용 인터페이스를 공유하는 다른 클래스와 함께 사용할 수도 있습니다. 따라서 실제로 일반 독립형 함수로 작성할 수 있습니다.
이것은 예를 들어 표준 라이브러리가 알고리즘으로하는 것입니다.
#pragma once
Node
대신 게터와 세터를 사용하고, 그것을struct
로 만드십시오.private
에서 선언LinkedList
의 섹션 . 자체 파일도 필요하지 않습니다.함수 중 하나를 제외한 모든 함수가 PascalCase에 있습니다. 당신이 그것을 선택하든 낙타를 선택하든, 하나만 선택하고 유지하십시오. 일관성을 유지해야합니다.
새 연결 목록을 만들 때 헤드는 먼저
NULL
를 가리켜 야합니다 (또는nullptr
빈 목록을 나타냅니다. 대신 새 노드를 향한 헤드 포인트를 가지므로 시작시 목록이 실제로 비어있는 것으로 간주되지 않습니다.new
로 메모리를 할당하더라도 빈 소멸자가 있습니다. . 메모리 누수를 방지하려면 소멸자는delete
를 사용해야합니다. 할당 된 각Node
에 . 이것은 일반적으로 목록을 반복하여 수행됩니다.데이터 멤버를 수정하지 않는 멤버 함수는
const
여야합니다. . 또한 이러한 기능 내에서 데이터 멤버가 실수로 수정되는 것을 방지합니다.get_count()
의 예 기능 :