// Main 함수
 
#include <stdio.h>
#include <stdlib.h>
 
#include "linkedArrayList.h"
 
int main(void)
{
 // 메뉴 입력 번호 변수
 int inputNumber;
 CString str;
 // List 생성 및 초기화
 arrayList* list = new arrayList();
 list->ListInit();
 
  while(1)
  {
   printf("\n메뉴: 0-종료 1-출력 2-추가 3-검색 4-삭제\n");
   printf("입력 : " );
   scanf("%d", &inputNumber);
 
   switch (inputNumber)
   {
   case 0:
    {
     printf("종료합니다");
    delete list;
     return 0;
    }
   case 1:
    {
     list->LPrint();
     break;
    }
   case 2:
    {
    char inputString[LIST_LEN] = "";    
 
    printf("저장하려는 문자를 입력하세요\n");
    printf("입력: ");
    scanf("%s", inputString);
 
    str = inputString;
    
     list->LInsert(str);
     break;
    }
  case 3:
   {
    char inputString[LIST_LEN] = ""; 
 
    printf("검색할 문자열 입력: ");
    scanf("%s", inputString);  
    str = inputString;
 
    list->LSearch(str);    
    break;
   }
  case 4:
   {
    char inputString[LIST_LEN] = ""; 
 
    printf("삭제할 문자열 입력: ");
    scanf("%s", inputString); 
    str = inputString;
 
    list->LRemove(str);   
    break;
   }
   default:
    continue;
   }
  }
 
 return 0;
}
 
// linkedList.h 파일
#ifndef __ARRAY_LIST_H__
#define __ARRAY_LIST_H__
 
#include "atlstr.h"
 
#define TRUE 1
#define FALSE 0
#define LIST_LEN 100
 
typedef struct _STRING
{
 CString str;
 struct _STRING* next;
} STRING;
 
typedef STRING List;
 
class arrayList
{
public:
 arrayList();
 ~arrayList();
 
 void ListInit();  
 char* LPrint();
 List* LNext(List* list);
  void LInsert(CString str);
 char* LSearch(CString& searchString);
  int findSubString(CString& str, CString& findStr);
 void LRemove(CString& searchString);
 //List* nextFind(List* nextNode);
 
private:
 List * head;
 List * cur;
 List * before;
 int numOfData;
 
};
 
#endif
 
// linkedList.cpp 파일
 
 
#include <stdio.h>
#include "linkedArrayList.h"
 
arrayList::arrayList()
{
 head = NULL;
 cur = NULL;
 before = NULL;
 numOfData = 0;
}
 
arrayList::~arrayList()
{
 
}
 
void arrayList::ListInit()
{
  head = new List();
   head->next = NULL;
 head->str = "";
   cur = NULL;
   before = head; 
   before->next = NULL;
}
 
char* arrayList::LPrint()
{
 List* nextNode = head->next;
 
 while(1)
 {
  if(nextNode != NULL )
  {
   if( nextNode->next == NULL )
   {
    wprintf(_T("%s "), nextNode->str);
    return 0;
   }
   else
   {
    wprintf(_T("%s "), nextNode->str);
    nextNode = LNext(nextNode);      
   }   
  }
  else
   break;
 }
 
 return 0;
}
 
List* arrayList::LNext(List* list)
{
 List* nextNode = list->next;
 
 return nextNode;
}
 
void arrayList::LInsert(CString str)
{
 List* newNode = new List;                       // 다음번 노드를 동적할당 하고
 
 if( numOfData >= LIST_LEN)
 {
  puts("저장할 용량을 초과했습니다");
  delete newNode;
  return;
 } 
 
 newNode->next = head->next;                                  // 새로 생긴 공간의 다음에 널을 만든다
 newNode->str = str;
 head->next = newNode; 
 cur = newNode;  
 
 numOfData++;
}
 
char* arrayList::LSearch(CString& str)
{
 List* nextNode = head->next;
 int pFind;
 
 while(1)
 {
  if(nextNode != NULL)
  {
   if( nextNode->next == NULL )
   {   
    pFind = findSubString(nextNode->str, str);
 
    if( !pFind )
    {
     wprintf(_T("%s"), nextNode->str);
    }
    return 0;
   }
   else
   {
    pFind = findSubString(nextNode->str, str);    
 
    if( !pFind )
    {
     wprintf(_T("%s"), nextNode->str);
    }   
    nextNode = LNext(nextNode);
   }
  }
  else
   break;
 }
 return 0;
}
 
int arrayList::findSubString(CString& str, CString& searchStr)
{
 int ret;
 ret = str.Compare(searchStr);
 
 return ret; 
}
 
void arrayList::LRemove(CString& searchString)
{
 List* nextNode = head->next;
 int pFind;
 
 while(1)
 {
  if(nextNode != NULL)
  {
   pFind = findSubString(nextNode->str, searchString);
 
   if( !pFind )
   {
    cur = nextNode->next;
    delete nextNode;
    nextNode = NULL;
    before->next = cur;
    (numOfData)--;
    nextNode = LNext(before);
   }
   else
   {
    before = nextNode;
    nextNode = LNext(nextNode);
 
    if( nextNode == NULL)
     return;   
   }
  }
  else
   break;
  
 }
 
 return;
}
 
// List* arrayList::nextFind(List* nextNode)
// {
//  List* next = nextNode->next;
//
//  if( next != NULL)
//   return next;
//
//  return 0;
// }

'Computer Science > 자료구조' 카테고리의 다른 글

추상자료형이란?  (0) 2022.10.02
리스트 구현 문제  (0) 2022.10.02
자료구조의 이해(1)  (0) 2017.10.21

+ Recent posts