Hello Ocean! 🌼

[μ•Œκ³ λ¦¬μ¦˜ μŠ€ν„°λ””] 자료ꡬ쑰_1 λ³Έλ¬Έ

Algorithm

[μ•Œκ³ λ¦¬μ¦˜ μŠ€ν„°λ””] 자료ꡬ쑰_1

bba_dda 2020. 7. 20. 20:05
λ°˜μ‘ν˜•
  1. λ°°μ—΄
  • 정적배열

    μš°λ¦¬κ°€ μƒκ°ν•˜λŠ” 일반적인 λ°°μ—΄. 크기가 κ³ μ •λ˜μ–΄ μžˆλ‹€.

  • 동적배열 (vector)

    쀑간에 값을 μΆ”κ°€ν•˜κ±°λ‚˜ μ‚­μ œν•  수 μžˆλ‹€.
    https://blockdmask.tistory.com/70
    이 λ§ν¬μ—μ„œ λ°°μ—΄μ˜ μ‚¬μš©λ²•μ„ μžμ„Έν•˜κ²Œ 배울 수 μžˆλ‹€.

  1. λ¬Έμžμ—΄

  2. λ°°μ—΄

    • μ—¬λŸ¬ κΈ€μžλ‘œ 이루어진 문자 그룹을 ν•˜λ‚˜μ˜ 자료둜 μ·¨κΈ‰ν•˜μ—¬ λ©”λͺ¨λ¦¬μ— μ—°μ†μ μœΌλ‘œ μ €μž₯ν•˜λŠ” 자료 ν˜•μ‹.
      https://blockdmask.tistory.com/338
  3. μ—°κ²°λ¦¬μŠ€νŠΈ

    • μ—°κ²° μžλ£Œκ΅¬μ‘°λŠ” 순차 자료ꡬ쑰 (exλ°°μ—΄)κ³Ό 달리 μ›μ†Œμ˜ 논리적인 μˆœμ„œμ™€ 물리적인 μˆœμ„œκ°€ μΌμΉ˜ν•˜μ§€ μ•Šμ•„λ„ λœλ‹€.

    • μ—°μ†ν•œ λ¬Όλ¦¬μ£Όμ†Œμ— μ˜ν•΄ μ›μ†Œ μˆœμ„œλ₯Ό ν‘œν˜„ν•˜λŠ” 것이 μ•„λ‹ˆλΌ, 각 μ›μ†Œμ— μ €μž₯λ˜μ–΄ μžˆλŠ” λ‹€μŒ μ›μ†Œμ˜ μ£Όμ†Œ(링크)에 μ˜ν•΄ μˆœμ„œκ°€ μ—°κ²°λ˜λŠ” κ΅¬ν˜„ 방식이기 λ•Œλ¬Έμ΄λ‹€.

    • λ…Έλ“œ λ‹¨μœ„λ‘œ λ©”λͺ¨λ¦¬κ°€ ν• λ‹Ήλ˜λ©°, μ‚½μž… 및 μ‚­μ œ μ—°μ‚° ν›„ 논리적인 μˆœμ„œκ°€ λ³€κ²½λ˜μ–΄λ„ 각 λ…Έλ“œμ˜ λ§ν¬μ •λ³΄λ§Œ λ³€κ²½λ˜κ³  물리적인 μœ„μΉ˜λŠ” λ³€κ²½λ˜μ§€ μ•ŠλŠ”λ‹€.

    • 포인터(링크)λ₯Ό 담을 곡간이 μΆ”κ°€μ μœΌλ‘œ ν•„μš”ν•œ λŒ€μ‹ μ—, μ‚½μž… μ‚­μ œ μ—°μ‚°μ‹œμ— 물리적 μˆœμ„œλ₯Ό λ§žμΆ”λ €λŠ” μ˜€λ²„ν—€λ“œκ°€ λ°œμƒν•˜μ§€ μ•ŠλŠ”λ‹€.

    • μ—°κ²° 자료ꡬ쑰의 λ…Έλ“œ
      <데이터, λ‹€μŒ μ›μ†Œμ˜ μ£Όμ†Œ>

https://medium.com/wasd/c-c-%EC%9E%90%EB%A3%8C%EA%B5%AC%EC%A1%B0-%EC%97%B0%EA%B2%B0-%EB%A6%AC%EC%8A%A4%ED%8A%B8-1-41a312399a8f

λ‹¨μˆœμ—°κ²°λ¦¬μŠ€νŠΈ

#include <stdio.h>
#include <stdlib.h>
typedef int element;
typedef struct ListNode{
   element data;
   struct ListNode *link;
}ListNode;
void remove_node(ListNode **phead, ListNode *p, ListNode *removed)
{
   if(p==NULL)
      *phead = (*phead)->link;
   else
      p->link = removed->link;
   free(removed);
}
void insert_node(ListNode **phead, ListNode *p, ListNode *new_node)//phead ν—€λ“œν¬μΈν„°λ₯Ό κ°€λ₯΄ν‚€λŠ” 포인터, p μ‚½μž…ν•  곳의 μ„ ν–‰ λ…Έλ“œ, new_node μ‚½μž…λ  λ…Έλ“œ 
{
   if((*phead)==NULL)
   {
      new_node->link = NULL;
      *phead = new_node; 
   }
   else if(p==NULL)
   {
      new_node->link = *phead;
      *phead = new_node;
   }
   else
   {
      new_node->link = p->link;
      p->link = new_node;
   }
}
void display(ListNode *head)
{
   ListNode *p = head;
   while (p!=NULL)
   {
      printf("%d->",p->data);
      p = p->link;
   }
   printf("\n");
}
ListNode* create_node(element data, ListNode *link)
{
   ListNode *new_node;
   new_node = (ListNode*)malloc(sizeof(ListNode));
   new_node->data = data;
   new_node->link = link;
   return new_node;
}

int main(void)
{
   ListNode *head = NULL;
   ListNode *p;

   insert_node(&head, NULL, create_node(10,NULL));
   insert_node(&head, NULL, create_node(20,NULL));
   insert_node(&head, head, create_node(30,NULL));
   display(head);

   //remove_node(&head,NULL,head);
   display(head);
   return 0;
}

μ›ν˜•μ—°κ²°λ¦¬μŠ€νŠΈ

#include <stdio.h>
typedef int element;
typedef struct DlistNode {
    element data;
    struct DlistNode *llink;
    struct DlistNode *rlink;
}DlistNode;

void init(DlistNode *phead) {
    phead->llink=phead;
    phead->rlink=phead;
}
void display(DlistNode *phead) {
    DlistNode *p;
    for(p=phead->rlink; p!=phead; p=p->rlink)
        printf("<--| %x | %d | %x |-->\n",p->llink, p->data, p->rlink);
    printf("\n");
}
void dinsert_node(DlistNode *before,DlistNode *new_node) {
    new_node->llink = before;
    new_node->rlink = before->rlink;
    before->rlink->llink = new_node;
    before->rlink=new_node;
}
void dremove_node(DlistNode *phead_node, DlistNode *removed) {
    if (removed == phead_node) return;
    removed->llink->rlink=removed->rlink;
    removed->rlink->llink=removed->llink;
    free(removed);
}
int main(void) {
    DlistNode head_node;
    DlistNode *p[10];
    init(&head_node);
    int i; 
    for(i=0;i<5;i++) {
        p[i] = (DlistNode *)malloc(sizeof(DlistNode));
        p[i]->data = i;
        dinsert_node(&head_node, p[i]);
    }
    dremove_node(&head_node, p[3]);
    display(&head_node);

    return 0;
}
  1. μŠ€νƒ
    • μ ‘μ‹œλ₯Ό μŒ“μ•„ μ˜¬λ¦¬λ“― 데이터λ₯Ό 차곑차곑 μŒ“μ•„μ˜¬λ¦° ν˜„νƒœλ‘œ 자료λ₯Ό κ΅¬μ„±ν•˜λŠ” 방식이닀.
    • 같은 ꡬ쑰와 같은 크기의 데이터λ₯Ό 정해진 λ°©ν–₯으둜만 μŒ“μ„ 수 있고, top으둜 μ •ν•œ ν•œ 곳으둜만 μ ‘κ·Όν•˜λ„λ‘ μ œν•œλ˜μ–΄ μžˆλ‹€.
    • top은 μ‚½μž…κ³Ό μ‚­μ œκ°€ μΌμ–΄λ‚˜λŠ” μœ„μΉ˜λ‘œ, ν˜„μž¬ μŠ€νƒμ˜ κ°€μž₯ μœ„μ— μžˆλŠ” λ°μ΄ν„°μ˜ μœ„μΉ˜κ°€ λœλ‹€.
    • μ‚½μž…
      top μœ„μΉ˜μ— μžˆλŠ” 데이터 μœ„μ— μƒˆλ‘œμš΄ 데이터가 μŒ“μ΄κ²Œ λœλ‹€. λ”°λΌμ„œ top은 μƒˆλ‘œ μ‚½μž…λœ λ°μ΄ν„°μ˜ μœ„μΉ˜λ‘œ κ°±μ‹ λœλ‹€.
    • μ‚­μ œ
      top μœ„μΉ˜μ— μžˆλŠ” 데이터가 μ‚­μ œλœλ‹€. λ”°λΌμ„œ top은 top λ°”λ‘œ μ•„λž˜μ— μœ„μΉ˜ν•œ λ°μ΄ν„°μ˜ μœ„μΉ˜λ‘œ κ°±μ‹ λœλ‹€
      κ°€μž₯ λ§ˆμ§€λ§‰μ— μ‚½μž…λœ 데이터가 κ°€μž₯ λ¨Όμ € μ‚­μ œλœλ‹€ (LIFO)

링크λ₯Ό μ΄μš©ν•΄ κ΅¬ν˜„ν•œ μŠ€νƒ

#include <stdio.h> 

typedef int element;

typedef struct StackNode {
    element item;
    struct StackNode *link;
}StackNode;

typedef struct{    //λ©”μΈν•¨μˆ˜μ— 탑 μ„ μ–Έν•΄λ„λ˜μ§€λ§Œ μŠ€νƒμ—¬λŸ¬κ°œμΈκ²½μš° λ•Œλ¬Έμ—μ”€   
    StackNode *top;
}StackType;

void init(StackType *s) {
    s->top=NULL;
}
int is_empty(StackType *s) {
    return (s->top==NULL);
}
void push(StackType *s, element item) { //μ‚½μž…  

    StackNode *temp = (StackNode *)malloc(sizeof(StackNode));
    temp->item=item;
    temp->link=s->top;
    s->top=temp;
} 
int pop(StackType *s) { //μ‚­μ œ  
    if(is_empty(s)) {
        printf("μŠ€νƒμ΄ λΉ„μ–΄μžˆμŒ\n");
        exit(1);
    }
    else {
        StackNode *temp=s->top;
        int item=temp->item;
        s->top =s->top->link;
        free(temp);
        return item;
    }
}

int main(void) {
    StackType s;
    init(&s);
    push(&s,10);
    push(&s,20);
    push(&s,30);
    printf("%d\n",pop(&s));
    printf("%d\n",pop(&s));
    printf("%d\n",pop(&s));
    printf("is_empty:%d\n",is_empty(&s));

    return 0;
}
  1. 큐
    μ‚½μž… μ‚­μ œμ˜ μœ„μΉ˜μ™€ 방법이 μ œν•œλœ μœ ν•œ μˆœμ„œ λ¦¬μŠ€νŠΈλΌλŠ” 점은 μŠ€νƒκ³Όμ˜ κ³΅ν†΅μ μ΄μ§€λ§Œ, 리슀트의 ν•œμͺ½ λμ—μ„œλŠ” μ‚½μž…λ§Œ, λ°˜λŒ€μͺ½ λμ—μ„œλŠ” μ‚­μ œ μž‘μ—…λ§Œ μ΄λ£¨μ–΄μ§„λ‹€λŠ” 것이 μŠ€νƒκ³Ό λ‹€λ₯΄λ‹€.
    μ€ν–‰μ—μ„œ λ²ˆν˜Έν‘œλ₯Ό 뽑아 쀄을 μ„œλŠ” 방식과 λΉ„μŠ·ν•˜λ‹€. λ¨Όμ € μ‚½μž…λœ 데이터가 λ¨Όμ € μ‚­μ œλ˜λŠ” (FIFO) ꡬ쑰둜 μš΄μ˜λœλ‹€.
    ν•œμͺ½ 끝을 front(머리), λ‹€λ₯Έμͺ½ 끝을 rear or tail (꼬리)둜 μ •ν•˜μ—¬ λ¨Έλ¦¬μ—μ„œλŠ” μ‚­μ œλ§Œ, κΌ¬λ¦¬μ—μ„œλŠ” μ‚½μž… μ—°μ‚°λ§Œ μˆ˜ν–‰ν•  수 μžˆλ‹€.
#include <stdio.h>
#include <stdlib.h>
typedef int element;
typedef struct QueueNode {
    element item;
    struct QueueNode *link;
}QueueNode;
typedef struct {
    QueueNode *front,*back;
}QueueType;

void init(QueueType *q) {
    q->front=q->back=NULL;
}
int is_empty(QueueType *q) {
    return(q->front==NULL);
}
void enqueue(QueueType *q,element item) {
    QueueNode *temp=(QueueNode*)malloc(sizeof(QueueNode));
    if(temp==NULL) printf("λ©”λͺ¨λ¦¬λ₯Ό ν• λ‹Ήν•  수 μ—†μŠ΅λ‹ˆλ‹€.");
    else {
        temp->item=item;
        temp->link=NULL;
        if(is_empty(q)){ //큐가 곡백이면  
            q->front=temp;
            q->back=temp;
        }
        else {
            q->back->link=temp;
            q->back=temp;
        }
    }
}
element dequeue(QueueType *q) {
    QueueNode *temp=q->front;
    element item;
    if(is_empty(q)) printf("큐가 λΉ„μ–΄ μžˆμŠ΅λ‹ˆλ‹€.");
    else {
        item=temp->item;
        q->front=q->front->link;
        if(q->front==NULL)
            q->back=NULL; //ν•˜λ‚˜λ‚¨μ•˜μ„ λ•Œ μ‚­μ œν•˜λ©΄  
        free(temp);
        return item;
    }
    return -1;
} 

int main(void) {
    QueueType *q;
    init(&q);

    enqueue(&q,100);
    enqueue(&q,200);
    enqueue(&q,300);
    printf("dequeue()=%d\n", dequeue(&q));
    printf("dequeue()=%d\n", dequeue(&q));
    printf("dequeue()=%d\n", dequeue(&q));
    return 0;
    }
  • ν™˜ν˜• 큐
    1차원 배열을 μ΄μš©ν•΄μ„œ 큐λ₯Ό κ΅¬ν˜„ν–ˆμ„ λ•ŒλŠ” 큐가 ν¬ν™”μƒνƒœκ°€ 아닐 λ•Œλ§Œ μ‚½μž… 연산을 μˆ˜ν–‰ν•˜λ„λ‘ λ˜μ–΄μžˆλŠ”λ°, μ‹€μ œλ‘œ λ©”λͺ¨λ¦¬κ°€ λ‹€ 차지 μ•Šκ³  쀑간이 λΉ„μ–΄μžˆμŒμ—λ„ λΆˆκ΅¬ν•˜κ³  ν¬ν™”μƒνƒœλ‘œ μΈμ‹ν•΄μ„œ 더 이상 μ‚½μž… 연산을 μˆ˜ν–‰ν•˜μ§€ μ•ŠλŠ”λ‹€.
    이것을 ν•΄κ²°ν•˜κΈ° μœ„ν•΄ μ›ν˜• 큐λ₯Ό μ‚¬μš©ν•œλ‹€. 1차원 배열을 μ‚¬μš©ν•˜μ§€λ§Œ, λ…Όλ¦¬μ μœΌλ‘œ λ°°μ—΄μ˜ 처음과 끝이 μ—°κ²°λ˜μ–΄ μžˆλŠ” μ›ν˜• ꡬ쑰둜 κ°€μ •ν•˜κ³  μ‚¬μš©ν•œλ‹€.

    • μ‚½μž… μœ„μΉ˜
      rear = (rear+1)mod n (μ›ν˜• 큐)
      rear = rear+1 (순차 큐)

    • μ‚­μ œ μœ„μΉ˜
      front = (front+1) mod n (μ›ν˜• 큐)
      front = front+1 (순차 큐)

      #include <stdio.h>
      #include <stdlib.h>
      #define SIZE 4
      
      typedef char element;
      typedef struct {
          element queue[SIZE];
          int front, rear;
      } QueueType;
      
      QueueType *init() {
          QueueType *q;
          q = (QueueType *)malloc(sizeof(QueueType));
          q->front=0;
          q->rear=0;
          return q;
      }
      int isEmpty(QueueType* q){
          if (q->front == q->rear){
              printf("q is empty");
              return 1;
          }
          else return 0;
      }
      int isFull(QueueType *q){
          if (((q->rear)%SIZE)==q->front){
              printf("q is full");
              return 1;
          }
          else return 0;
      }
      void enqueue(QueueType *q, element item){
          if (isFull(q)) return;
          else {
              q->rear = (q->rear+1)%SIZE;
              q->queue[q->rear]=item;
          }
      }
      element dequeue(QueueType *q){
          if (isEmpty(q)) exit(1);
          else {
              q->front = (q->front+1)%SIZE;
              return q->queue[q->front];
          }
      }
      int main(void){
          QueueType *q=init();
          element data;
          //μ‚½μž…
          enqueue(q,'A');
          enqueue(q,'B');
          data = dequeue(q); 
          return 0;
      }
  1. 덱

    • 큐 λ‘κ°œ 쀑 ν•˜λ‚˜λ₯Ό 쒌우둜 λ’€μ§‘μ–΄μ„œ 뢙인 ꡬ쑰둜, 큐의 μ–‘μͺ½μ—μ„œ μ‚½μž…,μ‚­μ œ 연산을 λͺ¨λ‘ μˆ˜ν–‰ν•  수 μžˆλ„λ‘ ν™•μž₯ν•œ ꡬ쑰이닀.

      #include <stdio.h>
      #include <stdlib.h>
      
      typedef char element;
      typedef struce Node{
      	element data;
      	struct Node *llink;
      	struct Node *rlink;
      }Node;
      typedef struct{
      	Node *front, *rear;
      }DQueType;
      DQueType *init(){
      	DQueType *dq;
      	dq = (DQueType *)malloc(sizeof(DQueType));
      	dq->front = NULL;
      	dq->rear = NULL;
      	return dq
      }
      int isEmpty(DQueType *dq){
      	if (dq->front==NULL){
      		printf("dq is empty");
      		return 1;
      	}
      	else return 0;
      }
      void insertFront(DQueType *dq, element item){
      	Node *newNode = (Node *)malloc(sizeof(Node));
      	newNode->data = item;
      	if (dq->front==NULL){
      		dq->front=newNode;
      		dq->rear=newNode;
      		newNode->rlink=NULL;
      		newNode->llink=NULL;
      	}
      	else {
      		dq->front->llink = newNode;
      		newNode->rlink=dq->front;
      		dq->front=newNode;
      	}
      }
      void insertRear(DQueType *dq, element item){
      	Node *newNode = (Node *)malloc(sizeof(Node));
      	newNode->data = item;
      	if (dq->rear==NULL){
      		dq->front=newNode;
      		dq->rear=newNode;
      		newNode->rlink=NULL;
      		newNode->llink=NULL;
      	}
      	else {
      		dq->rear->llink = newNode;
      		newNode->rlink=dq->front;
      		dq->rear=newNode;
      	}
      }
      element deleteFront(DQueType *dq){
      	Node *old = dq->front;
      	element item;
      	if(isEmpty(dq)) return 0;
      	else {
      		item = old->data;
      		if(dq->front->rlink==NULL){
      			dq->front=NULL;
      			dq->rear=NULL;
      		}
      		else{
      			dq->front=dq->front->rlink;
      			dq->front->llink=NULL;
      		}
      		free(old);
      		return item;
      	}
      }
      element deleteRear(DQueType *dq){
      	Node *old = dq->rear;
      	element item;
      	if(isEmpty(dq)) return 0;
      	else {
      		item = old->data;
      		if(dq->rear->rlink==NULL){
      			dq->front=NULL;
      			dq->rear=NULL;
      		}
      		else{
      			dq->rear=dq->front->rlink;
      			dq->rear->llink=NULL;
      		}
      		free(old);
      		return item;
      	}
      }
      int main(void){
      	DQueType *dq = init();
      	element data;
      	insertFront(dq,'A');
      	insertRear(dq,'A');
      	data=deleteFront(dq);
      	return 0;
      }

 

λ°˜μ‘ν˜•