From 84d6d54e13e99c2b42e3f299de7f44a9f340180e Mon Sep 17 00:00:00 2001 From: skyminju9 Date: Sun, 10 Jan 2021 23:25:07 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=EB=A7=81=ED=81=AC=EB=93=9C=EB=A6=AC?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EA=B3=BC=EC=A0=9C...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- linkedList/210106_m_linkedList.cpp | 84 ++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 linkedList/210106_m_linkedList.cpp diff --git a/linkedList/210106_m_linkedList.cpp b/linkedList/210106_m_linkedList.cpp new file mode 100644 index 0000000..7da8c12 --- /dev/null +++ b/linkedList/210106_m_linkedList.cpp @@ -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 +using namespace std; + +typedef struct Node{ + int data; + struct Node* next; //선언한 노드 구조체의 주소 저장 +} node; + +void addNode(node*, int); +void deleteNode(node*); + + +int main(){ + //노드 생성 + node* head=(node*)malloc(sizeof(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>num1; + addNode(head, num1); + //head node와 그 다음 노드 사이에 새로운 node가 생성되므로, + //10 20 30 순서로 입력했다면 30 20 10 순으로 출력됨. + + } + + + cout<<"del Node: How many nodes? "; + cin>>g; + + for(h=0; hnext; //노드를 스캔할 curr 구조체 포인터에 머리노드가 가리키는 노드의 주소 대입 + while(curr!=NULL){ //curr에 저장된 값이 NULL이 아닐때 반복 (반복 시작) + cout<data<<" "; //data 출력 + curr=curr->next; //첫번째로 생성한 노드의 newNode->next는 NULL이므로 반복문 탈출 + } + + return 0; +} + + +void addNode(node* head, int data){ + node* newNode=(node*)malloc(sizeof(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 추가 : "<next; //삭제할 노드의 주소가 들어갈 임시 변수 targetNode + head->next=targetNode->next; + free(targetNode); //할당한 targetNode의 메모리 공간을 해제 + cout<<"Head node 뒤의 노드 삭제"< Date: Mon, 11 Jan 2021 02:35:07 +0900 Subject: [PATCH 2/2] =?UTF-8?q?1=EC=B0=A8=20=EC=88=98=EC=A0=95=EC=82=AC?= =?UTF-8?q?=ED=95=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- linkedList/210106_m_linkedList.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/linkedList/210106_m_linkedList.cpp b/linkedList/210106_m_linkedList.cpp index 7da8c12..9a3cd16 100644 --- a/linkedList/210106_m_linkedList.cpp +++ b/linkedList/210106_m_linkedList.cpp @@ -28,7 +28,7 @@ void deleteNode(node*); int main(){ //노드 생성 - node* head=(node*)malloc(sizeof(node)); //헤드 노드를 메모리 동적 할당으로 생성 + node* head=new node; //헤드 노드를 메모리 동적 할당으로 생성 head->next=NULL; //헤드 노드는 데이터를 저장하지 않음. 다음 노드는 NULL int i, j=0; int h, g=0; @@ -61,12 +61,12 @@ int main(){ curr=curr->next; //첫번째로 생성한 노드의 newNode->next는 NULL이므로 반복문 탈출 } - return 0; + delete[]head; } void addNode(node* head, int data){ - node* newNode=(node*)malloc(sizeof(node)); + 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 저장 @@ -77,7 +77,7 @@ void addNode(node* head, int data){ void deleteNode(node* head){ //head의 주소만 받음 node* targetNode=head->next; //삭제할 노드의 주소가 들어갈 임시 변수 targetNode head->next=targetNode->next; - free(targetNode); //할당한 targetNode의 메모리 공간을 해제 + delete[]targetNode; //할당한 targetNode의 메모리 공간을 해제 cout<<"Head node 뒤의 노드 삭제"<