단순연결리스트 [Singly linked list] 

 

 

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
 
struct Node
{
    int          data;
    struct Node* next;
};
 
void PrintNodes(struct Node* root)
{
    Node* temp = root;
    printf("--------------NODES--------------\n");
    int i = -1;
    while( temp != NULL){
        if(i == -1) printf("root");
        else
        {
            printf("pos");
            printf("%d", i);
        }
        printf(": ");
        printf("%d", (int)temp);
        printf(" / data: ");
        printf("%d", temp->data);
        printf(" / next: ");
        printf("%d", (int)temp->next);
        printf("\n");
        temp = temp->next;
        i++;
    }printf("---------------------------------\n");
}
 
Node* CreateNode(Node* newnew)
{
    newnew = new Node;
    newnew->data = 0;
    newnew->next = NULL;
    return newnew;
}
 
int GetNodeCount(Node* root)
{
    int count = 0;
    Node* temp = root;
    while( temp->next != NULL )
    {
        count++;
        temp = temp->next;
    }
    return count;
}
 
Node* GetNode(Node* root, int pos)
{
    int count = 0;
    Node* get = root;
    while( get->next != NULL)
    {
        get = get->next;
        if( count == pos ) break;
        count++;
    }
    return get;
}
 
int Insert(struct Node* root, int position)
{
    Node* NewNode = NULL;
    NewNode = CreateNode(NewNode);
    Node* prev = NULL;
    prev = GetNode(root, position-1);
 
    if(GetNodeCount(root) < position) return 0; //없는거 못 만든다.
    if( position == GetNodeCount(root) )
    {
        prev->next = NewNode;
        return 1;
    }
    else
    {
        Node* after = NULL;
        after = GetNode(root, position);
        if( position==0 ) root->next = NewNode;
        else    prev->next = NewNode;
        NewNode->next = after;
        return 1;
    }
    return 0;
}
 
int Delete(struct Node* root, int position)
{
    int a = GetNodeCount(root);
    if(GetNodeCount(root) <= position) return 0; //없는거 못 지운다.
    else
    {
        Node* deletedNode = NULL;
        deletedNode = GetNode(root, position);
        Node* afterDeletedNode = NULL;
        afterDeletedNode = GetNode(root, position+1);
        if(position == 0)
            root->next = afterDeletedNode; //첫번째꺼 지우면 root에 나머지 연결
        else
        {
            Node* prevDeletedNode = NULL;
            prevDeletedNode = GetNode(root, position-1);
            int a = GetNodeCount(root);
            if(position == GetNodeCount(root)-1)
                prevDeletedNode->next = NULL;
            else
                prevDeletedNode->next = afterDeletedNode;
        }
        delete deletedNode;
        return 1;
    }
}
 
int SetData(struct Node* root, int position, int data)
{
    if( root->next != NULL && GetNodeCount(root) > position )
    {
        GetNode(root,position)->data = data;
        return 1;
    }
    else return 0;
}
 
int GetData(struct Node* root, int position, int* data)
{
    if( root->next != NULL && GetNodeCount(root) > position )
    {
        *data = GetNode(root,position)->data;
        return 1;
    }
    else return 0;
}
 
void main()
{
    Node* root = NULL;
    root  = CreateNode(root);
    int data = 0;
 
    /* position==0 created */   printf("%d ", Insert(root,0));  printf(" inserted \n");
    /* position==0 set to 7 */  printf("%d ", SetData(root,0,7)); printf(" set \n");
    /* position==1 created */   printf("%d ", Insert(root,1));  printf(" inserted \n");
    /* position==0 added */     printf("%d ", Insert(root,0));  printf(" inserted \n");
    /* --> 원래 있던 노드 밀어냄 */
 
    /* position==3 created */   printf("%d ", Insert(root,3));  printf(" inserted \n");
    /* position==3 deleted */   printf("%d ", Delete(root,3));  printf(" inserted \n");
    /* position==5 created */   printf("%d ", Delete(root,5));  printf(" inserted \n");
    /* --> 앞에 빈 노드 있으므로 생성 못함 */
 
    /* position==3 created */   printf("%d ", Insert(root,3));  printf(" inserted \n");
    /* position==2 set to 5 */  printf("%d ", SetData(root,2,5)); printf(" set \n");
    /* position==3 set to 6 */  printf("%d ", SetData(root,3,6)); printf(" set \n");
    /* position==7 get  */      GetData(root, 7, &data);  printf("%d ", data);   printf(" got \n");
    /* --> 빈 노드이므로 데이터 얻지 못함 */
 
    /* position==2 get  */      GetData(root, 2, &data);  printf("%d ", data);   printf(" got \n");
 
    PrintNodes(root);
    printf("total nodes: "); printf("%d ", GetNodeCount(root)); printf("\n\n");
    delete root;
}
</stdio.h>
Posted by :