Conversation
LJLee37
left a comment
There was a problem hiding this comment.
전체적으로 짜임새가 괜찮은 코드지만 C스타일 코드라는 점이 아쉽습니다. 문제 드릴때 말씀드렸던 생성자, 메서드에 대한 부분 추가 바랍니다.
| #include <iostream> | ||
| using namespace std; | ||
|
|
||
| typedef struct Node{ |
There was a problem hiding this comment.
C++은 typedef없이 구조체 이름으로만 사용 가능합니다. 고치라는 뜻은 아니고 알아두시라는 뜻입니다. 적을게 한두가지가 아니라 라인별로 적도록 하겠습니다.
linkedList/210106_m_linkedList.cpp
Outdated
|
|
||
| int main(){ | ||
| //노드 생성 | ||
| node* head=(node*)malloc(sizeof(node)); //헤드 노드를 메모리 동적 할당으로 생성 |
There was a problem hiding this comment.
써도는 되지만, 굳이... 멀록을 쓰셨군요. 사용은 가능합니다만 new, delete가 더 편할겁니다.
| int data; | ||
| struct Node* next; //선언한 노드 구조체의 주소 저장 | ||
| } node; | ||
|
|
There was a problem hiding this comment.
생성자, 메서드 등을 하나도 안쓰셨군요. 수정 바랍니다.
| cout<<curr->data<<" "; //data 출력 | ||
| curr=curr->next; //첫번째로 생성한 노드의 newNode->next는 NULL이므로 반복문 탈출 | ||
| } | ||
|
|
There was a problem hiding this comment.
프로그램 종료 전에 메모리 할당을 해제해야 메모리 누수가 없습니다.
| } | ||
|
|
||
|
|
||
| void addNode(node* head, int data){ |
|
|
||
| } | ||
|
|
||
| void deleteNode(node* head){ //head의 주소만 받음 |
|
생성자, 메서드에 관한 부분은 추후에 추가하겠습니다... |
코딩 스타일을 보니 함수원형을 선언 하시고 메인 이후에 코드를 작성하시는 스타일인 것 같아요! |
LJLee37
left a comment
There was a problem hiding this comment.
delete[] 대신 delete를 사용하시는게 좋겠습니다.
| curr=curr->next; //첫번째로 생성한 노드의 newNode->next는 NULL이므로 반복문 탈출 | ||
| } | ||
|
|
||
| delete[]head; |
There was a problem hiding this comment.
head는 배열이 아니기 때문에 delete[]을 이용하면 안되고 각 노드를 모두 각각 할당해제한 후에 헤드를 할당해제해야 합니다.
| void deleteNode(node* head){ //head의 주소만 받음 | ||
| node* targetNode=head->next; //삭제할 노드의 주소가 들어갈 임시 변수 targetNode | ||
| head->next=targetNode->next; | ||
| delete[]targetNode; //할당한 targetNode의 메모리 공간을 해제 |
There was a problem hiding this comment.
각 노드는 배열이 아니기 때문에 delete[]가 아닌 delete를 사용해야 합니다.
늦어서 죄송합니다...