티스토리 뷰

연결리스트(LinkedList)



-예제를 통한 연결리스트 연습


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <malloc.h>
 
typedef int element;
typedef struct node {
    element data;
    node* link;
}node;
 
node *head, *temp;
int N, M, x, y;
int password[1000], arr[1000];
 
void initList()
{
    head = (node *)malloc(sizeof(node));
    head->link = NULL;
}
 
void insertNode(int data)
{
    node *newnode;
    newnode = (node *)malloc(sizeof(node));
    newnode->data = data;
 
    temp = head;
 
    if (head->link == NULL)
    {
        newnode->link = NULL;
        head->link = newnode;
        return;
    }
 
    while (temp->link != NULL)
        temp = temp->link;
 
    newnode->link = NULL;
    temp->link = newnode;
}
 
void insertpos(int pos, int data)
{
    node *newnode;
    newnode = (node *)malloc(sizeof(node));
    newnode->data = data;
 
    temp = head;
 
    for (int i = 0; i < pos; i++)
        temp = temp->link;
 
    newnode->link = temp->link;
    temp->link = newnode;
}
 
void printList()
{
    temp = head;
 
    for (int i = 0; i < 10; i++)
    {
        printf("%d ", (temp->link)->data);
        temp = temp->link;
    }
}
 
void clearList()
{
    free(head);
    free(temp);
}
 
int main()
{
    for (int tc = 1; tc <= 10; tc++)
    {
        char ins = 0;
 
        initList();
 
        scanf("%d\n"&N);
        for (int i = 0; i < N; i++)
        {
            scanf("%d "&password[i]);
            insertNode(password[i]);
        }
        scanf("%d\n"&M); //명령어 수
        for (int i = 0; i < M; i++)
        {
            if (i == 0)
                scanf("%c %d %d"&ins, &x, &y);
            else
                scanf(" %c %d %d"&ins, &x, &y);
        
            for (int j = 0; j < y; j++)
            {
                scanf(" %d"&arr[j]);
                insertpos(x + j, arr[j]);
            }
        }
 
        printf("#%d ", tc);
        printList();
        printf("\n");
 
        clearList();
    }
    return 0;
}
 
cs


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
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
글 보관함