#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Node{
char number[11];
struct Node *leftChild;
struct Node *rightSibling;
} Node;
Node* CreateNode(char NewData[]) // 노드생성
{
Node * NewNode = (Node *)malloc(sizeof(Node));
memset(NewNode->number, 0, sizeof(char) * 11);
strcpy(NewNode->number, NewData);
NewNode->leftChild = NULL;
NewNode->rightSibling = NULL;
return NewNode;
}
void AddChildNode(Node* parent, Node* child) //노드추가
{
if (parent->leftChild == NULL)
parent->leftChild = child;
else
{
Node* tempNode = parent->leftChild;
while (tempNode->rightSibling != NULL)
{
tempNode = tempNode->rightSibling;
}
tempNode->rightSibling = child;
}
}
void PrintTree(Node* node) //전화번호를 입력받아 완성된 트리에서 값을비교해서 유일성을 검사하는 프로그램 ex) 전화번호가 911과 91124라는 번호가 잇다고 하면 911이라는 번호때문에 91124에 전화를 걸지못하므로 No를 출력
{
char temp[11];
Node* tempNode = node->leftChild;
memset(temp, 0, sizeof(char) * 11);
strcpy(temp, node->number);
puts("1");
while (tempNode->rightSibling != NULL && !tempNode)
{
if (strncmp(temp, tempNode->number, strlen(temp)) == 0) //디버깅시 여기서 터짐
{
printf("No\n");
puts("2");
}
else if (strncmp(temp, tempNode->rightSibling->number, strlen(temp)) == 0)
{
printf("No\n");
puts("3");
}
else
{
printf("Yes\n");
puts("4");
}
tempNode = tempNode->rightSibling;
puts("5");
}
if (node->leftChild != NULL)
{
puts("6");
PrintTree(node->leftChild);
}
if (node->rightSibling != NULL)
{
puts("7");
PrintTree(node->rightSibling);
}
}
int main(){
int N = 0, A = 0;
char input[11];
Node * List = NULL;
Node * c = NULL;
scanf("%d", &N); //몇개의 테스트를 할 것이가?
for (int i = 0; i < N; i++)
{
scanf("%d", &A); //몇개의 전화번호를 비교할것인가?
for (int j = 0; j < A; j++)
{
scanf("%s", &input); //전화번호 입력
if (List == NULL) List = CreateNode(input);
else c = CreateNode(input);
if (j > 0)
{
if (j < 5000) AddChildNode(List, c);
else AddChildNode(List->leftChild, c);
}
c = NULL;
}
PrintTree(List);
free(List);
}
}
전화번호 리스트를 입력받아 유일성을 테스트하는 프로그램입니다.
트리로 구현하는게 조건이라 푸는도중 while문에서 프로그램이 터져버립니다.
아마 if문의 조건식인 strncmp에서 Null값이 들어오면 터져버리는거 같은데 어떻게 비교해야할지 도저히 몰라 질문드립니다. ㅠㅠ
트리에는 순서대로 저장되는걸 확인하였습니다.
입출력 예
입력
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
출력
NO
YES
입니다. ㅜㅜ
댓글 0