[C Programming] 연결리스트

[linkedListFunc.h]
 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
#define TRUE 1
#define FALSE 0

typedef struct Student *StudentPointer;

struct Student
{
 char name[50];
 int sno; // 학번
 float gpa; //학점
 char phone[20];
 char province[30];


 StudentPointer next;
 StudentPointer prev;

};



typedef struct _LinkedList
{
 StudentPointer head;
 StudentPointer tail;
 StudentPointer cur;
 int num_of_Node;
} LinkedList;

typedef LinkedList List;

StudentPointer List_Init(List * ptr_list);
void IN(List * ptr_list); 
void PP(List * ptr_list);
void DE(List * ptr_list);
void PH(List * ptr_list);
void CT(List * ptr_list);
void PV(List * ptr_list);

int List_First(List * ptr_list);
int List_Next(List * ptr_list);
int List_Prev(List * ptr_list);

int List_Number(List * ptr_list);

void Sort_By_Name(List * ptr_list);
void Print_Node(List * ptr_list);



[linkedListFunc.c]
  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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedListFunc.h"
#pragma warning(disable:4996)


StudentPointer List_Init(List * ptr_list) { //초기화 : 연결리스트를 생성하고 파일을 읽어온다.

 ptr_list->head = NULL;
 ptr_list->num_of_Node = 0;

 StudentPointer newNode;
 
 FILE *fp;

 fp = fopen("mydata.txt", "r");
 if (fp == NULL) {
  perror("error : can't read the data file..\n");
  return FALSE;
 }
 else {
  
  while(!feof(fp)){
   //1. newNode 생성 - 메모리 할당,초기화
   newNode = (StudentPointer)malloc(sizeof(struct Student));
   
   fscanf(fp, "%s %d %f %s %s", newNode->name, &newNode->sno, &newNode->gpa, newNode->phone, newNode->province);
   
   
   newNode->next = ptr_list->head;
   
   if (ptr_list->head != NULL)
    ptr_list->head->prev = newNode;
  
   newNode->prev = NULL;
   ptr_list->head = newNode;
   

   (ptr_list->num_of_Node)++;
   
  }
  ptr_list->cur = ptr_list->head;


  
 
  return TRUE;
 }

 
}

void IN(List * ptr_list) { //새 학생의 삽입
 StudentPointer newNode = (StudentPointer)malloc(sizeof(struct Student));
 
 //printf("이름을 입력하세요.\n");
 scanf("%s",newNode->name);

 //printf("학번을 입력하세요.\n");
 scanf("%d", &newNode->sno);

 //printf("학점을 입력하세요.\n");
 scanf("%f", &newNode->gpa);
 
 //printf("번호를 입력하세요.\n");
 scanf("%s", newNode->phone);
 
 //printf("주소를 입력하세요.\n");
 scanf("%s",newNode->province);
 
 newNode->next = ptr_list->head;
 if (ptr_list->head != NULL)
  ptr_list->head->prev = newNode;

 newNode->prev = NULL;
 ptr_list->head = newNode;
 Sort_By_Name(ptr_list);

 (ptr_list->num_of_Node)++;
}


void PP(List * ptr_list) { //일부의 출력
 //이름 두 개 입력
 char name1[50];
 char name2[50];
 int flag = 0;

 //printf("이름1 입력 : ");
 scanf("%s", name1);
 //printf("이름2 입력 : ");
 scanf("%s", name2);


 if (List_First(ptr_list))
 {
  while (List_Next(ptr_list)) {
   if (strcmp(ptr_list->cur->name, name1) == 0 || strcmp(ptr_list->cur->name, name2) == 0) {
    flag = 1;
    break;
   }
  }
 }
 if (!flag)
  printf("Can't find the name..\n");

 

 if (strcmp(name1, name2) > 0) {
  if (List_First(ptr_list)) {
   while (strcmp(ptr_list->cur->name, name2) != 0) {

    List_Next(ptr_list);

   }
  }

  while (strcmp(ptr_list->cur->name, name1) != 0) {
   printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
   ptr_list->cur = ptr_list->cur->next;
  }
  printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
 }


 else if (strcmp(name1, name2) < 0) {
  if (List_First(ptr_list)) {
   while (strcmp(ptr_list->cur->name, name1) != 0) {

    List_Next(ptr_list);

   }
  }

  while (strcmp(ptr_list->cur->name, name2) != 0) {
   printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
   ptr_list->cur = ptr_list->cur->next;
  }
  printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
 }
 else
  printf("동명이인입니다.\n");

}

void DE(List * ptr_list) { //한 학생의 삭제
 char name[50];
 //printf("삭제할 이름 입력: ");
 scanf("%s", name);
 if (List_First(ptr_list)) {
  while (strcmp(ptr_list->cur->name, name) != 0) {
   List_Next(ptr_list);
   if (ptr_list->cur == NULL)
    printf("없는 이름입니다.\n");
  }
 }

 printf("삭제할 데이터는 %s입니다.\n\n", ptr_list->cur->name);
 StudentPointer rm_pos = ptr_list->cur;

 //맨 첫 데이터 삭제할경우
 if (ptr_list->cur->prev == NULL) {
  ptr_list->cur->next->prev = NULL;
  ptr_list->cur = ptr_list->cur->next;
  ptr_list->head = ptr_list->cur;
 }
 //맨 뒤 데이터 삭제할경우
 else if (ptr_list->cur->next == NULL) {
  ptr_list->cur->prev->next = NULL;
  ptr_list->cur = ptr_list->cur->prev;
 }
 
 
 else {
  ptr_list->cur->prev->next = ptr_list->cur->next;
  ptr_list->cur->next->prev = ptr_list->cur->prev;
  ptr_list->cur = ptr_list->cur->prev;
 }

 free(rm_pos);
 (ptr_list->num_of_Node)--;

}

void PH(List * ptr_list) { // 전화번호에 의한 출력
 char TwoDigit_phone[20];
 int flag = 0;
 
 //printf("전화번호의 연속되는 두 숫자 입력: ");
 scanf("%s", TwoDigit_phone);
 
 if (List_First(ptr_list))
 {
  if (strstr(ptr_list->cur->phone, TwoDigit_phone)) {
   printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
   flag = 1;
  }

  while (List_Next(ptr_list)){
   if (strstr(ptr_list->cur->phone, TwoDigit_phone)) {
    printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
    flag = 1;
   }
  } 
  if (!flag) 
   printf("Can't find the digits..\n");
 }

}

void CT(List * ptr_list) { // 학점 이상
 float gpa;
 int flag = 0;

 //printf("학점 입력: ");
 scanf("%f", &gpa);

 if (List_First(ptr_list)) {

  while (List_Next(ptr_list)) {
   if (ptr_list->cur->gpa >= gpa) {
    printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
    flag = 1;
   }
  }
  if (!flag)
   printf("No one had the grade..\n");
 }

}

void PV(List * ptr_list) { // 카운트 - 지역명을 입력받아 출력
 char local[20];
 int flag = 0;

 //printf("지역명을 입력하세요: ");
 scanf("%s", local);

 if (List_First(ptr_list))
 {
  if (strcmp(ptr_list->cur->province, local) == 0) {
   printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
   flag = 1;
  }

  while (List_Next(ptr_list)) {
   if (strcmp(ptr_list->cur->province, local) == 0) {
    printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
    flag = 1;
   }
  }
  if (!flag)
   printf("Can't find the city..\n");
 }
}
int List_First(List * ptr_list) { // 첫번째 데이터 탐색
 if (ptr_list->head == NULL)
  return FALSE;
 
 ptr_list->cur = ptr_list->head;

 
 return TRUE;
}
int List_Next(List * ptr_list) { // 다음 데이터 탐색
 if (ptr_list->cur->next == NULL)
  return FALSE;
 
 ptr_list->cur = ptr_list->cur->next;
 
 return TRUE;
}
int List_Prev(List * ptr_list) { // 이전 데이터 탐색
 if (ptr_list->cur->prev == NULL)
  return FALSE;

 ptr_list->cur = ptr_list->cur->prev;
 
 return TRUE;
}

int List_Number(List * ptr_list) { // 리스트의 노드 개수
 return ptr_list->num_of_Node;
}

void Sort_By_Name(List * ptr_list) { // 정렬 함수
 /*
 strcmp의 리턴값
 음수 - string1 < string2
 0    - string1 = string2
 양수 - string1 > string2
 */
 
 ptr_list->cur = ptr_list->head;


 while (ptr_list->cur->next != NULL) {
  if (strcmp(ptr_list->cur->name, ptr_list->cur->next->name) > 0) {
   //비교한 노드를 통째로 변경
   
   if (ptr_list->cur->next->next != NULL && ptr_list->cur->prev != NULL) { // 중간 노드를 변경할 때

    ptr_list->cur->next = ptr_list->cur->next->next;
    ptr_list->cur->next->prev->prev = ptr_list->cur->prev; 
    ptr_list->cur->next->prev->next = ptr_list->cur;
    ptr_list->cur->prev->next = ptr_list->cur->next->prev;
    ptr_list->cur->prev = ptr_list->cur->prev->next;
    ptr_list->cur->next->prev = ptr_list->cur;
   
   }
   
   else if(ptr_list->cur->next->next == NULL) //예외1 . 리스트에서 마지막 노드를 변경할 때
   {
    ptr_list->cur->next->next = ptr_list->cur;
    ptr_list->cur->prev->next = ptr_list->cur->next;
    ptr_list->cur->next->prev = ptr_list->cur->prev;
    ptr_list->cur->prev = ptr_list->cur->next;
    ptr_list->cur->next= NULL;
    break;
   }

   else if (ptr_list->cur->prev == NULL) //예외2 . 리스트에서 첫 노드를 변경할 때
   {

    ptr_list->cur->next = ptr_list->cur->next->next;
    ptr_list->cur->next->prev->prev = NULL;
    ptr_list->cur->prev = ptr_list->cur->next->prev;

    ptr_list->cur->next->prev->next = ptr_list->cur;
    
    ptr_list->cur->next->prev = ptr_list->cur;
    ptr_list->cur = ptr_list->cur->prev;
    ptr_list->head = ptr_list->cur;
   
   
   }
   
  

  }
  else if (strcmp(ptr_list->cur->name, ptr_list->cur->next->name) <= 0) {
   
   if (ptr_list->cur->next != NULL)
    ptr_list->cur = ptr_list->cur->next;
  

   }
   
  
  else {
    perror("error : from Sort_by_name");
    printf("\n");

   } 
  } 


 

}


void Print_Node(List * ptr_list) {


 ptr_list->cur = ptr_list->head;
 printf("Name==============SNO===========GPA================Phone========Region\n");
 printf("\n");
 while (ptr_list->cur->next != NULL) {
  printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa, ptr_list->cur->phone, ptr_list->cur->province);
  ptr_list->cur = ptr_list->cur->next;
 }
 printf("%-15s %-15d %-15.2f %-15s %-15s\n", ptr_list->cur->name, ptr_list->cur->sno, ptr_list->cur->gpa,ptr_list->cur->phone,ptr_list->cur->province);
 
}


[main.c]
 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
#include <stdio.h>
#include <string.h>
#include "linkedListFunc.h"
#pragma warning(disable:4996)

int main() {

 List list;
 int count = 1;
 char command[20] = "";
 int numOfNode;

 List_Init(&list);
 
 numOfNode = List_Number(&list); //리스트에 저장된 노드 개수
 while (count < numOfNode-1) {
  Sort_By_Name(&list);
  count++;
 }
 
 Print_Node(&list);
 
 while (strcmp(command,"EX") != 0) {

  printf("Type command> ");
  scanf("%s", command);

  if (strcmp(command, "IN") == 0) {
   IN(&list);
   Print_Node(&list);
  }
  else if (strcmp(command, "PP") == 0)
   PP(&list);
  else if (strcmp(command, "DE") == 0) {
   DE(&list);
   Print_Node(&list);
  }
  else if (strcmp(command, "CT") == 0)
   CT(&list);
  else if (strcmp(command, "PH") == 0)
   PH(&list);
  else if (strcmp(command, "PV") == 0)
   PV(&list);
  else if (strcmp(command, "EX") == 0)
   break;
  else
   printf("잘못된 명령입니다.\n");
  
 }
 
 printf("프로그램을 종료합니다!\n");
 
 return 0;
}

댓글