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

typedef struct Node
{
    int num;
    char data[80];
    struct Node *next;
} Node;

// 전역변수
Node head;


void add_node (int num, char *data)
{
    Node *here = &head;
    Node *temp;
    temp = malloc(sizeof (Node));
    temp->num = num; strcpy(temp->data, data); temp->next = NULL;

    while(here->next)
    {
        if (here->num <= temp->num)
        {
            if (!here->next)
            {
                here->next = temp;
                return;
            }
            else if (here->next->num > temp->num)
            {
                temp->next = here->next;
                here->next = temp;
                return;
            }   
        }
        here = here->next;
    }

    here->next = temp;
    return;
};

void delete_node (int index)
{
    Node *here = &head;
    Node *temp;
    while(here)
    {
        if (here->next->num == index)
        {
            temp = here->next;
            here->next = here->next->next;
            free(temp);
            return;
        }
        here = here->next;
    }
    return;
};

int display(void)
{
    int count = 1;
    Node *here = head.next;
    
    while(here)
    {
        printf ("%d : %d  \t%s\n", count, here->num, here->data);
        here = here->next;
        count++;
    }

    return count;
}

int main()
{
    int loop;
    int t_n;
    char t_d[80];
    head.next = NULL;
    head.num = -1;
    strcpy(head.data, "");

    for (loop = 0; loop < 5; loop++)
    {
        scanf("%d\n", &t_n);
        gets(t_d);
        add_node(t_n, t_d);
    }
    display();

    delete_node(3);
    delete_node(1);
    printf ("\n");

    display();

    return 0;
}

Posted by 지그프리드 지그프리드

댓글을 달아주세요:: 네티켓은 기본, 스팸은 사절


BLOG main image
일상, 프로그래밍, IT 그리고 직장생활, Dive, 여행 by 지그프리드

카테고리

Class List (402)
Studies (30)
Exercise & Quizz (10)
Term Project (0)
ECIM list (Help!) (10)
Issues & News (0)
Gossip about IT & Job (22)
Tools (2)
Think about the Justice (23)
Book Review (170)
조엘 온 소프트웨어(번역) (28)
Diary (87)
Vacations (9)
Clash of clans 클래시 오브.. (11)

글 보관함

달력

«   2019/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Total : 315,600
Today : 2 Yesterday : 22