반응형
참고자료
2020/04/28 - [자료구조] - C언어 자료구조(심화 연결 리스트 Linked_list 파일 읽어서 정렬하기)
2020/04/29 - [자료구조] - C언어 자료구조 (심화 연결리스트 Linked_list 데이터 삽입)
2020/04/30 - [자료구조] - C언어 자료구조 (심화 연결리스트 Linked_list 데이터 검색, 삭제)
개요
위의 3개를 통해서 만든 연결리스트의 동작을 알아보도록 하겠습니다.
코드
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <Windows.h>
// Declaring a node and it's link
typedef struct _node {
char name[50];
int sno;
float gpa;
char phone[20];
char province[30];
struct node* link;
}node;
//Functions for insert, printf, etc.
node* insert_node(node* head, char* name, int sno, float gpa, char* phone, char* province);
void print_node(node* head, char* fir_name, char* last_name);
void phone_node(node* head, char* phone);
void gpa_node(node* head, float gpa);
void region_node(node* head, char* region);
void search(node* head, char* name);
node* search_ins_position(node* head, char* d_name);
void delete_node(node* head);
int main() {
node* head, * curnode, * prev;
char d_name[50], d_phone[20], d_province[30]; //temporary storage
int d_sno, count; //temporary storage
float d_gpa; //temporary storage
int i, in_sno = 0; //input student number
float in_gpa = 0; //input gpa
char input[200], * token, data[6][50], in_name[2][50], in_phone[20], in_province[30]; //input name, phone, province
//Open 'mydata.txt' from the project directory
FILE* fp;
fp = fopen("mydata.txt", "r");
head = NULL;
prev = NULL;
while (1) {
//This procedure makes rotation not to work infinitely.
count = fscanf(fp, "%s", d_name);
if (count != 1)
break;
fscanf(fp, "%d %f %s %s", &d_sno, &d_gpa, &d_phone, &d_province);
//Copy data from temporary storage to current node
curnode = (node*)malloc(sizeof(node));
strcpy(curnode->name, d_name);
curnode->sno = d_sno;
curnode->gpa = d_gpa;
strcpy(curnode->phone, d_phone);
strcpy(curnode->province, d_province);
if (!head) {
head = curnode;
curnode->link = NULL;
}
else {
prev = search_ins_position(head, d_name);
if (!prev) {
curnode->link = head;
head = curnode;
}
else {
curnode->link = prev->link;
prev->link = curnode;
}
}
}
printf("추가 (IS) 입력예) IS 정성현 201825 4.3 010-1234-5678 서울\n");
printf("한 학생 이름 으로 검색 (SE) 입력예) SE 고길동 \n");
printf("이름으로 검색 (RG) 입력예) RG 고길동 홍길동\n");
printf("삭제 (DL) 입력예) DL\n");
printf("전화번호로 검색 (SP) 입력예) SP 4890\n");
printf("지역으로 검색 (LO) 입력예) LO 서울\n");
printf("종료 (EX) 입력예) EX\n");
Sleep(4000);
while (1) {
system("cls");
i = 0;
curnode = head; //Reading file is finished.
while (curnode)
{
printf("%s %d %.1f %s %s\n", curnode->name, curnode->sno, curnode->gpa, curnode->phone, curnode->province);
curnode = curnode->link;
} //Do it while curnode is not NULL
printf("Type command> ");
gets(input); //get string from user's keyboard
//cut string by each 'space' and save as separated data
token = strtok(input, " ");
do {
strcpy(data[i], token);
i++;
} while (token = strtok(NULL, " "));
//execute each command and change data type of each data
if (!strcmp(data[0], "IS")) {
strcpy(in_name[0], data[1]);
in_sno = atoi(data[2]);
in_gpa = atof(data[3]);
strcpy(in_phone, data[4]);
strcpy(in_province, data[5]);
head = insert_node(head, in_name[0], in_sno, in_gpa, in_phone, in_province);
}
else if (!strcmp(data[0], "SE")) {
strcpy(in_name[0], data[1]);
search(head, in_name[0]);
}
else if (!strcmp(data[0], "RG")) {
strcpy(in_name[0], data[1]);
strcpy(in_name[1], data[2]);
print_node(head, in_name[0], in_name[1]);
}
else if (!strcmp(data[0], "DL")) {
delete_node(head);
}
else if (!strcmp(data[0], "SP")) {
strcpy(in_phone, data[1]);
phone_node(head, in_phone);
}
else if (!strcmp(data[0], "LO")) {
strcpy(in_province, data[1]);
region_node(head, in_province);
}
else if (!strcmp(data[0], "EX")) {
break;
}
else {
printf("INVALID INPUT\n");
Sleep(2000);
rewind(stdin);
}
}
printf("프로그램을 종료합니다!");
Sleep(1500);
return 0;
}
node* insert_node(node* head, char* name, int sno, float gpa, char* phone, char* province) {
node* front, * new, * temp;
new = malloc(sizeof(node));
front = head;
strcpy(new->name, name);
new->sno = sno;
new->gpa = gpa;
strcpy(new->phone, phone);
strcpy(new->province, province);
temp = new;
while (front != NULL) {
if (strcmp(name, front->name) < 0)
break;
temp = front;
front = front->link;
}
if (temp == new) {
new->link = front;
front = new;
}
else {
new->link = temp->link;
temp->link = new;
front = head;
}
return front;
}
void print_node(node* head, char* fir_name, char* last_name) {
int i = 0;
node* front, * back, * thead;
char temp;
temp = NULL;
thead = head;
front = head;
back = head;
system("cls");
//exchange two string if rear name must be go front
if (strcmp(fir_name, last_name) > 0) {
strcpy(temp, fir_name);
strcpy(fir_name, last_name);
strcpy(last_name, fir_name);
}
while (front->link != NULL) {
if (strcmp(fir_name, front->name) == 0)
break;
front = front->link;
}
while (back->link != NULL) {
if (strcmp(last_name, back->name) == 0)
break;
back = back->link;
}
if ((front == NULL) || (back == NULL)) {
printf("표시할 데이터가 없습니다\n");
Sleep(1000);
return;
}
while (thead) {
if ((strcmp(thead->name, front->name) >= 0) && (strcmp(thead->name, back->name) <= 0)) {
printf("%s %d %.1f %s %s\n", thead->name, thead->sno, thead->gpa, thead->phone, thead->province);
i++;
}
thead = thead->link;
} //Do it while thead is between front->name and back->name
printf("총 %d명", i);
Sleep(3000);
return;
}
void search(node* head, char* name) {
system("cls");
while (head)
if (strcmp(head->name, name) == 0) {
printf("%s %d %.1f %s %s\n", head->name, head->sno, head->gpa, head->phone, head->province);
Sleep(3000);
break;
}
else {
head = head->link;
}
}
void delete_node(node* head) {
system("cls");
node* prev;
node* curr;
if (head == NULL) return; // 공백 리스트인 경우
if (head->link == NULL) { // 리스트 노드가 한개인 경우
printf("%s %d %.1f %s %s\n", head->name, head->sno, head->gpa, head->phone, head->province);
Sleep(3000);
free(head);
head = NULL;
return;
}
else {
prev = head;
curr = head->link;
while (curr->link != NULL) {
prev = curr;
curr = curr->link;
}
printf("%s %d %.1f %s %s\n", curr->name, curr->sno, curr->gpa, curr->phone, curr->province);
Sleep(3000);
free(curr);
prev->link = NULL;
}
}
void phone_node(node* head, char* phone) {
system("cls");
int i = 0;
node* front;
front = head;
while (front != NULL) {
if (strstr(front->phone, phone) != NULL) {
printf("%s %d %.1f %s %s\n", front->name, front->sno, front->gpa, front->phone, front->province);
i++;
}
front = front->link;
}
if (i == 0)
printf("데이터가 없습니다\n");
printf("총 %d 명", i);
Sleep(3000);
return;
}
void region_node(node* head, char* region) {
system("cls");
int i = 0;
node* front, * temp;
front = head;
while (front != NULL) {
if (strcmp(front->province, region) == 0) {
printf("%s %d %.1f %s %s\n", front->name, front->sno, front->gpa, front->phone, front->province);
i++;
}
front = front->link;
}
if (i == 0)
printf("데이터가 없습니다\n");
printf("총 %d 명", i);
Sleep(3000);
return;
}
node* search_ins_position(node* head, char* d_name) {
node* cmp, * before;
int re = 0;
cmp = (node*)malloc(sizeof(node));
before = NULL;
cmp = head;
while (1) {
re = strcmp(d_name, cmp->name);
if (re > 0) {
before = cmp;
cmp = cmp->link;
if (cmp == NULL)
return before;
}
else if (re <= 0) {
return before;
}
}
}
결과
반응형
'알고리즘 & 자료구조 > 자료구조' 카테고리의 다른 글
자료구조 BST (Binary search tree) 이원 탐색 트리 (0) | 2020.05.16 |
---|---|
C언어 자료구조 (심화 연결리스트 Linked_list 데이터 검색, 삭제) (0) | 2020.04.30 |
C언어 자료구조 (심화 연결리스트 Linked_list 데이터 삽입) (0) | 2020.04.29 |
C언어 자료구조(심화 연결리스트 Linked_list 파일읽어서 정렬하기) (0) | 2020.04.28 |
자료구조 C언어 (Recursion 함수 와 동적할당) (0) | 2020.04.15 |