프로그래밍/C++언어
[C++언어]더미노드가 있는 단순연결리스트로 주소록 만들기
천마서생
2016. 4. 15. 04:12
1. 선언부와 구현부 확실히 나눌 것.
2. 중복 삽입 불가(node의 데이타와 추가 시킬 node의 데이타가 완전 똑같을 경우)
3. 삭제를 할때 이름, 연락처 , 주소를 입력하고 이것들과 전부 일치하는 노드를 제거하도록 짤 것,
4. 추가로 연락처, 주소, 이름은 길이제한이 없고, 영어, 한글, 숫자 제한이 없음
5. 첨부 파일과 동일하게 실행되어야 할 것.
List.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #pragma once #include"Node.h" class LIST { private: Node* head; Node* before; std::string Name; std::string num; std::string Address; public: LIST(); void Insert(); void Delete(); void print(); }; | cs |
List.cpp
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 | #include"List.h" LIST::LIST() { head = new Node(); head->next = nullptr; before = head; } void LIST::Insert() { std::cout << "추가할 정보를 기입해주세요." << std::endl; std::cout << "회원의 이름을 기입하세요: "; std::cin >> Name; std::cout << "연락처를 기입하세요: "; std::cin >> num; std::cout << "주소를 기입하세요: "; std::cin >> Address; for (Node *ptr = head; ptr->next != nullptr; ptr = ptr->next) { if ((ptr->Get_name() == Name) && (ptr->Get_number() == num) && (ptr->Get_address() == Address)) { std::cout << "중복되는 정보가 있습니다." << std::endl; return; } } Node* newNode = new Node(); newNode->Set_name(Name); newNode->Set_number(num); newNode->Set_address(Address); newNode->next = head->next; head->next = newNode; } void LIST::Delete() { std::cout << "삭제할 정보를 정확히 기입해주세요." << std::endl; std::cout << "회원의 이름을 기입하세요: "; std::cin >> Name; std::cout << "연락처를 기입하세요: "; std::cin >> num; std::cout << "주소를 기입하세요: "; std::cin >> Address; for (Node *ptr = head; ptr != nullptr; ptr = ptr->next) { Node *remove = ptr; if ((ptr->Get_name() == Name) && (ptr->Get_number() == num) && (ptr->Get_address() == Address)) { before->next = ptr->next; ptr = before; delete remove; return; } before = ptr; } std::cout << "그러한 정보가 없습니다." << std::endl; } void LIST::print() { std::cout << "--------------------------------------------" << std::endl; std::cout << "회원의 이름" << std::endl; std::cout << "연락처" << std::endl; std::cout << "주소지" << std::endl; std::cout << "--------------------------------------------" << std::endl; for (Node *ptr = head->next; ptr != nullptr; ptr = ptr->next) { std::cout << ptr->Get_name() << std::endl; std::cout << ptr->Get_number() << std::endl; std::cout << ptr->Get_address() << std::endl; } std::cout << "--------------------------------------------" << std::endl; } | cs |
Node.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #pragma once #include<iostream> #include<string> class Node { private: std::string name; std::string number; std::string address; public: Node* next; std::string Get_name(); std::string Get_number(); std::string Get_address(); void Set_name(std::string _name); void Set_number(std::string _number); void Set_address(std::string _address); }; | cs |
Node.cpp
1 2 3 4 5 6 7 | #include"Node.h" std::string Node::Get_name() { return name; } std::string Node::Get_number() { return number; } std::string Node::Get_address() { return address; } void Node::Set_name(std::string _name) { name = _name; } void Node::Set_number(std::string _number) { number = _number; } void Node::Set_address(std::string _address) { address = _address; } | cs |
Phonebook.h
1 2 3 4 5 6 7 | #pragma once #include<iostream> #include<string> #include"Node.h" #include"List.h" enum { MAKE = 1, DELETE, PRINT, EXIT }; | cs |
Phonebook.cpp
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 | #include"Phonebook.h" void ShowMenu() { std::cout << "무엇을 하겠습니까?" << std::endl; std::cout << "1: 추가" << std::endl; std::cout << "2: 삭제" << std::endl; std::cout << "3: 출력" << std::endl; std::cout << "4: 종료" << std::endl; } void main() { int choice; LIST *phonebook = new LIST(); while (1) { ShowMenu(); std::cin >> choice; switch (choice) { case MAKE: phonebook->Insert(); break; case DELETE: phonebook->Delete(); break; case PRINT: phonebook->print(); break; case EXIT: return; default: std::cout << "다시 입력하시오." <<std::endl; break; } } } | cs |
더미노드란 head의 주소값이 NULL값이 안되게 붙여주는 데이터값이 없는 노드를 말한다.