-
Notifications
You must be signed in to change notification settings - Fork 0
링크드리스트 과제... #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
링크드리스트 과제... #50
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| file name : 210106_m_linkedList | ||
| author : kimminju | ||
| created : 2021/01/10 | ||
| */ | ||
|
|
||
| /* | ||
| linked list : 프로그래밍에서 쓰이는 자료 구조의 일종 / 메모리 공간인 Node에 data, pointer 저장 | ||
| - Node 들이 한줄로 연결된 방식으로 데이터 저장 | ||
| - 자료의 추가, 삭제가 단 시간에 가능 / 배열, 트리에 비해 데이터를 검색하는 시간이 길어짐 | ||
| - Head : first Node / Tail : last Node / -> : Node를 가리키는 pointer | ||
| - 이중 연결리스트 (양방향) / 단일 연결리스트(단방향) / 원형 연결리스트(마지막노드->처음 노드) | ||
|
|
||
| Node - data를 저장할 변수 / 다음 node의 address를 저장할 자기참조 구조체 포인터 변수로 구성 | ||
| */ | ||
|
|
||
| #include <iostream> | ||
| using namespace std; | ||
|
|
||
| typedef struct Node{ | ||
| int data; | ||
| struct Node* next; //선언한 노드 구조체의 주소 저장 | ||
| } node; | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 생성자, 메서드 등을 하나도 안쓰셨군요. 수정 바랍니다. |
||
| void addNode(node*, int); | ||
| void deleteNode(node*); | ||
|
|
||
|
|
||
| int main(){ | ||
| //노드 생성 | ||
| node* head=new node; //헤드 노드를 메모리 동적 할당으로 생성 | ||
| head->next=NULL; //헤드 노드는 데이터를 저장하지 않음. 다음 노드는 NULL | ||
| int i, j=0; | ||
| int h, g=0; | ||
|
|
||
| cout<<"Add Node: How many nodes? "; | ||
| cin>>j; | ||
|
|
||
| for(i=0; i<j; i++){ | ||
| int num1=0; | ||
| cout<<"Enter num to add:"; | ||
| cin>>num1; | ||
| addNode(head, num1); | ||
| //head node와 그 다음 노드 사이에 새로운 node가 생성되므로, | ||
| //10 20 30 순서로 입력했다면 30 20 10 순으로 출력됨. | ||
|
|
||
| } | ||
|
|
||
|
|
||
| cout<<"del Node: How many nodes? "; | ||
| cin>>g; | ||
|
|
||
| for(h=0; h<g; h++){ | ||
| deleteNode(head); | ||
| } | ||
|
|
||
| // 결과 노드 출력 // | ||
| node* curr=head->next; //노드를 스캔할 curr 구조체 포인터에 머리노드가 가리키는 노드의 주소 대입 | ||
| while(curr!=NULL){ //curr에 저장된 값이 NULL이 아닐때 반복 (반복 시작) | ||
| cout<<curr->data<<" "; //data 출력 | ||
| curr=curr->next; //첫번째로 생성한 노드의 newNode->next는 NULL이므로 반복문 탈출 | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 프로그램 종료 전에 메모리 할당을 해제해야 메모리 누수가 없습니다. |
||
| delete[]head; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. head는 배열이 아니기 때문에 delete[]을 이용하면 안되고 각 노드를 모두 각각 할당해제한 후에 헤드를 할당해제해야 합니다. |
||
| } | ||
|
|
||
|
|
||
| void addNode(node* head, int data){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋습니다. |
||
| node* newNode=new node; | ||
| newNode->data=data; //newNode의 data에 data 저장 | ||
| newNode->next=head->next; //newNode의 next node는 head node가 가리키던 다음 주소 저장 | ||
| head->next=newNode; //head node의 next node는 newNode의 address 저장 | ||
| cout<<"Head Node 뒤에 new Node 추가 : "<<data<<endl; | ||
|
|
||
| } | ||
|
|
||
| void deleteNode(node* head){ //head의 주소만 받음 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 삭제도 좋습니다. |
||
| node* targetNode=head->next; //삭제할 노드의 주소가 들어갈 임시 변수 targetNode | ||
| head->next=targetNode->next; | ||
| delete[]targetNode; //할당한 targetNode의 메모리 공간을 해제 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 각 노드는 배열이 아니기 때문에 delete[]가 아닌 delete를 사용해야 합니다. |
||
| cout<<"Head node 뒤의 노드 삭제"<<endl; | ||
|
|
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C++은 typedef없이 구조체 이름으로만 사용 가능합니다. 고치라는 뜻은 아니고 알아두시라는 뜻입니다. 적을게 한두가지가 아니라 라인별로 적도록 하겠습니다.