01. ์๋ฃ๊ตฌ์กฐ lists: array, structure, linked list, stack, queue
01. lists: array, stack, queue, structure, linked list
์๋ฃ๊ตฌ์กฐ, ๋ฆฌ์คํธ, ๋ฐฐ์ด, ์คํ, ํ, ๋งํฌ๋ ๋ฆฌ์คํธ
์ง๋ ํ๊ธฐ์ ๋ฐฐ์ด ๋ด์ฉ๋ค์ด๋ค. ์์ ํ์๋ค์ ์ํด ๋ณต์ต ๋๋์ผ๋ก ๋ฃ์ ๋ฏ ํ๋ค.
1. Array
๋ฐฐ์ด์ด๋ผ๊ณ ๋ ๋ถ๋ฅธ๋ค. ๋ฒํธ(์ธ๋ฑ์ค)์ ๋ฒํธ์ ๋์ํ๋ ๋ฐ์ดํฐ๋ค๋ก ์ด๋ฃจ์ด์ง ์๋ฃ๊ตฌ์กฐ๋ฅผ ๋ํ๋ธ๋ค.
์๋ฃํ์ด ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ผ๋ ฌ๋ก ๋์ดํ๋ ๊ฒ์ด ํน์ง์ด๋ฉฐ, ๋ณดํต ์ฒซ๋ฒ์งธ ์นธ์ ์ธ๋ฑ์ค๋ 0๋ถํฐ ์์ํ๋ค.
์๋ง c๋ฅผ ๋ฐฐ์ธ ๋ ๊ฐ์ฅ ๋จผ์ ๋ฐฐ์ฐ๋ ์๋ฃ๊ตฌ์กฐ๋ค.
์๋์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ ์ธ์ ํ ์ ์๋ค.
int a[5]; #์ ์ธ
int a[5] = {0, }; #์ ์ธ๊ณผ ์ด๊ธฐํ
int a[5] = {1, 2, 3, 4, 5}; #์ ์ธ๊ณผ ์ด๊ธฐํ
์๋ฃํ ์ด๋ฆ[๊ฐ์]
์ฒ์ ๋ฐฐ์ด์ ๋ง๋ค๊ฒ ๋๋ฉด ์์ ์ฐ๋ ๊ธฐ ๊ฐ์ด ๋ค์ด๊ฐ ์๊ธฐ ๋๋ฌธ์, ๊ผญ ์ด๊ธฐํ๋ฅผ ํด์ค์ผ ํ๋ค. 0์ผ๋ก ์ด๊ธฐํ ํ๋ ๊ฒฝ์ฐ,
{0, }
์ผ๋ก ์จ์ฃผ๊ฒ ๋๋ฉด ์๋์ผ๋ก 0์ผ๋ก ์ด๊ธฐํ๊ฐ ๋๋ค. ๋ค๋ฅธ ์๋ ์ ์ฉ๋์ง ์๋๋ค. ๋ง์ฝ, ๋ค๋ฅธ ์๋ก ์ ์ฒด ์ด๊ธฐํ๋ฅผ ํด์ฃผ๊ณ ์ถ๋ค๋ฉด memset ํจ์๋ฅผ ์ด์ฉํ๋ฉด ๋๋ค. ๋งจ ์๋ ์ฝ๋๊ฐ ์คํ๋๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์ ์ฅ์ด ๋๋ค. ์๋ ๊ทธ ๋ฐ์ดํฐ์ ์ ๊ทผํ๊ธฐ ์ํ ์ธ๋ฑ์ค๊ณ , ์๋๊ฐ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ค.
์ธ๋ฑ์ฑ(๊ทธ ๊ฐ์ ์ ๊ทผ) ํ๋ ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
print("%d", a[3]);
a[2] = 5;
a๋ฐฐ์ด์ intํ ๋ฐฐ์ด์ด๊ธฐ ๋๋ฌธ์ int์์์ง์ ์์ธ %d๋ฅผ ์ด์ฉํด์ ์ถ๋ ฅํ ์ ์๋ค. ๋ฐฐ์ด์ ๋ค๋ฅธ ์๋ฅผ ๋ฃ์ด์ฃผ๊ณ ์ถ๋ค๋ฉด ๋ฃ์ด์ฃผ๊ณ ์ถ์ ์ธ๋ฑ์ค์ ๋ค๋ฅธ ์๋ฅผ ๋์ ํด ์ฃผ๋ฉด ๋๋ค. ๋จ ์ฒ์์ a[n]์ผ๋ก ์ ์ธํ๋ค๋ฉด, ๋ฒ์๋ 0๋ถํฐ n-1๊น์ง ์์ ์์ง ๋ง์.
2. Structure
๊ตฌ์กฐ์ฒด๋ผ๊ณ ๋ถ๋ฅธ๋ค. ์ฝ๊ฒ ๋งํด์ ์ฌ๋ฌ ์๋ฃํ๋ค์ ํฉ์ณ์ ํ๋์ ์๋ก์ด ์๋ฃํ์ ๋ง๋๋ ๋ฐฉ๋ฒ! ์ด๋ผ๊ณ ํ ์ ์๋ค.
๊ตฌ์กฐ์ฒด ์์ ์๋ ๋ณ์๋ค์ ๋ฉค๋ฒ๋ผ๊ณ ๋ถ๋ฅธ๋ค.
๋ค์๊ณผ ๊ฐ์ด ์ ์ํ๋ค.
struct node {
int date;
char name[10];
int money = 100;
};
struct <tag_name> {
<type> <member_name1>;
<type> <member_name2>;
} ;
๋ค์ ๊ผญ ์ธ๋ฏธ์ฝ๋ก ; ์ ๋ถ์ฌ์ค์ผ ํ๋ค! (๋ญ vs์์ tapํค ๋๋ฅด๋ฉด ์์์ ๋ง๋ค์ด ์ฃผ๊ธด ํ๋๋ฐ)
์ฌ๊ธฐ์ node๊ฐ ์๋ก์ด ์๋ฃํ์ ์ด๋ฆ, ๊ทธ๋ฆฌ๊ณ ์์ data, name, money๊ฐ node์ member๊ฐ ๋๋ค.
์ ์ธ์ ๋ค์๊ณผ ๊ฐ์ด ํ ์ ์๋ค.
struct node a;
struct node a = {10, 'nana', 200};
์ ์ธ๋ง ํด๋ ๋๊ณ , ์ ์ธ๊ณผ ์ด๊ธฐํ๋ฅผ ๊ฐ์ด ํ ์๋ ์๋ค. ์ฌ์ง์ด๋ ์ ์, ์ ์ธ, ์ด๊ธฐํ๋ฅผ ๋์์ ํ ์๋ ์๋ค.
struct node {
int date;
char name[10];
int money = 100;
} a = {10, 'nana', 100};
๋ ์์ธํ ๋ถ๋ถ์ ์๋ ๋งํฌ๋ฅผ ์ฐธ์กฐํ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค.
https://dojang.io/mod/page/view.php?id=407
C ์ธ์ด ์ฝ๋ฉ ๋์ฅ: 48.0 ๊ตฌ์กฐ์ฒด ์ฌ์ฉํ๊ธฐ
์ง๊ธ๊น์ง ์๋ฃํ๋ณ๋ก ๋ณ์๋ฅผ ํ๋์ฉ ์ ์ธํด์ ์ฌ์ฉํ์ต๋๋ค. ํ์ง๋ง ํ๋ก๊ทธ๋จ์ ๋ง๋ค๋ค ๋ณด๋ฉด ๋ณ์ ํ๋ ๊ฐ๋ก๋ ์ฒ๋ฆฌํ๊ธฐ๊ฐ ํ๋ ์ํฉ์ด ๋ฐ์ํ์ฃ . ๋ง์ฝ ์ธ์ ์ ๋ณด๋ฅผ ์ฒ๋ฆฌํ๋ค๋ฉด ์ด๋ฆ, ๋์ด, ์ฃผ์ ๋ฑ์ ์ ์ฅํ ๋ณ์๊ฐ ํ์ํฉ๋๋ค. char name[20]; // ์ด๋ฆ int age; // ๋์ด char address[100]; // ์ฃผ์ name, age, address ๋ณ์์๋ ํ ์ฌ๋์ ์ ๋ณด๋ง ์ ์ฅํ ์ ์์ต๋๋ค. ์ฌ๋ฌ ๋ช ์ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ค๋ฉด name1, nam
dojang.io
3. linked list
๊ตฌ์กฐ์ฒด + ๋์ ํ ๋น์ ํฉ์์ผ๋ก ์ด๋ค์ง๋ ์ฐ๊ฒฐ ๋ฆฌ์คํธ! ๋๋ฌด ํท๊ฐ๋ ค์ ์ฒ์ ๋ฐฐ์ธ ๋ ๋จธ๋ฆฌ ๊นจ์ง๋ ์ค ์์๋ค.
์ด๋์ ๋ ํฌ๊ธฐ์ ๋ฐฐ์ด์ ์ก์์ผ ํ ์ง ๋ชจ๋ฅด๊ฒ ์ ๋, ์ฌ์ฉํ๋ฉด ์ข๋ค.
์ฐ๊ฒฐ๋ฆฌ์คํธ๋ ๋ ธ๋๊ฐ ๋ฐ์ดํฐ๋ฅผ ๋ด์ ์ฑ๋ก ์ฐ๊ฒฐ๋์ด ์๋ ์๋ฃ๊ตฌ์กฐ๋ค. ๋ฐ์ดํฐ์ ํฌ์ธํฐ๋ฅผ ๊ฐ๊ณ ์๋ ๋ ธ๋๋ค์ด ์ฐ๊ฒฐ๋์ด ์๋๋ฐ, ๋ ธ๋์ ํฌ์ธํฐ๋ ๋ค์ ๋ ธ๋๋ฅผ ๊ฐ๋ฆฌํจ๋ค. ์ฆ, ๋ ธ๋ = ๋ฐ์ดํฐ + ํฌ์ธํฐ ์ธ๋ฐ ๊ทธ ๋ ธ๋ ์์ ํฌ์ธํฐ๋ค์ด ๋ค์์ ๋ ธ๋๋ฅผ ๊ฐ๋ฆฌํค๋ ์ ์ด๋ค. ๋ณดํต, head๋ผ๋ ํฌ์ธํฐ๊ฐ ์ฒซ๋ฒ์งธ ๋ ธ๋๋ฅผ ๊ฐ๋ฆฌํค๊ณ , ๋ง์ง๋ง ๋ ธ๋์ธ tail์ ํฌ์ธํฐ๋ NULL์ ๊ฐ๋ฆฌํจ๋ค.
์ฐ๊ฒฐ๋ฆฌ์คํธ๋ ๋ฐฐ์ด๊ณผ๋ ๋ค๋ฅด๊ฒ ์ค๊ฐ์ ์ฝ์ ํ๊ฑฐ๋ ์ ๊ฑฐํ ๋, ์์๋ฅผ ์์ง์ฌ์ค ํ์๊ฐ ์๋ค. ๋์ , n๋ฒ์งธ ์์๋ฅผ ์ฐพ์ ๋, ์์์ ๋ถํฐ ํ์ด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ฐพ๋ ๊ณผ์ ์ด ์ค๋ ๊ฑธ๋ฆฐ๋ค.
์๋๋ ์ฝ๋๋ค.
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE {
int key;
struct NODE* next;
}Node;
typedef Node* Head; //head๋ฅผ node์ ํฌ์ธํฐ ๋ณ์๋ก ๋ณํ.
Node* findLostNode(Head head);
//๋ง์ง๋ง ๋
ธ๋ ์ฐพ๊ธฐ
Node* findLostNode(Head head) {
if (head == NULL)return NULL;
else {
Node* node = head;
while (node->next != NULL) {
node = node->next;
}
return node;
}
}
//๋ฆฌ์คํธ๊ฐ ๋น์ด์๋์ง ํ์ธ
int isEmpty(Head head) {
if (head == NULL)return 1;
else return 0;
}
//๋ชจ๋ ๋
ธ๋ ์ถ๋ ฅ
void printAllNode(Head head) {
if (isEmpty(head))return;
else {
NODE* node = head;
while (node) {
printf("%d\n", node->key);
node = node->next;
}
}
}
//๊ฒ์
NODE* searchNode(Head head, int key) {
Node* node = head;
if (isEmpty(head))return NULL;
else {
while (node) {
if (node->next->key == key) {
return node; //์ฐพ๋ ๋
ธ๋์ ์ ๋
ธ๋๋ฅผ ๋ฐํ
}
node = node->next;
}
}
return NULL;
}
//๋งจ ๋ค์ ์ฝ์
void insertLast(Head head, int key) { //๋์ค์ ์ ์ฐํ๊ฒ ๋์ฒํ๋ ค๋ฉด Node๋๊ฒจ์ฃผ๋๊ฒ ์ข์.
Node* lastnode = findLostNode(head);
Node* newNode = (NODE*)malloc(sizeof(NODE));
newNode->key = key;
newNode->next = NULL;
if (lastnode == NULL) {
head = newNode;
}
else {
lastnode->next = newNode;
}
}
//๋งจ ์์ ์ฝ์
void insertfirst(Head head, int key) {
Node* newNode = (NODE*)malloc(sizeof(NODE));
newNode->key = key;
if (isEmpty(head)) {
head = newNode;
newNode->next = NULL;
}
else {
newNode->next = head->next;
head = newNode;
}
}
//๋งจ ๋ค์ ์ญ์
void deleteLast(Head head) {
Node* node = head;
Node* preNode = head;
if (isEmpty(head))return;
else {
while (node->next != NULL) {
preNode = node;
node = node->next;
}
free(node);
preNode->next = NULL;
}
}
//๋งจ ์์ ์ญ์
void deletefirst(Head head) {
if (isEmpty(head))return;
else {
head = head->next->next;
free(head->next);
}
}
//์ญ์ ํ๊ณ ์ถ์ ์ ์ญ์ (์ค๊ฐ์ ๋
ธ๋ ์ญ์ )
void deleteNode(Head head, int key) {
if (isEmpty(head))return;
NODE* predel_node = searchNode(head, key); //์ญ์ ํ๊ณ ์ถ์ ์ ์ ๋
ธ๋๋ฅผ ๋ฐ์
NODE* nextnode = predel_node->next->next;
free(predel_node->next);
predel_node->next = nextnode;
}
int main(void) {
Head head = NULL;
int key;
while (1) {
printf("=================\n");
printf("1. ์์ ๋
ธ๋ ์ฝ์
\n");
printf("2. ์ค๊ฐ์ ๋
ธ๋ ์ฝ์
\n");
printf("3. ๋ค์ ๋
ธ๋ ์ฝ์
\n");
printf("4. ์์ ๋
ธ๋ ์ญ์ \n");
printf("5. ์ค๊ฐ์ ๋
ธ๋ ์ญ์ \n");
printf("6. ๋ค์ ๋
ธ๋ ์ญ์ \n");
printf("7. ๋ฆฌ์คํธ ์ถ๋ ฅ\n");
int num;
scanf("%d", &num);
switch (num) {
case 1:
printf("์์ ์
๋ ฅํ๊ณ ์ถ์ data๋ฅผ ์
๋ ฅํ์ธ์ : ");
scanf("%d", &key);
insertfirst(head, key);
break;
case 2:
printf("์ค๊ฐ์ ์
๋ ฅํ๊ณ ์ถ์ data๋ฅผ ์
๋ ฅํ์ธ์ : ");
scanf("%d", &key);
break;
case 3:
printf("๋์ ์
๋ ฅํ๊ณ ์ถ์ data๋ฅผ ์
๋ ฅํ์ธ์ : ");
scanf("%d", &key);
insertLast(head, key);
break;
case 4:
deletefirst(head);
break;
case 5:
int key;
printf("์ญ์ ํ๊ณ ์ถ์ data๋ฅผ ์
๋ ฅํ์ธ์ : ");
scanf("%d", &key);
deleteNode(head, key);
break;
case 6:
deleteLast(head);
break;
case 7:
printAllNode(head);
printf("\n");
break;
default:
return 0;
}
}
}
4. Stack
์คํ์ด๋ผ๊ณ ์ฝ๊ณ , ํ๋ง๊ธ์ค ํต์ด๋ผ๊ณ ๋ณธ๋ค!
์ด ์๋ฃํ์ ํ์ ์ ์ถ, Last in First out, LIFO๋ค. ๋ง์น ํ๋ง๊ธ์ค ํต ์ฒ๋ผ, ๋จผ์ ๋ค์ด๊ฐ๊ฒ ๋งจ ๋์ค์ ๋์ค๊ณ , ๋์ค์ ๋ค์ด๊ฐ๊ฒ ๋งจ ๋จผ์ ๋์จ๋ค๊ณ ์๊ฐํ๋ฉด ๋๋ค.
๋ค์ด๊ฐ๋ ๊ฒ์ push, ๋์ค๋ ๊ฒ์ pop์ด๋ผ๊ณ ํ๋ค.
์คํ์ ํน์ฑ์ ์๊ฐ์ ์ผ๋ก ๋ํ๋ด ๋ณด์๋ค. ์ ๋ง ํ๋ง๊ธ์ค ํต์ด๋ ๋๊ฐ๋ค. ํ๋ง๊ธ์ค ๋จน๊ณ ์ถ๋ค...
C์์ ์คํ์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋๊ฐ์ง๊ฐ ์๋๋ฐ, ๋ฐฐ์ด์ ์ด์ฉํ๋ ๊ฒ๊ณผ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํ๋ ๊ฒ์ด๋ค.
๋จผ์ ๋ฐฐ์ด์ ์ด์ฉํด stack์ ๋ง๋ ์ฝ๋์ด๋ค. ์ต๋ ํฌ๊ธฐ๋ 5๋ก ์ค์ ํ๋ค.
#include <stdio.h>
#define max 5
int ds[max];
int top = -1;
int get(int*);
int put(int);
int is_full();
int is_empty();
void printall();
int main(void) {
int is_while = 1;
while (is_while) {
printf("1. put\n2. get\n3. is_full\n4. is_empty\n5. printall\n");
int n, ck;
int item = NULL;
scanf("%d", &n);
switch (n) {
case 1:
scanf("%d", &item);
put(item);
break;
case 2:
ck = get(&item);
if(ck==0) printf("%d\n", item);
break;
case 3:
is_full();
break;
case 4:
is_empty();
break;
case 5:
printall();
break;
default:
is_while = 0;
break;
}
}
}
int get(int *item) {//pop
if (is_empty()) {printf("stack is empty!\n"); return -1;}
*item = ds[top];
top--;
return 0;
}
int put(int newitem) { //push
if (is_full()) { printf("stack is full!\n"); return -1; }
top++;
ds[top] = newitem;
return 0;
}
int is_full() {
if (top == max-1)return 1;
else return 0;
}
int is_empty() {
if (top == -1)return 1;
else return 0;
}
void printall() {
for (int i = 0; i <= top; i++) {
printf("%d ", ds[i]);
}
printf("\n");
}
๊ทธ๋ฐ๋ฐ ๋ฐฐ์ด๋ก ์คํ์ ๋ง๋ค๋ฉด ์ต๋ ํฌ๊ธฐ ์ด์์ ๋ฃ์ด์ค ์๊ฐ ์๋ค๋ ๋ฌธ์ ์ ์ด ์๋ค. ์ด๋ฅผ ๋ณด์ํ๊ธฐ ์ํด ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ก ๋ง๋ ์คํ์ด ์๋ค.
๋ค์์ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํด Stack์ ๋ง๋ ์ฝ๋์ด๋ค.
#include <stdio.h>
#include <stdlib.h>
struct node {int key; struct node* next;}; //์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ์ํ ๊ตฌ์กฐ์ฒด
void addElement(int);
void push(int);
void pop();
void print_all();
void empty();
struct node* head = NULL;
int top = -1;
int main(void) {
int is_true = 1, ck, value; //while์ ๊ณ์ ๋๋ฆด ๋ณ์ is_true
while (is_true) {
printf("Enter command (1.push / 2.pop / 3.empty / 4.print all)? ");
scanf("%d", &ck);
switch (ck) {
case 1: //push
printf("push value? ");
scanf("%d", &value);
push(value);
break;
case 2: //pop
pop();
break;
case 3: //empty ( all free )
empty();
break;
case 4: //print
print_all();
break;
default: //์ด ์ธ์ ์๊ฐ ๋ค์ด์์ ์, ์ข
๋ฃ.
is_true = 0;
break;
}
}
}
void addElement(int key)
{
//๋
ธ๋๋ฅผ ์๋ก ๋ง๋ค์ด์ ๊ฐ์ ๋ฃ์ด์ฃผ๊ณ ๋ง์ง๋ง์ NULL์ ์ค
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->key = key;
newnode->next = NULL;
struct node* ptr = head;
if (head == NULL) { //top == -1์ผ ๋, stack์ ๋ค์ด๊ฐ๋ ์ฒซ๋ฒ์งธ ๊ฐ ์ผ๋
head = newnode;
return;
}
while (ptr->next != NULL) { ptr = ptr->next; } //stack์ ์ด๋ฏธ ๊ฐ์ด ์์ ๋ ๋ง์ง๋ง ๋
ธ๋๊น์ง ์ฐพ์๊ฐ.
ptr->next = newnode; //๋ง์ง๋ง ๋
ธ๋ next์ ์๋ก๋ง๋ ๋
ธ๋๋ฅผ ์ฐ๊ฒฐ
}
void push(int k) {
if (top == 4) { //stack์ด ๊ฝ ์ฐผ์๋
printf("Cannot push anymore\n");
return;
}
top++;
addElement(k); //stack์ ๊ฐ์ ๋ฃ์ด์ฃผ๋ addElementํธ์ถ
}
void pop() {
if (top == -1) { //stack์ด ๋น์ด์์ ๋
printf("Stack is empty now\n");
return;
}
top--;
struct node* ptr = head;
if (top == -1) { //stack์ ์์๊ฐ 1๊ฐ๋ง ๋จ์์์ ๊ฒฝ์ฐ!
printf("%d\n", ptr->key);
free(ptr);
head = NULL; //์ด์ stack์ ์๋ฌด๊ฒ๋ ์์.
return;
}
while (ptr->next->next != NULL) {ptr = ptr->next;} //๊ทธ ์ธ์ ๊ฒฝ์ฐ
printf("%d\n", ptr->next->key);
free(ptr->next); //๋ง์ง๋ง ๋
ธ๋ ํ๋ฆฌํด์ค
ptr->next = NULL;
return;
}
void print_all()
{
struct node* ptr; // ๋ด๊ฐ ์ฒ๋ฆฌํ๊ณ (๊ฐ๋ฆฌํค๊ณ ) ์๋ ๋
ธ๋
ptr = head;
while (ptr != NULL) { //ํ๋์ฉ ํ๋ฆฐํธํ๋ฉด์ ptr์ด ๋ค๋ก ์ฎ๊ฒจ๊ฐ.
printf("%d ", ptr->key);
ptr = ptr->next;
}
printf("\n");
}
void empty() {
if (top == -1)return; //์ด๋ฏธ ๋น์ด์๋ค๋ฉด ๊ฑ ๋ฆฌํด
top = -1; //์ด์ ๋น๋๊น -1๋ฃ์ด์ค.
struct node* ptr = head;
struct node* temp;
while (ptr->next != NULL) { //ํ๋์ฉ ํ์ผ๋ฉด์ free
temp = ptr;
ptr = ptr->next;
free(temp);
}
head = NULL; //๊ฐ์ง๊ฒ ์๋ head์๋ NULL์ ์ค
}
C์์๋ ์ด๋ ๊ฒ ์ฝ๋๋ฅผ ์ง์ ์ง์ผํ์ง๋ง, C++ ๊ฐ์ ๊ฒฝ์ฐ STL๋ผ์ด๋ธ๋ฌ๋ฆฌ์ <stack>์ ์ฌ์ฉํ๋ฉด ์ ์ฝ๊ฒ ์คํ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ด์ฉํ ์ ์๋ค.
5. Queue
ํ๋ ๋ง์น ํธ์ค๊ฐ๋ค. ๋ค์ด๊ฐ๋ ์ ๊ตฌ์ ๋๊ฐ๋ ์ ๊ตฌ๊ฐ ์ ํด์ ธ ์๋ค! ๊ทธ๋ฐ๋ฐ ํต๋ก๊ฐ ์์ง๋ฅผ ์๋ ์์ผ๋, ๋จผ์ ๋ค์ด๊ฐ๋ฉด ๋จผ์ ๋์ค๊ณ , ๋์ค์ ๋ค์ด๊ฐ๋ฉด ๋์ค์ ๋์จ๋ค! ์ฆ, ์ ์ ์ ์ถ, First in Fisrt out, FIFO ๊ตฌ์กฐ๋ผ๊ณ ํ ์ ์๋ค.
๋ค์ด๊ฐ๋ ๊ฒ์ enqueue, ๋์ค๋ ๊ฒ์ dequeue๋ผ๊ณ ํ๋ค.
ํ๋ฅผ ์๊ฐ์ ์ผ๋ก ๋ํ๋ด๋ณด์๋ค.
C์์ ํ๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์, ์คํ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฐฐ์ด๊ณผ, ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํด์ ๋ง๋ค ์ ์๋ค.
๋จผ์ ๋ฐฐ์ด์ ์ด์ฉํด์ ๋ง๋ ์ฝ๋์ธ๋ฐ, ๋จ์ํ ๋ฐฐ์ด์ ์ด์ฉํด์ ๋ง๋ค๊ฒ ๋๋ค๋ฉด ๋ง์ฝ ํ์ 5๋ฒ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด๋ฒ๋ฆฐ๋ค๋ฉด, ๋ค์ ๋บ๋ค๊ณ ํด๋ ์ฌ๋ฌ๋ฒ ์ธ ์ ์๋ ์ฝ๋๊ฐ ๋์ด๋ฒ๋ฆฐ๋ค. ์ด๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด mod(%)๋ฅผ ์ด์ฉํด์ ์ํ ํ๋ก ๋ง๋ค๊ฒ ๋๋ค๋ฉด, ๋ค์๊ณผ ๊ฐ์ด ์งค ์ ์๋ค.
#include <stdio.h>
int ds[5] = { 0, };
int front = 0, back = 0;
int is_full() {
if (back - front == 5) return 1;
else return 0;
}
int is_empty() {
if (front == back) return 1;
else return 0;
}
void print_all() {
int i = front;
while (i != back) {
printf("%d ", ds[i%5]);
i++;
}
printf("\n");
}
int get(int* k) { //dequeue
if (is_empty()) {
printf("queue is empty\n");
return 1;
}
*k = ds[front%5];
front++;
return 0;
}
int put(int newitem) { //enqueue
if (is_full()) {
printf("queue is full\n");
return 1;
}
ds[back%5] = newitem;
back++;
return 0;
}
void main() {
int is_true = 1;
while (is_true) {
printf("enter (1. put/ 2.get/ 3.printall) ");
int n, item, ck;
scanf("%d", &n);
switch (n) {
case 1:
printf("what value? ");
scanf("%d", &item);
put(item);
break;
case 2:
ck = get(&item);
if(ck==0) printf("%d\n", item);
break;
case 3:
print_all();
break;
default:
is_true = 0;
break;
}
}
}
๋ค์์ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํด์ ๋ง๋ ํ์ธ๋ฐ, ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ก ํ๋ฅผ ๋ง๋ค ๋ ์ด๋๋ฅผ ๋จธ๋ฆฌ๋ก ํ ์ง ์๊ฐ์ ์ ํด์ผ ํ๋ค. ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ head์ tail์ด ์๋๋ฐ, ํ์ ์ ๊ตฌ๋ฅผ head๋ก ํ ์ง, ์ถ๊ตฌ๋ฅผ head๋ก ํ ์ง ์ ์๊ฐํด์ผ ํ๋ค๋ ๋ง์ด๋ค.
๋ด ์๊ฐ์, ์ถ๊ตฌ๋ฅผ head๋ก ํ๋ ๊ฒ์ด ๋ ํจ์จ์ ์ด๊ณ ์๊ฐ์ ์ ์ฝํ ์ ์๋ค.
์ฆ, ๋ค์๊ณผ ๊ฐ๋ค๊ณ ์๊ฐํ๋ฉด ํธํ๋ค. ๋ง์ฝ head๊ฐ ์ ๊ตฌ๊ณ tail์ด ์ถ๊ตฌ๋ผ๋ฉด, dequeue๋ฅผ ํ ๋, head๋ถํฐ tail๊น์ง ๋ฐ๋ณต๋ฌธ์ ๋๋ ค์ผ ํ ๊ฒ์ด๋ค. ํ์ง๋ง, head๊ฐ ์ถ๊ตฌ๋ผ๋ฉด, dequeue๋ฅผ ํ ๋ ํ๋ก ๋ฐ๋ณต๋ฌธ์ ๋ ํ์๊ฐ ์์ด ๋ฝ์์ค ์ ์๋ค. ์ฌ์ค head๋ฟ๋ง ์๋๋ผ tail๋ ๋ฐ๋ก ํฌ์ธํฐ๋ฅผ ๋ง๋ค์ด ์ค๋ค๋ฉด, ์ด๋ป๊ฒ ํ๋ ์๊ด์ ์๋ค.
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int key;
struct _node *next;
} node_t;
node_t *head = NULL, *tail = NULL;
void insert_node(int n) {
node_t *new_node = (node_t*)malloc(sizeof(node_t));
new_node->key = n;
new_node->next = NULL;
if (head == NULL) {
head = new_node;
tail = new_node;
}
else {
tail->next = new_node; //์๋ ๋ง์ง๋ง ๋
ธ๋์ ํฌ์ธํฐ
tail = new_node; //tail
}
}
int delete_node() {
node_t *node;
int r;
if (head == NULL) {
return -1;
}
node = head;
head = head->next;
if (head == NULL) {
tail = NULL;
}
r = node->key;
free(node);
return r;
}
reference
https://ko.wikipedia.org/wiki/%EB%B0%B0%EC%97%B4
https://dojang.io/mod/page/view.php?id=407
๋ฌธ์ ๋ก ํ์ด๋ณด๋ ์๊ณ ๋ฆฌ์ฆ(ํฉ์ธ์ฑ, ๊น์ฉํ)