-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquivalenceClass.cpp
More file actions
74 lines (65 loc) · 1.63 KB
/
EquivalenceClass.cpp
File metadata and controls
74 lines (65 loc) · 1.63 KB
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
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 24
#define FALSE 0
#define TRUE 1
/*연결 리스트를 위한 노드 구조 정의*/
typedef struct node *nodePointer;
typedef struct node{
int data;
nodePointer link;
};
int main(){
short int out[MAX_SIZE];
nodePointer seq[MAX_SIZE];
nodePointer x,y,top;
int i,j,n;
/*배열의 크기 설정*/
printf("크기를 입력하세요. (<= %d) ", MAX_SIZE);
scanf("%d", &n);
/*out 배열을 true로, seq배열을 null로 초기화*/
for(i=0;i<n;i++){
out[i]=TRUE;
seq[i]=NULL;
}
printf("동치 쌍을 입력하세요.(-1,-1 입력 시 종료):");
scanf("%d%d",&i,&j);
while(i>=0){ //i가 음수가 되면 실행을 그만한다.
x=(node*)malloc(sizeof(*x));
x->data=j;
x->link=seq[i];
seq[i]=x;
//x노드에 공간을 임시로 할당해 j값을 넣고 seq[i]가 노드를 가리키게 한다.
x=(node*)malloc(sizeof(*x));
x->data=i;
x->link=seq[j];
seq[j]=x;
//x노드에 공간을 임시로 할당해 i값을 넣고 seq[j]가 노드를 가리키게 한다.
printf("동치 쌍을 입력하세요.(-1,-1입력 시 종료):");
scanf("%d%d",&i,&j);
}
/*동치 부류를 출력한다.*/
for(i=0;i<n;i++)
if(out[i]){ //out[i]의 값이 null이라면 실행하지 않는다.
printf("\n동치 부류 = %5d",i);
out[i]=FALSE;//i를 출력한 후 out 값을 FALSE로 만든다.
x=seq[i]; top=NULL;
for(;;){ //i와 동치 부류인 것을 모두 출력하는 반복문
while(x){
j=x->data;
if(out[j]){
printf("%5d",j);
out[j]=FALSE;//i와 동치 부류인 것들을 출력한 후 out 값을 FALSE로 만든다.
y=x->link;
x->link=top;
top=x;
x=y;
}
else x=x->link;
}
if(!top) break;
x=seq[top->data]; top=top->link;
}
}
return 0;
}