Home Forums US Life c++ 아래 짧은 링크드 리스트 코드중 2가지 궁금한것 This topic has [10] replies, 0 voices, and was last updated 3 years ago by ee. Now Editing “c++ 아래 짧은 링크드 리스트 코드중 2가지 궁금한것” Name * Password * Email Topic Title (Maximum Length 80) `#include <bits/stdc++.h> using namespace std; class node { public: int data; node* next; node(int value) { data = value; next = NULL; } }; void insertAtHead(node*& head, int val) { node* n = new node(val); n->next = head; head = n; } void insertAfter(node* head, int key, int val) { node* n = new node(val); if (key == head->data) { n->next = head->next; head->next = n; return; } node* temp = head; while (temp->data != key) { temp = temp->next; // key를 만날때까지 전진 if (temp == NULL) { // key가 없는 경우 return; } } n->next = temp->next; temp->next = n; // head=temp; // 질문1: 이 temp 포인터는 코멘트할경우 어떻게 리턴되는것인가? } void print(node*& head) { node* temp = head; while (temp != NULL) { cout << temp->data << " -> "; temp = temp->next; } cout << "NULL" << endl; } int main() { node* head = NULL; insertAtHead(head, 1); insertAtHead(head, 2); insertAfter(head, 1, 3); insertAfter(head, 3, 6); print(head); cout << endl; return 0; }` 아웃풋: 2 -> 1 -> 3 -> 6 -> NULL 질문 1. 이게 주요 질문인데... 위의 함수 insertAfter( ) 에서 마지막 라인 // head=temp; 이걸 코멘트해도 실행에 문제가 없을까요? 아니면 문제가 생길까요? 아니면 결과가 다를까요? 실행에 문제가 없다면 이유가 뭘까요? (포인터에 이해에 관한것인데 헷갈림) 질문 2. 위 프로그램은 메모리 리크가 발생하는데...메모리 리크를 없애려면 어떻게 딜리트를 위에서 잘 이용해야 할까요? 그리고 혹시 재미로 연습삼아 해보고 싶은 분은 insertAfter() 를 응용해서 insertBefore() 를 한번 임플리멘트 해보세요. ㅎㅎ I agree to the terms of service Update List