Struct STRING
{
        Char string[100];
        STRING* pstring;
}
 
위와 같은 구조체 사용하세요
 

포인터 배열을 사용해서, 문자열에 대해 다음과 같은 메뉴를 처리합니다.
 
     메뉴 : 0. 종료  1. 출력  2. 추가  3. 검색  4. 삭제
 
   출력 : 지금까지 입력한 모든 문자열을 출력합니다.
   추가 : 새로운 문자열을 포인터 배열에 추가합니다.
   검색 : 검색할 문자열을 포함하고 있는 모든 문자열을 출력합니다.
   삭제 : 삭제할 문자열을 포함하고 있는 모든 문자열을 삭제합니다.
 
   이 문제는 입력할 문자열의 개수와 문자열의 길이가 정해져 있지않다는 점에서 어렵습니다.
   또한 검색과 삭제에서 완전히 똑같은 문자열이 아니라 부분 문자열을 처리하는 것도 어렵습니다.
 
   [입출력]
   메뉴 : 0. 종료  1. 출력  2. 추가  3. 검색  4. 삭제
   선택 : 2
   단어 : bonus
   메뉴 : 0. 종료  1. 출력  2. 추가  3. 검색  4. 삭제
   선택 : 2
   단어 : big_money
   메뉴 : 0. 종료  1. 출력  2. 추가  3. 검색  4. 삭제
   선택 : 2
   단어 : rainbow
   메뉴 : 0. 종료  1. 출력  2. 추가  3. 검색  4. 삭제
   선택 : 2
   단어 : handshaking
   메뉴 : 0. 종료  1. 출력  2. 추가  3. 검색  4. 삭제
   선택 : 2
   단어 : switch_on
   메뉴 : 0. 종료  1. 출력  2. 추가  3. 검색  4. 삭제
   선택 : 1
   bonus big_money rainbow handshaking switch_on
   메뉴 : 0. 종료  1. 출력  2. 추가  3. 검색  4. 삭제
   선택 : 3
   단어 : on
   bonus big_money switch_on
   메뉴 : 0. 종료  1. 출력  2. 추가  3. 검색  4. 삭제
   선택 : 4
   단어 : on
   메뉴 : 0. 종료  1. 출력  2. 추가  3. 검색  4. 삭제
   선택 : 1
   handshaking rainbow
   메뉴 : 0. 종료  1. 출력  2. 추가  3. 검색  4. 삭제
   선택 : 0

 

 

 

 

 

Main.c 파일
 
#include <stdio.h>
#include <stdlib.h>
 
#include "linkedArrayList.h"
#include "stringData.h"
 
int main(void)
{
 // 메뉴 입력 번호 변수
 int inputNumber;
 // List 생성 및 초기화
 lList list;
 List * pString;
 char* nullString = 0;
 ListInit(&list);
 
 while(1)
 {
  printf("\n메뉴: 0-종료 1-출력 2-추가 3-검색 4-삭제\n");
  printf("입력 : " );
  scanf("%d", &inputNumber);
 
  switch (inputNumber)
  {
  case 0:
   {
    printf("종료합니다");
    return 0;
   }
  case 1:
   {
    LPrint(list.head);
    break;
   }
  case 2:
   {
    pString = makeString();
    LInsert(&list, pString);
    break;
   }
  case 3:
   {
    printf("검색할 문자열 입력: ");
    scanf("%s", &nullString);   
 
    LSearch(list.head, &nullString);
    nullString = 0;
    break;
   }
  case 4:
   {
    printf("삭제할 문자열 입력: ");
    scanf("%s", &nullString); 
 
    LRemove(&list, &nullString);
    nullString = 0;
    break;
   }
  default:
   continue;
  }
 }
 
 return 0;
}
 
LinkedArrayList.h 파일
#ifndef __ARRAY_LIST_H__
#define __ARRAY_LIST_H__
 
#include "stringData.h"
 
#define TRUE 1
#define FALSE 0
 
typedef struct _linkedList
{
 List * head;
 List * cur;
 List * before;
 int numOfData;
} linkedList;
 
typedef linkedList lList;
 
// 해당 문제의 ADT
void ListInit(lList * plist);    
// 초기화할 리스트의 주소값을 인자로 전달
// 리스트 생성 후 제일 먼저 호출되어야 하는 함수이다
char* LPrint(List *plist);
// 저장된 문자열 모두를 출력한다
void LInsert(lList * plist); //, LData data);
// 리스트에 데이터를 저장한다. 매개변수 data에 전달된 값을 저장한다
char* LSearch(List * plist, char * searchString);
char* findSubString(char* str, char* findStr); //, char* searchStr)
// 리스트에서 특정 문자열을 찾아서 찾은 문자열을 반환한다
/*int LFirst(List * plist, LData * pdata);*/
// 첫번째 데이터가 pdata가 가리키는 메모리에 저장된다
// 데이터의 참조를 위한 초기화가 진행된다
// 참조 성공시 True 실패시 False를 반환
/*int LNext(List * plist, LData * pdata);*/
// 참조된 데이터의 다음 데이터가 pdata가 가리키는 메모리에 저장된다
// 순차적인 참조를 위해서 반복 호출이 가능하다
// 참조를 새로 시작하려면 먼저 LFirst 함수를 호출한다
// 참조 성공 시 True 실패시 False를 반환
void LRemove(lList * plist, char * searchString);
List* nextFind(List* nextNode);
// LFirst 또는 LNext 함수의 마지막 반환 데이터를 삭제 한다
// 삭제된 데이터는 반환된다
// 마지막 반환 데이터를 삭제하므로 연이은 반복 호출을 허용하지 않는다
/*int LCount(List * plist);*/
// 리스트에 저장되어 있는 데이터의 수를 반환한다
 
#endif
 
stringData.h 파일
 
#ifndef __NAME_CARD_H__
#define __NAME_CARD_H__
 
#define LIST_LEN 100
 
typedef struct _STRING
{
 char stringData[LIST_LEN];
 struct _STRING* next;
} STRING;
 
typedef STRING List;
 
List* makeString();
 
#endif

 

// LinkedArrayList.c 파일
 
#include <stdio.h>
#include "linkedArrayList.h"
 
void ListInit(lList *pList)
{
 pList->head = (List *)malloc(sizeof(List));
 pList->head->next = NULL;
 memset(pList->head->stringData, 0, sizeof(pList->head->stringData));
 //pList->head->stringData[LIST_LEN] = {0};
 pList->cur = NULL;
 pList->before = pList->head; 
 pList->before->next = NULL;
 pList->numOfData = 0;
}
 
char* LPrint(List *plist)
{
 List* nextNode = plist->next;
 if(nextNode)
 {
  if( nextNode->next == NULL )
  {
   printf("%s ", nextNode->stringData);
   return 0;
  }
  else
  {
   LPrint(nextNode); 
   printf("%s ", nextNode->stringData);
  }
 }
 
 return 0;
}
 
void LInsert(lList * plist, List* pStringData)
{
 List* newNode = (List *)malloc(sizeof(List));                       // 다음번 노드를 동적할당 하고
 strcpy(newNode->stringData, pStringData->stringData);
 
 if( plist->numOfData >= LIST_LEN)
 {
  puts("저장할 용량을 초과했습니다");
  free(newNode);
  return;
 } 
 
 newNode->next = plist->head->next;                                  // 새로 생긴 공간의 다음에 널을 만든다
 plist->head->next = newNode; 
 plist->cur = pStringData;  
 
 plist->numOfData++;
}
 
char* LSearch(List * plist, char * searchString)
{
 List* nextNode = plist->next;
 char *pFind;
 
 if(nextNode)
 {
  if( nextNode->next == NULL )
  {   
   pFind = findSubString(nextNode->stringData, searchString);
 
   if( !pFind )
   {
    printf("%s", nextNode->stringData);
   }
 
   return 0;
  }
  else
  {
   LSearch(nextNode, searchString); 
 
   pFind = findSubString(nextNode->stringData, searchString);
 
   if( !pFind )
   {
    printf("%s", nextNode->stringData);
   }   
  }
 }
 
 return 0;
}
 
char* findSubString(char* str, char* searchStr)
{
 if( strstr(str, searchStr) != NULL )
  return 0;
 else
  return -1;
}
void LRemove(lList * plist, char * searchString)
{
 List* nextNode = plist->head;
 char *pFind;
 
 while(1)
 {
  //if( nextNode->stringData == NULL)
  // return;
 
  pFind = findSubString(nextNode->stringData, searchString);
 
  if( !pFind )
  {
   plist->cur = nextNode->next;
   free(nextNode);
   nextNode->stringData[0] = 0;
   plist->before->next = plist->cur;
   (plist->numOfData)--;
   nextNode = nextFind(plist->before);
  }
  else
  {
   plist->before = nextNode;
   nextNode = nextFind(nextNode);
 
   if( nextNode == NULL)
    return;   
  }
 }
 
 return;
}
 
List* nextFind(List* nextNode)
{
 List* next = nextNode->next;
 
 if( next != NULL)
  return next;
 
 return 0;
}
 
// stringData.c 파일
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#include "stringData.h"
 
List * makeString()
{
 char inputString[LIST_LEN] = "";
 List * newString = (List *)malloc(sizeof(List));
 
 printf("저장하려는 문자를 입력하세요\n");
 printf("입력: ");
 scanf("%s", inputString);
 
 strcpy(newString->stringData, inputString);
 return newString;
}

 

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

추상자료형이란?  (0) 2022.10.02
C++ 기반  (0) 2022.10.02
자료구조의 이해(1)  (0) 2017.10.21

+ Recent posts