배열 기반 리스트 이해 안가는 부분 있습니다! 봐주세요(시쓰는 님 봐주셨으면)
철죽
헤더 파일 입니다.
#ifndef __ARRAY_LIST_H__
#define __ARRAY_LIST_H__
#define TRUE 1
#define FALSE 0
/*** ArrayList의 정의 ****/
#define LIST_LEN 100
typedef char* LData;
typedef struct __ArrayList
{
LData arr[LIST_LEN];
int numOfData;
int curPosition;
} ArrayList;
typedef ArrayList List;
/*** ArrayList와 관련된 연산들 ****/
void ListInit(List * plist); //list 초기화
void LInsert(List * plist, LData data); //문자열 입력
int LFirst(List * plist, LData pdata); //리스트 첫 번째 데이터 출력
int LNext(List * plist, LData pdata); // 리스트 다음 데이터 출력
LData LRemove(List * plist); // 리스트에 있는 데이터 삭제
int LCount(List * plist); // 리스트의 데이터 수 출력
void LReverse(List *plist); // 데이터 역순 출력
void LAppend(List *plist, LData data); // 데이터 합치기
void LFlatten(List *plist,LData data); // ()지우기
#endif
배열 기반 리스트 소스입니다
#include stdio.h
#include string.h
#include stdlib.h
#include ArrayList.h
void ListInit(List *plist)
{
(plist-numOfData) = 0;
(plist-curPosition) = -1;
}
void LInsert(List *plist, LData data)
{
char *s; // 새롭게 할당한 메모리의 주소
int n; // 새롭게 할당한 메모리의 크기
if(plist-numOfData LIST_LEN) {
puts(저장이 불가능합니다.);
return;
}
n = strlen(data)+1;
s = (char *)malloc(sizeof(char)*n);
memcpy(s,data,sizeof(char)*n);
plist-arr[plist-numOfData] = s;
plist-numOfData++;
}
void LReverse(List *plist)
{
char *t;
int i,j;
i = 0;
j = plist-numOfData-1;
while(i j)
{
t = plist-arr[i];
plist-arr[i] = plist-arr[j];
plist-arr[j] = t;
i++;
j--;
}
}
void LAppend(List *plist, LData data)
{
List list1,list2;
int i=0,j=0;
char *s; // 새롭게 할당한 메모리의 주소
int n; // 새롭게 할당한 메모리의 크기
n=strlen(data)+1;
s = (char *)malloc(sizeof(char)*n);
memcpy(s,data,sizeof(char)*n);
list1.arr[i] = s;
i++;
if(strcmp(data,|))
{
list2.arr[list2.numOfData]=s;
j++;
}
plist-arr[plist-numOfData] = list1.arr[i];
plist-numOfData++;
if(plist-numOfData i)
{
plist-arr[plist-numOfData] = list2.arr[j];
plist-numOfData++;
}
}
/*void LFlatten(List *plist,LData data)
{
int numOfLeftWidth=0;
int numOfRightWidth=0;
if(strcmp(plist-arr[plist-numOfData],())
{
free(plist-arr[plist-numOfData]);
numOfLeftWidth++;
}
else if(strcmp(plist-arr[plist-numOfData],)))
{
free(plist-arr[plist-numOfData]);
numOfRightWidth++;
}
}*/
int LFirst(List *plist, LData pdata)
{
if(plist-numOfData == 0)
return FALSE;
plist-curPosition = 0;
strcpy(pdata,plist-arr[0]);
return TRUE;
}
int LNext(List * plist, LData pdata)
{
if(plist-curPosition = (plist-numOfData)-1)
return FALSE;
plist-curPosition++;
strcpy(pdata,plist-arr[plist-curPosition]);
return TRUE;
}
LData LRemove(List * plist)
{
int rpos = plist-curPosition;
int num = plist-numOfData;
int i;
LData rdata = plist-arr[rpos];
for(i=rpos; inum-1; i++)
plist-arr[i] = plist-arr[i+1];
(plist-numOfData)--;
(plist-curPosition)--;
return rdata;
}
int LCount(List * plist)
{
return plist-numOfData;
}
마지막 메인 소스입니다!
#include stdio.h
#include string.h
#include stdlib.h
#include ArrayList.h
#define STR_LEN 100
int main(void)
{
FILE * fr;//읽는 파일
FILE * fw;//쓰는 파일
int n;
/*리스트 초기화 과정 */
List list;
char data[STR_LEN];
char * command;
char * reverse = reverse;
ListInit(&list);
fr = fopen(Problem.txt,r);
if(fr==NULL)
{
puts(file open error);
return -1;
}
fscanf(fr,%[^ (]%*[ (],data);// 명령어 읽음.
command = data;
while( !feof(fr))
{
n = fscanf(fr,%[^ )]%*[ )]%*[ |],data);// a b c 읽음
if( n 1) break;
LInsert(&list,data);
}
if(strcmp(command,reverse))
{
LReverse(&list);
}
else if(strcmp(command,append))
{
LAppend(&list,data);
}
/*else if(strcmp(command,flatten))
{
LFlatten(&list,data);
}*/
fclose(fr);
fw = fopen(Solve.txt,w);if( LFirst(&list,data))
{
fprintf(fw,(%s,data);
while( LNext(&list,data))
{
fprintf(fw, %s,data);
}
fprintf(fw,),data);
}
fclose(fw);
return 0;
}프로그램을 설瀏??설명하자면..
메모장에 있는 문자열을 읽어 리스트에 저장 후
reverse (a b c) 경우
(c b a) 역순으로 출력해야 합니다.
reverse ((ab) (bc)) 경우
((bc) (ab)) 이런식으로 출력이 나와야 합니다.
append (a) | (b)
(a b)로 출력 됩니다.
apeend (a (ab) | (b (ab))
(a (ab) b (ab))
출력 됩니다.
flatten ((a) ((bcd)))
(a bcd) 로 출력 됩니다. 안에 가로를 다 삭제 하는함수 입니다.
여기서 질문 입니다.
1. fscanf(fr,%[^ (]%*[ (],data);앞에 있는 명령어를 읽을 때 이런식으로 쓰셨는데..
fscanf(fr,%s,data);이런 식으로 써도 읽을 수 있는데 왜 이렇게 쓰셨는가요..
근데 제 방식 대로하면 프로그램이 안돌아 가더군요..
2.그리고 n = fscanf(fr,%[^ )]%*[ )]%*[ |],data);
if(n1)break;
이해가 잘 안됩니다. n에는 무엇이 들어가죠? ( )안에 있는 문자열이 들어가는게 맞나요(제 생각임)
3.그리고 append 함수를 만들었는데.. |앞에 있는 문자열은 list1에 저장하고 |뒤에 있는 함수는 list2에 저장 후 배열 기반 리스트에 순차적으로 들어가서 붙이는 방식으로 코드를 짰습니다.
근데 출력을 해보면.. append (a) | (b)
((a b) 이런식으로 출력이 나오더군요.. (a b)로 나와야 하는데.. 아직 fscanf에서 %*[] %[]에 이해가 부족한거 같습니다.
4.마지막으로 flatten 함수를 작성하려고 하는데.. ((ab) (bc))일 경우 (ab bc)로 출력 되어야 하는데.. 문자열이라서.. ((ab)가 한꺼번에 들어가서 ( 를 따라 못읽는 데 어떤 방식으로 코딩을 해야 할까요? (만 따로 빼서 입력 할 수 있다면.. 코드를 짤 것 같은데.. 문자열로 배열 리스트안으로 들어가서.. (만 따로 빼서 저장 하는 방법을 모르겠습니다. 아니면 다른 방식으로 생각해야 하나요?
길지만 고수님들 부탁드릴께요!
쫌 길지만.. 고수님들 부탁드립니다.