[IT]/Data Structure
[자료구조] C++로 정렬되지 않은 배열리스트 만들기
working_hard
2018. 2. 20. 16:35
728x90
안녕하세요! 오늘은 자료구조에 대해서 알아보는 시간을 가지겠습니다. 정렬되지 않는 배열리스트(unsorted arraylist)를 개발하는 시간을 가졌습니다. 오늘 개발한 내용에 대해 리뷰를 하면서 공부한 내용을 다시 다지는 것이니 좋은 자료가 된다면 좋겠습니다.
1 2 3 4 5 6 7 8 9 10 11 12 | #include<iostream> #include"Application.h" using namespace std; int main() { Application app; app.Run(); //과제 1의 시작부분.. 대망의 시작이다. return 0; } | cs |
main.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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include "Application.h" Application::Application() { } Application::~Application() { } void Application::Run() { while (1) { m_Command = GetCommand(); switch (m_Command) { case 1: // read a record and add to list. AddItem(); break; case 2: // display all the records in list on screen. DisplayAllItem(); break; case 3: // make empty list. EmptyList(); break; case 4: // make empty list. SearchItem(); break; case 5: // make empty list. //this->Delete1(); break; case 6: // make empty list. //this->Replace1(); break; case 7: // load list data from a file. //ReadDataFromFile(); break; case 8: // save list data into a file. //WriteDataToFile(); break; case 0: return; default: cout << "\tIllegal selection...\n"; break; } } } int Application::GetCommand() { int command; cout << endl << endl; cout << "1. Add Item" << endl; cout << "2. Display All Items" << endl; cout << "3. Make Empty" << endl; cout << "4. Search Item" << endl; cout << "5. Delete Item" << endl; cout << "6. Replace Item" << endl; cout << "7. Update Item" << endl; cout << "8. Put To File" << endl; cout << "0. Quit" << endl; cout << endl << "choose a Command->"; cin >> command; cout << endl; return command; } //1번 내용 void Application::AddItem() { cout << "Let's Add Item!" << endl; m_List.AddItem(); } //2번 내용 void Application::DisplayAllItem() { cout << "Let's Display All Items!" << endl; m_List.DisplayAllItem(); } //3번 내용 void Application::EmptyList() { cout << "Let's Make Empty List!" << endl; m_List.MakeEmpty(); } //4번 내용 void Application::SearchItem() { cout << "Let's Search Item!" << endl; m_List.Search(); } | cs |
Application.cpp에 대한 내용이다. 4번까지 구현하였다. 8번까지 구현을 해야하는데 많이 부족하다. 서둘러야겠다는 생각을 했다.
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 | #include "ArrayList.h" ArrayList::ArrayList() { m_NumOfData = 0; } ArrayList::~ArrayList() { } bool ArrayList::IsFull() { if (m_NumOfData == MAXSIZE) { cout << "List is Full!" << endl; return TRUE; } else { return FALSE; } } int ArrayList::AddItem() { ItemType data; data.SetDataFormKB(); if(!IsFull()) { m_Array[m_NumOfData] = data; m_NumOfData++; cout << data.GetData()<<" Insert Successfully!" << endl; return TRUE; } else { cout << "List is FUll!!" << endl; return FALSE; } } void ArrayList::DisplayAllItem() { ItemType data; ResetCurPoint(); int curIndex = GetNextItem(data); if (!m_NumOfData) { cout << "\nThere's nothing to show List in this array!" << endl; } while (curIndex < m_NumOfData && curIndex != -1) { cout << "데이터 출력 : " << data.GetData() << endl; curIndex = GetNextItem(data); } } void ArrayList::ResetCurPoint() { m_CurPointer = -1; } int ArrayList::GetNextItem(ItemType& data) { m_CurPointer++; if (m_CurPointer == MAXSIZE) { return -1; } else{ data = m_Array[m_CurPointer]; return m_CurPointer; } } void ArrayList::MakeEmpty() { m_NumOfData = 0; } void ArrayList::Search() { ItemType temp; if (Get(temp)) { cout << "->" << temp.GetData() << endl; } } bool ArrayList::Get(ItemType& data) { data.SetDataFormKB(); for (int i = 0; i < m_NumOfData; i++) { if (data.GetData() == m_Array[i].GetData()) { data = m_Array[i]; cout << "Get Data Successfully!"; return TRUE; } } cout << "Fail to find Data!"; return FALSE; } | cs |
실질적인 데이터를 처리하는 부분인 ArrayList.cpp이다. 각종 데이터 처리하는데 어려움이 있었다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include "ItemType.h" ItemType::ItemType() { } ItemType::~ItemType() { } void ItemType::SetDataFormKB() { cout << "Input Data->"; cin >> data; } int ItemType::GetData() { return data; } | cs |
데이터를 입력받거나, 출력하는 것에 쓰이는 get,set함수들을 만들어 보았다. 내일은 5~8번까지 만들어 보려고 한다.