기타

Pointer based Address list(C)

grze0 2026. 2. 18. 14:33

//조건(c언어, 취약점이 없도록 설계) //1. 동적 할당 함수 사용 //2. CRUD(create, read, update, delete) 기능이 포함되어야 함 //3. 사람의 정보는 구조체로 관리(저장해야 하는 정보 : 이름, 전화번호, 나이, 이메일 주소) //4. 단일 연결 리스트 사용 //5. 찾고자 하는 사람의 이름을 입력하면 빠르게 검색할 수 있도록 해싱 함수를 사용할 것 //⇒ 이후 stress testing 했을 때 정상적으로 동작해야 함

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct memberinfo {
	int index;
	char name[20];
	int age;
	char number[20];
	char email[30];

	struct memberinfo* next;
	
}info ;

void init(info *ptr2) {
	info* ptr = ptr2;
	unsigned int name_size = sizeof(ptr->name);
	unsigned int number_size = sizeof(ptr->number);
	unsigned int email_size = sizeof(ptr->email);

	ptr->next = NULL;
	ptr->index = 0;
	ptr->age = 0;
	strcpy_s(ptr->name, name_size, "");
	strcpy_s(ptr->number, number_size, "");
	strcpy_s(ptr->email, email_size ,"");
}
bool create(info *ptr2) {
	info* ptr = ptr2;
	unsigned int info_size = sizeof(info);
	while (ptr->next != NULL) {
		ptr = ptr->next;
	}
	ptr->next = (info*)malloc(info_size);

	if (ptr->next == NULL) {
		printf("메모리 에러");
		exit(1);
	}
	ptr = ptr->next;

	unsigned int name_size = sizeof(ptr->name);
	unsigned int number_size = sizeof(ptr->number);
	unsigned int email_size = sizeof(ptr->email);

	//잘못된 입력에 대한 검증 추가 필요함
	printf("name: ");
	scanf_s("%s", ptr->name,name_size);
	
	printf("age: ");
	scanf_s("%d", &ptr->age);
	
	printf("number: ");
	scanf_s("%s", ptr->number, number_size);

	printf("email: ");
	
	scanf_s("%s", ptr->email, email_size);
	ptr->next = NULL;

	return true;
}
void read(info* ptr2) {
	info* ptr = ptr2;
	ptr = ptr->next;
	while (ptr != NULL) {
		printf("name: %s\\n", ptr->name);
		printf("age: %d\\n", ptr->age);
		printf("number: %s\\n", ptr->number);
		printf("email: %s\\n", ptr->email);
		printf("------------------\\n");
		ptr = ptr->next;
	}
}
void update(info* ptr2) {
	info* ptr = ptr2;
	unsigned int name_size = sizeof(ptr->name);
	unsigned int number_size = sizeof(ptr->number);
	unsigned int email_size = sizeof(ptr->email);
	char update_name[20];
	
	printf("업데이트 할 사용자 : ");
	scanf_s("%s", update_name, name_size);
	update_name[name_size - 1] = '\\0';

	while (ptr->next != NULL) {
		ptr = ptr->next;
		if (strncmp(ptr->name, update_name, name_size)==0) {
			printf("name : ");
			scanf_s("%s", ptr->name, name_size);
			printf("age : ");
			scanf_s("%d", &ptr->age);
			printf("number : ");
			scanf_s("%s", ptr->number, number_size);
			printf("email : ");
			scanf_s("%s", ptr->email, email_size);
			printf("업데이트 성공\\n");
			return;
		}
	}

}
void delete(info *ptr2) {
	info* ptr = ptr2;
	unsigned int name_size = sizeof(ptr->name);
	info *temp = NULL;

	char del_name[20];
	

	printf("삭제할 이름 : ");
	scanf_s("%s", del_name, name_size);
	del_name[name_size-1] = '\\0';

	while (ptr->next != NULL) {
		temp = ptr;
		ptr = ptr->next;

		if (strncmp(ptr->name, del_name, name_size)==0) {
			temp->next = ptr->next;
			free(ptr);
			printf("%s 삭제 성공\\n",del_name);
			return;
		}
		
	}
	printf("찾는 이름이 없습니다\\n");
	
	
}
int main() {
	unsigned int info_size = sizeof(info);
	info* head = (info*)malloc(info_size);
	
	init(head);
	
	while (1) {
		int choice = 0;
		printf("1.Create || 2.Read || 3.Update || 4.Delete || other input exit\\n");
		scanf_s("%d", &choice);
		switch (choice)
		{
		case 1:
			printf("Create selected\\n");
			if (create(head)) {
				printf("주소 추가 성공\\n");
			}
			else {
				printf("에러\\n");
			}
			break;
		case 2:
			printf("Read selected\\n");
			read(head);
			break;
		case 3:
			printf("Update selected\\n");
			update(head);
			
			break;
		case 4:
			printf("Delete selected\\n");
			delete(head);
			break;
		default:
			printf("종료\\n");
			exit(1);
			break;

		}

	}
		return 0;
}

'기타' 카테고리의 다른 글

ARP-Soofing tool(python)  (0) 2026.02.18
코드업 C언어 기초 100제(41~50)  (0) 2021.04.04
코드업 C언어 기초100제(31~40)  (0) 2021.04.04
코드업 C언어 기초 100제(21~30)  (0) 2021.04.04
코드업 C언어 기초100제(11~20)  (0) 2021.04.04