• 01. ์ž๋ฃŒ๊ตฌ์กฐ lists: array, structure, linked list, stack, queue

    2020. 2. 22.

    by. ํ•ด๋Š”์„ 

    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 ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค. ๋งจ ์•„๋ž˜ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ €์žฅ์ด ๋œ๋‹ค. ์œ„๋Š” ๊ทธ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ์ธ๋ฑ์Šค๊ณ , ์•„๋ž˜๊ฐ€ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋‹ค.

    array

    ์ธ๋ฑ์‹ฑ(๊ทธ ๊ฐ’์— ์ ‘๊ทผ) ํ•˜๋Š” ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

    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์ด๋ผ๊ณ  ํ•œ๋‹ค.

     

    ์Šคํƒ์˜ ํŠน์„ฑ์„ ์‹œ๊ฐ์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด ๋ณด์•˜๋‹ค. ์ •๋ง ํ”„๋ง๊ธ€์Šค ํ†ต์ด๋ž‘ ๋˜‘๊ฐ™๋‹ค. ํ”„๋ง๊ธ€์Šค ๋จน๊ณ ์‹ถ๋„ค...

    Stack

    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๋ผ๊ณ  ํ•œ๋‹ค.

     

    ํ๋ฅผ ์‹œ๊ฐ์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ณด์•˜๋‹ค.

    Queue

    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

    ๋ฌธ์ œ๋กœ ํ’€์–ด๋ณด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜(ํ™ฉ์ธ์šฑ, ๊น€์šฉํ˜)

     

     

     

    ๋Œ“๊ธ€