y0u_bat

C언어 - 링크드리스트 linklist(추가,수정,삭제,검색,출력) 예제 본문

프로그래밍/C언어

C언어 - 링크드리스트 linklist(추가,수정,삭제,검색,출력) 예제

유뱃 2015. 11. 16. 22:34




링크드리스트 Linklist(추가,수정,삭제,검색,출력)

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <stdlib.h>
 
typedef struct game{
 
    int no;    
    char id[20];
    char pw[20];
    struct game *next;
 
}Game;
 
Game *head = NULL,*tail = NULL;
 
int menu()
{
    int input;
 
    printf("1. user_add\n2. user_modify\n3. user_delete\n4. user_search\n5. user_print\n\ninput: ");
    scanf("%d",&input);
 
    return input;
}
 
void user_add()
{
    Game *new_user = (Game*)malloc(sizeof(Game));
 
    printf("user number : ");
    scanf("%d",&new_user->no);
 
    printf("user id : ");
    scanf("%s",new_user->id);
 
    printf("user pw : ");
    scanf("%s",new_user->pw);
 
    if(head == NULL)
        head = tail = new_user;
    else{        
        tail->next = new_user;
        tail = new_user;    
    }        
}
 
void user_modify()
{
    Game *user_modify = head;
 
    int input;
 
    printf("user modify id:");    
    scanf("%d",&input); 
           
    if(user_modify == NULL)    
    {        
        printf("not found data\n");        
        return;    
    }            
 
    while(user_modify != NULL)    
    {        
        if(user_modify->no == input)        
        {            
            printf("=====user_modify=====\nuser_id: ");  
            scanf("%s",user_modify->id); 
            printf("user_pw: ");            
            scanf("%s",user_modify->pw);            
            printf("=====================\n");        
        }                
        
        user_modify = user_modify->next;
    }
}
 
void user_delete()
{
    
    Game *delete_user = head;
    Game *prev_user = NULL;
    
    int input;
 
    printf("delete user id: ");
    scanf("%d",&input);
   
    while(delete_user != NULL)    
    {
        if(delete_user->no == input)        
        {            
            if(head == delete_user)            
            {                
                if(head->next==NULL)                    
                    head=tail=NULL;                
                else                    
                    head = head->next;  
                              
            }else if(tail == delete_user)            
            {                
                prev_user->next = NULL;                
                tail = prev_user;            
            }else
            {                
                prev_user->next = delete_user->next;                            
            }    
                    
            free(delete_user);            
            break;                                           
        }                
    
        prev_user = delete_user;        
        delete_user = delete_user->next;    
    } 
}
 
void user_search()
{    
    Game *user_search = head; 
   
    int input;        
 
    printf("find user id: ");    
    scanf("%d",&input);        
    
    if(user_search == NULL)    
    {        
        printf("not found data\n\n\n");        
        return;    
    }        
    
    while(user_search != NULL)    
    {        
        if(user_search->no == input)        
        {            
            printf("==================\nID: %s\nPW: %s\n==================\n",user_search->id,user_search->pw); 
        }                        
 
        user_search = user_search->next;                
    }
}
 
void user_print()
{    
    Game *user_print = head;        
    
    if(user_print == NULL)    
    {        
        printf("not found data\n\n\n");        
        return;    
    }        
 
    while(user_print != NULL)    
    {        
        printf("==================user_data==================\nno: %d / user_id: %s / user_pw: %s \n",user_print->no,user_print->id,user_print->pw);                        
        user_print = user_print->next;    
    }        
 
    printf("=============================================\n");
}
 
int main(int argc,char *argv[])
{    
    int select;            
 
    while(1)    
    {        
        printf("\n\n");        
 
        select = menu();            
 
        switch(select)        
        {            
            case 1:                
                user_add();                
                break;            
            case 2:                
                user_modify();                
                break;            
            case 3:                
                user_delete();                
                break;            
            case 4:                
                user_search();                
                break;            
            case 5:                
                user_print();                
                break;        
        }    
    }
 
    return 0;
}
 
cs


Comments