List

๋ฆฌ์ŠคํŠธ๋ž€ ์ˆœ์„œ๋ฅผ ๊ฐ€์ง„ ํ•ญ๋ชฉ๋“ค์˜ ๋ชจ์ž„์ด๋‹ค.

๋ฆฌ์ŠคํŠธ ์—ฐ์‚ฐ

  • ์ƒˆ๋กœ์šด ํ•ญ๋ชฉ์„ ๋ฆฌ์ŠคํŠธ์˜ ์ฒ˜์Œ, ์ค‘๊ฐ„, ๋์— ์ถ”๊ฐ€

  • ๊ธฐ์กด์˜ ํ•ญ๋ชฉ or ๋ชจ๋“  ํ•ญ๋ชฉ ์‚ญ์ œ

  • ๊ธฐ์กด ํ•ญ๋ชฉ ๋ณ€๊ฒฝ(๋Œ€์น˜)

  • ํŠน์ • ํ•ญ๋ชฉ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ๊ฒ€์ƒ‰

  • ํŠน์ •์œ„์น˜ ํ•ญ๋ชฉ ๋ฐ˜ํ™˜

  • ๋ฆฌ์ŠคํŠธ ๊ธธ์ด

  • ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ์ฐผ๋Š”์ง€

  • ๋ชจ๋“  ํ•ญ๋ชฉ ์ถœ๋ ฅ

๋ฆฌ์ŠคํŠธ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

์ˆœ์ฐจ์ ์ธ ๋ฐฐ์—ด์„ ์ด์šฉ

img
  • ๊ตฌํ˜„์ด ๊ฐ„๋‹จํ•˜๋‹ค.

  • ์‚ฝ์ž… ๋˜๋Š” ์‚ญ์ œ ์‹œ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ํ•ญ๋ชฉ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ œํ•œ๋œ๋‹ค.

์ด๋•Œ 1์ฐจ์› ๋ฐฐ์—ด์— ํ•ญ๋ชฉ๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•ด ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. ์‚ฝ์ž… / ์‚ญ์ œ์‹œ ํ•ญ๋ชฉ๋“ค์„ ์ด๋™ํ•ด์•ผํ•œ๋‹ค.

  • ์ด๋™ํšŸ์ˆ˜ = ๋งˆ์ง€๋ง‰ ์›์†Œ ์ธ๋ฑ์Šค - ์‚ฝ์ž…ํ•  ์ž๋ฆฌ ์ธ๋ฑ์Šค +1

์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉ

ํ•˜๋‚˜์˜ ๋…ธ๋“œ๊ฐ€ ๋ฐ์ดํ„ฐ์™€ ๋งํฌ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๊ณ  ๋งํฌ๊ฐ€ ๋…ธ๋“œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.

  • ๋…ธ๋“œ(node ) = ๋ฐ์ดํ„ฐํ•„๋“œ + ๋งํฌํ•„๋“œ

    • ๋ฐ์ดํ„ฐ ํ•„๋“œ : ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ, ์ฆ‰ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ณณ

    • ๋งํฌ ํ•„๋“œ : ๋‹ค๋ฅธ ๋…ธ๋“œ์˜ ์ฃผ์†Œ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ์žฅ์†Œ(ํฌ์ธํ„ฐ)

์ด๋•Œ ๋ฉ”๋ชจ๋ฆฌ์•ˆ์—์„œ์˜ ๋…ธ๋“œ์˜ ๋ฌผ๋ฆฌ์  ์ˆœ์„œ๊ฐ€ ๋ฆฌ์ŠคํŠธ์˜ ๋…ผ๋ฆฌ์  ์ˆœ์„œ์™€ ์ผ์น˜ํ•  ํ•„์š”๋Š” ์—†๋‹ค.

  • Head pointer : ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ณ€์ˆ˜

  • Tail : ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ณ€์ˆ˜

์žฅ์ 

  1. ์‚ฝ์ž…, ์‚ญ์ œ๊ฐ€ ํšจ์œจ์ ์ด๋‹ค.

  2. ์—ฐ์†๋œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์ด ํ•„์š”์—†๋‹ค.

  3. ํฌ๊ธฐ ์ œํ•œ์ด ์—†๋‹ค.

๋‹จ์ 

  1. ๊ตฌํ˜„์ด ์–ด๋ ต๋‹ค.

  2. ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ์‰ฝ๋‹ค.

์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ์ข…๋ฅ˜

๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

  • ํ•˜๋‚˜์˜ ๋งํฌ ํ•„๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์—ฐ๊ฒฐํ•œ ๋ฆฌ์ŠคํŠธ์ด๋‹ค.

  • ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ ๋งํฌ ๊ฐ’์„ NULL

์‚ฝ์ž…

  • ๋งจ ์•ž์— ์‚ฝ์ž…

void add_first(Node **head, int data){
    Node *p = new_node();
    p->data = data;
    p->next = *head;
    *head = p;
}
  • ์ค‘๊ฐ„์— ์‚ฝ์ž…

void add(Node *head, Node *tail, int data){
    Node *p = head;
    Node *new = NULL;
    while(p->next->data <= data && p->next!=NULL)
        p=p->next;

    if(p->data == data){
        printf("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ์ž…๋‹ˆ๋‹ค.");
        return;
    }
    new = new_node();
    new->next = p->next;
    new->data = data;
    p->next = new;
}
  • ๋งˆ์ง€๋ง‰์— ์‚ฝ์ž…

void add_last(Node **tail, int data){
    Node *new = new_node();
    Node *p = *tail;

    new->data = data;
    new->next = NULL;
    p->next = new;
    *tail = new;
}
  • ์‚ฝ์ž…

void create_list(Node **head,Node **tail){

    Node *p = *head;

    int data;

    while(1){
        printf("๊ณ„์ˆ˜์™€ ์ฐจ์ˆ˜๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.(0์€ ์ข…๋ฃŒ) : ");
        scanf("%d",&data);

        if(!data)break;

        if(p==NULL){
            p=new_node(data);
            (*head)=(*tail)=p;
        }else{
            while(p->next!=NULL)
                p=p->next;
            if(data<(*head)->data)add_first(head, data);
            else if(data>(*tail)->data)add_last(tail, data);
            else add(*head, data);
        }
    }
}

์‚ญ์ œ

  • ๋…ธ๋“œ ์‚ญ์ œ

void delete_node(Node **head, Node **tail, int data){
    Node *p = *head;
    Node *tmp=NULL;

    while(p!=NULL){
        if(p->data == data)break;
        tmp = p;
        p = p->next;
    }

    if(p==NULL){
        printf("๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์ ธ์žˆ๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๊ณ  ์‹ถ์€ ๋ฐ์ดํ„ฐ(%d)๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.\n",data);
        return;
    }

    if( p == *head ){

        *head = p->next;
        free(p);
        return;
    }
    tmp->next = p->next;
    free(p);
    if(tmp->next==NULL) *tail = tmp;
    printf("๋ฐ์ดํ„ฐ(%d)๋ฅผ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ญ์ œํ–ˆ์Šต๋‹ˆ๋‹ค. \n",data);
}
  • ์ „์ฒด์‚ญ์ œ

void delete_list(Node **head){
    Node *p = *head;
    Node *tmp = NULL;

    while(p!=NULL){
        tmp=p->next;
        free(p);
        p=tmp;
    }
    *head = NULL;
}

์ถœ๋ ฅ

int print_node(Node *head){
    Node *p = head;
    int i=0;

    while(p!=NULL){
        i++;
        printf("[%d] : %d ",i,p->data);
        p=p->next;
    }
    printf("\n\n");
    return i;
}

์ˆ˜์ •

void edit_node(Node **head,int before,int after){
    Node *p = *head;
    while(p!=NULL){
        if(p->data==before){
            p->data=after;
            return;
        }
    }
    printf("๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์ ธ์žˆ๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.\n");
}

์›ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

์›ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ ๋งํฌ๊ฐ€ ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฆฌ์ŠคํŠธ์ด๋‹ค. ํ•˜๋‚˜์˜ ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ๋…ธ๋“œ๋กœ์˜ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

๋ณดํ†ต ํ—ค๋“œํฌ์ธํ„ฐ๊ฐ€ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๊ตฌ์„ฑํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ์ฒ˜์Œ์ด๋‚˜ ๋งˆ์ง€๋ง‰์— ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ์—ฐ์‚ฐ์ด ๋งค์šฐ ๊ฐ„๋‹จํ•ด์ง„๋‹ค.

์‚ฝ์ž…

๋‹ค๋ฅธ ์—ฐ์‚ฐ์€ ๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ™๋‹ค.

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    int data;
    struct node *next;
} Node;

Node *new_node(int data){
    Node *new = (Node *)malloc(sizeof(Node));
    new->data = data;

    return new;
}

/*
 * p->next = p;
 * new->next = head
 */
// ์›ํ˜•์—์„œ๋Š” ํ˜„์žฌ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” head์˜ ์œ„์น˜๊ฐ€ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์ด๋ฉฐ, head->next๋Š” ๋งจ ์•ž๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

int insert_node(Node *head, int data){
    Node *p = NULL, *new;
    p=head;

    while(p->next->data <=  data )
        p=p->next;

    if(data == p->data){
        printf("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค.\n");
        return 0;
    }

    new = new_node(data);

    new->next = p->next;
    p->next = new;

    return 1;
}

int push_back(Node **head, int data){
    Node * new = new_node(data);
    Node * p = *head;

    if(p->data == data){
        printf("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค.\n");
        return 0;
    }

    new->next = p->next;
    p->next = new;

    (*head)=new;
    return 1;
}

void create_node(Node **head){

    Node *p = NULL;
    int data,i=0,j=0;


    while(1){
        printf("์‚ฝ์ž…ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”: (์ข…๋ฃŒ๋Š” 0)");
        scanf("%d",&data);
        if(!data)break;
        if(p==NULL){
            p=new_node(data);
            p->next = p;
            *head = p;
            i++;
        }else{
            p=*head;
            if (data >= (*head)->data) j=push_back(head,data);
            else j=insert_node(*head, data);
        }
        i+=j;
    }
    return i;

}

์—ฌ๊ธฐ์„œ head ํฌ์ธํ„ฐ๊ฐ€ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋งŒ๋“ค์–ด์„œ ์‚ฝ์ž… ํ•จ์ˆ˜๋ฅผ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

์ถœ๋ ฅ

//๋ณด์—ฌ์ค„๋•Œ๋Š” length๋ฅผ ๋ณด๋‚ด์ค˜์„œ ๋ช‡๊ฐœ์˜ ๋…ธ๋“œ๊ฐ€ ์žˆ๋Š”์ง€ ๋ฐ›์€ ํ›„์— ๋Œ๋ ค์•ผํ•œ๋‹ค.
void show_node(Node *head, int length){
    Node *p = head->next;
    int i;
    for(i=0;i<length;i++){
        printf("[%d] : %d\n",i+1,p->data);
        p=p->next;
    }
}

์‚ญ์ œ

void delete_node(Node **head,int data){
    Node *p = (*head)->next , *tmp=NULL;


    if(p==NULL){
        printf("๋น„์–ด์žˆ๋Š” ์Šคํƒ์ž…๋‹ˆ๋‹ค.\n");
        return;
    }

    while(p->data!=data){
        tmp = p;
        p=p->next;
    }

    if(p==(*head)->next){
        if(p->data==data&&)tmp=(*head);
        else{
            printf("์ฐพ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.\n");
            return;
        }
    }

    tmp->next = p->next;
    if(p==*head)*head = tmp;
    free(p);
    count--;
}

void delete_list(Node **head){
    Node *p = *head ,*tmp=NULL;

    while(p->next!=*head){
        tmp = p;
        free(p);
        count--;
        p=tmp->next;
    }

    free(p);

    (*head)=NULL;
}

์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ์„ ํ–‰ ๋…ธ๋“œ(prev)๋ฅผ ์ฐพ๊ธฐ ํž˜๋“  ๋‹จ์ ์ด ์žˆ๋‹ค. ์‚ฝ์ž… ๋˜๋Š” ์‚ญ์ œ ์‹œ์—๋Š” ๋ฐ˜๋“œ์‹œ ์„ ํ–‰ ๋…ธ๋“œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ํ•˜๋‚˜์˜ ๋…ธ๋“œ๊ฐ€ ์„ ํ–‰ ๋…ธ๋“œ์™€ ํ›„์† ๋…ธ๋“œ์— ๋Œ€ํ•œ ๋‘ ๊ฐœ์˜ ๋งํฌ๋ฅผ ๊ฐ€์ง€๋Š” ๋ฆฌ์ŠคํŠธ์ด๋‹ค. ๋งํฌ๊ฐ€ ์–‘๋ฐฉํ–ฅ์ด๋ฏ€๋กœ ์–‘๋ฐฉํ–ฅ ๊ฒ€์ƒ‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

  • ๋‹จ์ 

    • ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋” ์‚ฌ์šฉํ•ด์•ผํ•˜๋ฏ€๋กœ ๊ทธ๋งŒํผ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.

    • ๊ตฌํ˜„์ด ๋ณต์žกํ•ด์ง„๋‹ค.

  • ํ—ค๋“œ๋…ธ๋“œ : ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๊ณ  ๋‹จ์ง€ ์‚ฝ์ž…, ์‚ญ์ œ ์ฝ”๋“œ๋ฅผ ๊ฐ„๋‹จํ•œ๊ฒŒ ํ•  ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.(ํ—ค๋“œ ํฌ์ธํ„ฐ์™€ ๊ตฌ๋ณ„์ด ํ•„์š”ํ•˜๋‹ค.)

๋…ธ๋“œ ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ„

typedef struct node{
    int data;
    struct node * prev;
    struct node * next;
}Node;

์‚ฝ์ž…

  • ๋งจ ์•ž ์‚ฝ์ž…

void add_first(Node **head, int data){
    Node *p = new_node();
    p->data = data;
    p->next = *head;
    p->prev = NULL;
    (*head)->prev = p;
    *head = p;
}
  • ์ค‘๊ฐ„ ์‚ฝ์ž…

//๋ฆฌ์ŠคํŠธ์˜ ์ค‘๊ฐ„์— ์ถ”๊ฐ€
void add(Node *head, Node *tail, int data){
    Node *p = head;
    Node *new = NULL;
    while(p->next->data <= data)
        p=p->next;

    if(p->data == data){
        printf("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ์ž…๋‹ˆ๋‹ค.\n");
        return;
    }
    new = new_node();
    new->next = p->next;
    new->data = data;
    new->prev = p;
    p->next->prev = new;
    p->next = new;
}
  • ๋งจ ๋’ค ์‚ฝ์ž…

// ๋ฆฌ์ŠคํŠธ์˜ ๋์— ์ถ”๊ฐ€
void add_last(Node **tail, int data){
    Node *new = new_node();
    Node *p = *tail;

    new->data = data;
    new->next = NULL;
    new->prev = p;
    p->next = new;
    *tail = new;
}

์‚ญ์ œ

void delete_node(Node **head, Node **tail, int data){
    Node *p = *head;
    Node *tmp=NULL;

    while(p!=NULL){
        if(p->data == data)break;
        tmp = p;
        p = p->next;
    }

    if(p==NULL){
        printf("๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์ ธ์žˆ๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๊ณ  ์‹ถ์€ ๋ฐ์ดํ„ฐ(%d)๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.\n",data);
        return;
    }

    if( p == *head ){
        *head = p->next;
        p->next->prev = NULL;
        free(p);
        return;
    }

    if(p==*tail){
        (*tail)=tmp;
        tmp->next=NULL;
        free(p);
        return;
    }

    tmp->next = p->next;
    p->next->prev = tmp;
    free(p);
    printf("๋ฐ์ดํ„ฐ(%d)๋ฅผ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ญ์ œํ–ˆ์Šต๋‹ˆ๋‹ค. \n",data);
}

์ถœ๋ ฅ

//์˜ค๋ฆ„์ฐจ์ˆœ
int print_node(Node *head){
    Node *p = head;
    int i=0;

    while(p!=NULL){
        printf("[%d] : %d ",i,p->data);
        p=p->next;
        i++;
    }
    printf("\n\n");
    return i;
}
//๋‚ด๋ฆผ์ฐจ์ˆœ
int print_node2(Node *tail){
    Node *p = tail;
    int i=0;

    while(p!=NULL){
        printf("[%d] : %d ",i,p->data);
        p=p->prev;
        i++;
    }
    printf("\n\n");
    return i;
}

Last updated