아주 간단한 링크드 리스트인데요. 알고리즘만 한번 봐주세요.

winicon의 이미지

아주 간단한 링크드 리스트인데요. 알고리즘만 한번 봐주세요.
그럼 답변 부탁 드립니다 ^^


///////////////////////////// main.c //////////////////////////////////////////////////////////////////

 

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "linkedlist.h" 

int main(int argc, char* argv[])
{

    struct node *p;
    struct node *head = NULL; 

    p = (struct node *)malloc(sizeof(struct node));
    memset((struct node *)p, 0x00,sizeof(struct node));

    head = p; 

    travling(head); 

    add(head, 1);
    add(head, 2);
    add(head, 3); 

    travling(head);

    del(head, 3); 

    travling(head);   

    all_del(head);

    return 1;

} 

///////////////////////////// linkedlist.h ///////////////////////////////////////////////////////////////

 

#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H 

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

struct node {
    int key;
    struct node *next;
};

void travling(struct node *p);
void add(struct node *p, int key);
void del(struct node *p, int key);
void all_del(struct node *p);

 
////////////////////////////// linkedlist.c  //////////////////////////////////////////////////////////

 #include "linkedlist.h" 

void travling(struct node *p)
{

    while(p != NULL) 
    {
        printf("%d ", p->key);
        p = p->next;
    }

    printf("\n");
}

void add(struct node *p, int key)
{   
    if(p == NULL) 
    {
        struct node *q;
        q = (struct node *)malloc(sizeof(struct node)); 

        q->key = key;
        q->next = NULL;
        p = q;
    } 
    else 
    {
        struct node *q;

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

        q = (struct node *)malloc(sizeof(struct node)); 

        q->key = key;
        q->next = NULL;
        p->next = q;
    }

}

 
void del(struct node *p, int key)
{

    if(p != NULL) 
    {
        struct node *q; 

        while(p->next != NULL && p->next->key != key) 
        {
            p = p->next;
        }

        if(p->next == NULL) return;

        q = p->next;
        p->next = q->next;
        free(q);
    }

}

void all_del(struct node *p)
{
    if(p != NULL) 
    {
        struct node *t;     
        while(p->next != NULL) 
        {
            printf("delete = %d\n", p->key);

            t = p;      
            p = p->next;            
 
            free(t);
            t = NULL;

        }
        if(p != NULL)
       {
             free(p);
             p=NULL;
       }
    }

}

익명 사용자의 이미지

자세히 본건 아니지만 대충 말해보자면

void add(struct node *p, int key); 
void del(struct node *p, int key);


void add(struct node **p, int key); 
void del(struct node **p, int key);

이렇게 바꾸고 코드도 그에 맞게 수정해야 할 겁니다.
안 그러면 리스트에 노드를 추가하거나 삭제해도 아무 변화가 없을 겁니다.

제 생각으로는 linked_list 를 별도의 struct 로 만든뒤
관련된 함수를 linked_list 의 포인터를 받도록 수정하는 것이 더 나아 보입니다.

xster의 이미지

리스트 루트 노드의 사용도 좀 걸리는 군요.
루트 노드를 사용하지 않는 경우와 사용하는 경우가 섞여 있어서 루트 노드의 키는 삭제를 못하게 되네요.
add에서 NULL노드를 넘길 때 리턴하면서 메모리 잃어버리는 것도 문제가 됩니다. 함수의 매개변수는 호출할 때 사용한 변수와는 별도의 것이라는 생각을 가지고 살펴보세요.