// Simple Linked List
#include<stdio.h>
#include<stdlib.h>

typedef struct _node
{
    int data;
    struct _node* next;
}node;
node *head, *tail;

 void node_print(void);
void init_node(void);
void insert_node(int);
node* find_node(int);
void insert_after(int, node*);
void del_next_node(node*);
void insert_before(int, node*);
void del_node(int);
void del_all(void);

 void main()
{
    init_node();
    insert_node(1);
    insert_node(10);
    insert_node(5);
    insert_node(8);
    insert_node(3);
    insert_node(8);
    insert_node(7);

    puts("Inital Linked list is");
    node_print();

    printf("Finding 4 is %s\n", find_node(4)!=tail ? "successful" : "unsuccessful");
    printf("Finding 5 is %s\n", find_node(5)!=tail ? "successful" : "unsuccessful");
   
    puts("Inserting 9 after 5");
    insert_after(9, find_node(5));
    node_print();

    puts("Deleting next last node");
    del_next_node(find_node(10));
    node_print();

    puts("Deleting next 3");
    del_next_node(find_node(3));
    node_print();

    puts("Inserting 2 before 3");
    insert_before(2, find_node(3));
    node_print();

    puts("Deleting node 2");
    del_node(2);
    node_print();

    puts("Deleting node 1");
    del_node(1);
    node_print();

    puts("Deleting all node");
    del_all();
    node_print();

}

 //노드전체 삭제
void del_all(void)
{
    node *a, *temp;
    a = head->next;
    while(a != tail)
    {
        temp = a;
        a = a->next;
        free(temp);
    }
    head->next=tail;
}

 //선택한 노드 삭제
void del_node(int x)
{
    node *a, *b;
    a = head;
    b = head->next;
    while(b->data != x && b != tail)
    {
        a = a->next;
        b = b->next;
    }
    if(b != tail)
    {
        a->next = b->next;
        free(b);
    }
}
 

//찾는 숫자 이전에 노드 삽입
void insert_before(int x, node* y)
{
    node *a, *b;
    node* insert = (node*)malloc(sizeof(node));
    a = head;
    b = a->next;
    while(b->data != y->data && b != tail)
    {
        a = a->next;
        b = b->next;
    }
    if(b != tail)
    {
        insert->data = x;
        insert->next = b;
        a->next = insert;
    }
}
   
//찾는 숫자 다음에 노드 삭제
void del_next_node(node* x)
{
    node* temp;
    if(x != tail)
    {
        temp = x->next;
        x->next = x->next->next;
        free(temp);
    }
}

 //찾는 숫자다음에 노드 삽입
void insert_after(int x, node* y)
{
    node* insert = (node*)malloc(sizeof(node));
    insert->data = x;
    insert->next = y->next;
    y->next = insert;
}

 //노드 찾기
node* find_node(int x)
{
    node* a;
    a = head->next;
    while(a->data != x && a!=tail)
        a = a->next;
    return a;
}

 //노드 출력하기
void node_print(void)
{
    node* a;
    a = head->next;
    while(a != tail)
    {
        printf("%-7d",a->data);
        a = a->next;
    }
    puts("");
}

 //head, tail 노드 초기화
void init_node(void)
{
    head = (node*)malloc(sizeof(node));
    tail = (node*)malloc(sizeof(node));
    head->next = tail;
    tail->next = tail;
}

 //노드 추가
void insert_node(int x)
{
    node *a, *b;
    node* insert = (node*)malloc(sizeof(node));
    a = head;
    b = a->next;
    while(b->data <= x && b != tail)
    {
        a = a->next;
        b = b->next;
    }  
    insert->data = x;
    a->next = insert;
    insert->next = b;
}

+ Recent posts