Skip to content

Conversation

@skyminju9
Copy link
Contributor

늦어서 죄송합니다...

Copy link
Member

@LJLee37 LJLee37 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전체적으로 짜임새가 괜찮은 코드지만 C스타일 코드라는 점이 아쉽습니다. 문제 드릴때 말씀드렸던 생성자, 메서드에 대한 부분 추가 바랍니다.

#include <iostream>
using namespace std;

typedef struct Node{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++은 typedef없이 구조체 이름으로만 사용 가능합니다. 고치라는 뜻은 아니고 알아두시라는 뜻입니다. 적을게 한두가지가 아니라 라인별로 적도록 하겠습니다.


int main(){
//노드 생성
node* head=(node*)malloc(sizeof(node)); //헤드 노드를 메모리 동적 할당으로 생성
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

써도는 되지만, 굳이... 멀록을 쓰셨군요. 사용은 가능합니다만 new, delete가 더 편할겁니다.

int data;
struct Node* next; //선언한 노드 구조체의 주소 저장
} node;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자, 메서드 등을 하나도 안쓰셨군요. 수정 바랍니다.

cout<<curr->data<<" "; //data 출력
curr=curr->next; //첫번째로 생성한 노드의 newNode->next는 NULL이므로 반복문 탈출
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

프로그램 종료 전에 메모리 할당을 해제해야 메모리 누수가 없습니다.

}


void addNode(node* head, int data){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니다.


}

void deleteNode(node* head){ //head의 주소만 받음
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

삭제도 좋습니다.

@skyminju9
Copy link
Contributor Author

생성자, 메서드에 관한 부분은 추후에 추가하겠습니다...

@kimz1121
Copy link
Contributor

생성자, 메서드에 관한 부분은 추후에 추가하겠습니다...

코딩 스타일을 보니 함수원형을 선언 하시고 메인 이후에 코드를 작성하시는 스타일인 것 같아요!
보니까
PullRequest 중에서
https://github.com/DigitalHumans/JJepp/pull/46/files
여기 권순형씨의 작업 스타일과 비슷해 보이셔서 서로 참고해 보시면 좋을 것 같아 코멘트 남깁니다...!😊😊😊

Copy link
Member

@LJLee37 LJLee37 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete[] 대신 delete를 사용하시는게 좋겠습니다.

curr=curr->next; //첫번째로 생성한 노드의 newNode->next는 NULL이므로 반복문 탈출
}

delete[]head;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

head는 배열이 아니기 때문에 delete[]을 이용하면 안되고 각 노드를 모두 각각 할당해제한 후에 헤드를 할당해제해야 합니다.

void deleteNode(node* head){ //head의 주소만 받음
node* targetNode=head->next; //삭제할 노드의 주소가 들어갈 임시 변수 targetNode
head->next=targetNode->next;
delete[]targetNode; //할당한 targetNode의 메모리 공간을 해제
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

각 노드는 배열이 아니기 때문에 delete[]가 아닌 delete를 사용해야 합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants