SlideShare uma empresa Scribd logo
1 de 13
Baixar para ler offline
project #4



통신망에서 길 찾기


             이산치수학 C1조



             20083458 민영지
             20093474 박구남
             20093526 정준용
             20093530 최고봉
순서




      •조원별 업무분담


         •일정 계획


         •문제 파악


       •알고리즘 계획


          •소스구현


•문제점 파악 / 해결법 토의


          •최종소스
1)조원별 업무 분담
              최종 보고서 작성 및 프로젝트 총괄
민영지
              및 자료 조사


박구남           알고리즘 구상 및 소스 구현



정준용           알고리즘 구상 및 소스 구현



최고봉           알고리즘 구상 및 소스 구현




2)일정계획
              과제에 대한 자료 조사 및 업무분담
05.14~05.16
              일정 계획

              프로젝트 알고리즘 구성 및 소스 구
05.16~05.21
              현


05.21~05.23   최종 소스 확인 및 최종보고서 작성
3)문제파악


Degree 출력


-adjacent matrix 가 주어졌을 때 indegree/outdegree 를 출력하고

source/sink를 구별하시오 .




-입력


 4
 AB, BD, CB, DC




-출력


 A 0 1 source
 B 2 1
 C 1 1
 D 1 1
4)알고리즘 계획


 노드의 개수를 입력 받은 후 그 개수만큼의 빈 Node를 만든
다. 입력받은 Edge의 값과 비교하여 indegree와 outdegree의
값을 증가시킨다. 출력하는 부분에서 indegree의 값이 0이면
Source, outdegree의 값이 0이면 Sink를 출력하고 끝낸다.



5)소스 구현


1차 소스
 #include <stdio.h>
 #include <stdlib.h>

 typedef struct Node {
      char name;
      struct Node *next;
      struct Node *prev;
 }Node;

 typedef struct rst {
       int indgree;
       int outdgree;
 }rst;

 Node **graph;

 void InitGraph(int n);
 Node *AppendNode(Node *Target, Node *aNode);
 int DeleteNode(Node *Target);
 void UnInitGraph(int n);
void main() {
     int n, i, j, x;
     char name[3];
     Node *Now, temp;
     rst *result;
     temp.name = 0;     temp.next = NULL; temp.prev = NULL;

    printf("노드의 개수를 입력해주세요.n");
    scanf("%d",&n);
    result = (rst *)malloc(n*sizeof(rst));
    for( i = 0 ; i < n ; i++ ) {
            result[i].indgree = 0;
            result[i].outdgree = 0;
    }
    InitGraph(n);
    printf("Edge를 입력해주세요.(예, AB, BD)n입력을 끝내시려면 0을
            입력하세요.n");
    while(1) {
            scanf("%s",name);
            if( name[0] == '0' ) break;

          for( x = 0 ; x < n ; x++ ) {
                 Now = graph[x];
                 if( graph[x]->name == name[0] ) {
                         temp.name = name[1];
                         AppendNode(Now,&temp);
                         result[x].outdgree++;
                         break;
                 }
                 else if( graph[x]->next == NULL ) {
                         graph[x]->name = name[0];
                         temp.name = name[1];
                         AppendNode(Now,&temp);
                         result[x].outdgree++;
                         break;
                 }
          }
    }
    for( i = 0, j = 0 ; ; j++ ) {
if( j >= n ) {
                  j = -1;
                  i++;
                  if( i >= n ) break;
                  continue;
           }
           Now = graph[j]->next;
           while(1) {
                  if( Now == NULL )
                          break;
                  if( graph[i]->name == Now->name )
                         result[i].indgree++;
                  Now = Now->next;
           }
    }
    for( i = 0 ; i < n ; i++ ) {
            printf("%c %d %d ", graph[i]->name, result[i].indgree,
                    result[i].outdgree);
            if( result[i].indgree == 0 )
                    printf("Source");
            else if( result[i].outdgree == 0 )
                    printf("Sink");
            printf("n");
    }
    free(result);
    UnInitGraph(n);
    printf("n계속하시려면 아무키나 누르세요....");
    getch();
    system("PAUSE");
}

void InitGraph(int n) {
     int i;
     graph = (Node **)malloc(n*sizeof(Node *));
     for( i = 0 ; i < n ; i++ ) {
            graph[i] = (Node *)malloc(sizeof(Node));
            graph[i]->next = NULL;
            graph[i]->prev = NULL;
     }
}

Node *AppendNode(Node *Target, Node *aNode) {
    Node *New;
    Node *Right;
    Node *tail;
    New = (Node *)malloc(sizeof(Node));
    for(tail=Target;tail->next;tail=tail->next);
    *New = *aNode;
    Right = tail->next;
    New->next = Right;
    New->prev = tail;
    tail->next = New;
    if (Right) {
            Right->prev = New;
    }
    return New;
}

int DeleteNode(Node *Target) {
     Node *Left, *Right;
     if( Target == NULL )
            return 0;
     Left = Target->prev;
     Right = Target->next;
     Left->next = Right;
     if (Right)
            Right->prev = Left;
     free(Target);
     return 1;
}

void UnInitGraph(int n) {
     int i;
     for( i = 0 ; i < n ; i++ )
            while(DeleteNode(graph[i]->next));
     free(graph);
     graph = NULL;
}
1차 소스 문제점
- 입력하여 출력할 때 Sink가 되면 제대로 출력되지 않았다.
     for( i = 0 ; i < n ; i++ ) {
            printf("%c %d %d ", graph[i]->name, result[i].indgree,
                    result[i].outdgree);
            if( result[i].indgree == 0 )
                    printf("Source");
            else if( result[i].outdgree == 0 )
                    printf("Sink");
            printf("n");
     }


최종소스
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
     char name;
     struct Node *next;
     struct Node *prev;
}Node;

typedef struct rst {
      int indgree;
      int outdgree;
}rst;

Node **graph;

void InitGraph(int n);
Node *AppendNode(Node *Target, Node *aNode);
int DeleteNode(Node *Target);
void UnInitGraph(int n);

void main() {
     int n, i, j, x;
     char name[3];
Node *Now, temp;
   rst *result;
   temp.name = 0;     temp.next = NULL; temp.prev = NULL;

   printf("노드의 개수를 입력해주세요.n");
   scanf("%d",&n);
   result = (rst *)malloc(n*sizeof(rst));
   for( i = 0 ; i < n ; i++ ) {
           result[i].indgree = 0;
           result[i].outdgree = 0;
   }
   InitGraph(n);
   printf("Edge를 입력해주세요.(예, AB, BD)n입력을 끝내시려면 0 을
입력하세요.n");
   while(1) {
           scanf("%s",name);
           if( name[0] == '0' ) break;

        for( x = 0 ; x < n ; x++ ) {
               Now = graph[x];
               if( graph[x]->name == name[0] ) {
                       temp.name = name[1];
                       AppendNode(Now,&temp);
                       result[x].outdgree++;
                       break;
               }
               else if( graph[x]->next == NULL ) {
                       graph[x]->name = name[0];
                       temp.name = name[1];
                       AppendNode(Now,&temp);
                       result[x].outdgree++;
                       break;
               }
        }
        for( x = 0 ; x < n ; x++ ) {
               Now = graph[x];
               if( graph[x]->name == name[1] ) {
                       break;
               }
else if( graph[x]->next == NULL ) {
                         graph[x]->name = name[1];
                         temp.name = '0';
                         AppendNode(Now,&temp);
                         break;
                 }
           }
      }
      for( i = 0, j = 0 ; ; j++ ) {
              if( j >= n ) {
                      j = -1;
                      i++;
                      if( i >= n ) break;
                      continue;
              }
              Now = graph[j]->next;
              while(1) {
                      if( Now == NULL )
                               break;
                      if( graph[i]->name == Now->name )
                              result[i].indgree++;
                      Now = Now->next;
              }
      }
      for( i = 0 ; i < n ; i++ ) {
              printf("%c                        %d        %d
",graph[i]->name,result[i].indgree,result[i].outdgree);
              if( result[i].indgree == 0 )
                      printf("Source ");
              if( result[i].outdgree == 0 )
                      printf("Sinkn");
              else
                      printf("n");
      }
      free(result);
      UnInitGraph(n);
      printf("n계속하시려면 아무키나 누르세요....");
      getch();
}
void InitGraph(int n) {
     int i;
     graph = (Node **)malloc(n*sizeof(Node *));
     for( i = 0 ; i < n ; i++ ) {
            graph[i] = (Node *)malloc(sizeof(Node));
            graph[i]->next = NULL;
            graph[i]->prev = NULL;
     }
}

Node *AppendNode(Node *Target, Node *aNode) {
    Node *New;
    Node *Right;
    Node *tail;
    New = (Node *)malloc(sizeof(Node));
    for(tail=Target;tail->next;tail=tail->next);
    *New = *aNode;
    Right = tail->next;
    New->next = Right;
    New->prev = tail;
    tail->next = New;
    if (Right) {
            Right->prev = New;
    }
    return New;
}

int DeleteNode(Node *Target) {
     Node *Left, *Right;
     if( Target == NULL )
            return 0;
     Left = Target->prev;
     Right = Target->next;
     Left->next = Right;
     if (Right)
            Right->prev = Left;
     free(Target);
     return 1;
}

void UnInitGraph(int n) {
     int i;
     for( i = 0 ; i < n ; i++ )
            while(DeleteNode(graph[i]->next));
     free(graph);
     graph = NULL;
}




코딩결과

Mais conteúdo relacionado

Mais procurados

이산치7보고서
이산치7보고서이산치7보고서
이산치7보고서KimChangHoen
 
[Td 2015]java script에게 형(type)이 생겼어요. typescript(박용준)
[Td 2015]java script에게 형(type)이 생겼어요. typescript(박용준)[Td 2015]java script에게 형(type)이 생겼어요. typescript(박용준)
[Td 2015]java script에게 형(type)이 생겼어요. typescript(박용준)Sang Don Kim
 
이산치수학 Project5
이산치수학 Project5이산치수학 Project5
이산치수학 Project5KoChungWook
 
2021 2학기 정기 세미나 4주차
2021 2학기 정기 세미나 4주차2021 2학기 정기 세미나 4주차
2021 2학기 정기 세미나 4주차Moonki Choi
 
2021 2학기 정기 세미나 5주차
2021 2학기 정기 세미나 5주차2021 2학기 정기 세미나 5주차
2021 2학기 정기 세미나 5주차Moonki Choi
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
Tensorflow regression 텐서플로우 회귀
Tensorflow regression 텐서플로우 회귀Tensorflow regression 텐서플로우 회귀
Tensorflow regression 텐서플로우 회귀beom kyun choi
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdfkd19h
 
Day by day iPhone Programming
Day by day iPhone ProgrammingDay by day iPhone Programming
Day by day iPhone ProgrammingYoung Oh Jeong
 
2012 Ds A1 05
2012 Ds A1 052012 Ds A1 05
2012 Ds A1 05seonhyung
 
자료구조 01 최종 보고서
자료구조 01 최종 보고서자료구조 01 최종 보고서
자료구조 01 최종 보고서pkok15
 
정수론적 알고리즘 - Sogang ICPC Team, 2020 Winter
정수론적 알고리즘 - Sogang ICPC Team, 2020 Winter정수론적 알고리즘 - Sogang ICPC Team, 2020 Winter
정수론적 알고리즘 - Sogang ICPC Team, 2020 WinterSuhyun Park
 
세그먼트 트리 느리게 업데이트하기 - Sogang ICPC Team, 2020 Winter
세그먼트 트리 느리게 업데이트하기 - Sogang ICPC Team, 2020 Winter세그먼트 트리 느리게 업데이트하기 - Sogang ICPC Team, 2020 Winter
세그먼트 트리 느리게 업데이트하기 - Sogang ICPC Team, 2020 WinterSuhyun Park
 
2012 Dm C2 04
2012 Dm C2 042012 Dm C2 04
2012 Dm C2 04seonhyung
 
Project#1파스칼 삼각형
Project#1파스칼 삼각형Project#1파스칼 삼각형
Project#1파스칼 삼각형Kimjeongmoo
 

Mais procurados (19)

자료구조05
자료구조05자료구조05
자료구조05
 
이산치7보고서
이산치7보고서이산치7보고서
이산치7보고서
 
[Td 2015]java script에게 형(type)이 생겼어요. typescript(박용준)
[Td 2015]java script에게 형(type)이 생겼어요. typescript(박용준)[Td 2015]java script에게 형(type)이 생겼어요. typescript(박용준)
[Td 2015]java script에게 형(type)이 생겼어요. typescript(박용준)
 
이산치4번
이산치4번이산치4번
이산치4번
 
이산치수학 Project5
이산치수학 Project5이산치수학 Project5
이산치수학 Project5
 
2021 2학기 정기 세미나 4주차
2021 2학기 정기 세미나 4주차2021 2학기 정기 세미나 4주차
2021 2학기 정기 세미나 4주차
 
2021 2학기 정기 세미나 5주차
2021 2학기 정기 세미나 5주차2021 2학기 정기 세미나 5주차
2021 2학기 정기 세미나 5주차
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
Tensorflow regression 텐서플로우 회귀
Tensorflow regression 텐서플로우 회귀Tensorflow regression 텐서플로우 회귀
Tensorflow regression 텐서플로우 회귀
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf
 
Day by day iPhone Programming
Day by day iPhone ProgrammingDay by day iPhone Programming
Day by day iPhone Programming
 
2012 Ds A1 05
2012 Ds A1 052012 Ds A1 05
2012 Ds A1 05
 
자료구조 01 최종 보고서
자료구조 01 최종 보고서자료구조 01 최종 보고서
자료구조 01 최종 보고서
 
정수론적 알고리즘 - Sogang ICPC Team, 2020 Winter
정수론적 알고리즘 - Sogang ICPC Team, 2020 Winter정수론적 알고리즘 - Sogang ICPC Team, 2020 Winter
정수론적 알고리즘 - Sogang ICPC Team, 2020 Winter
 
세그먼트 트리 느리게 업데이트하기 - Sogang ICPC Team, 2020 Winter
세그먼트 트리 느리게 업데이트하기 - Sogang ICPC Team, 2020 Winter세그먼트 트리 느리게 업데이트하기 - Sogang ICPC Team, 2020 Winter
세그먼트 트리 느리게 업데이트하기 - Sogang ICPC Team, 2020 Winter
 
2012 Dm C2 04
2012 Dm C2 042012 Dm C2 04
2012 Dm C2 04
 
Project#1파스칼 삼각형
Project#1파스칼 삼각형Project#1파스칼 삼각형
Project#1파스칼 삼각형
 
이산수학07
이산수학07이산수학07
이산수학07
 

Destaque (14)

이산수학06
이산수학06이산수학06
이산수학06
 
자료구조01
자료구조01자료구조01
자료구조01
 
Wood
WoodWood
Wood
 
자료구조06
자료구조06자료구조06
자료구조06
 
Heart disease
Heart diseaseHeart disease
Heart disease
 
이산수학03
이산수학03이산수학03
이산수학03
 
자료구조03
자료구조03자료구조03
자료구조03
 
이산수학02
이산수학02이산수학02
이산수학02
 
Radios
RadiosRadios
Radios
 
자료구조01
자료구조01자료구조01
자료구조01
 
Análisis de radios de Janeth Crespo
Análisis de radios  de Janeth CrespoAnálisis de radios  de Janeth Crespo
Análisis de radios de Janeth Crespo
 
Análisis de radios
Análisis de radiosAnálisis de radios
Análisis de radios
 
A flowering tree
A flowering treeA flowering tree
A flowering tree
 
자료구조02
자료구조02자료구조02
자료구조02
 

Semelhante a 이산수학05

Project#5 최단거리 찾기 D0 Hwp
Project#5 최단거리 찾기 D0 HwpProject#5 최단거리 찾기 D0 Hwp
Project#5 최단거리 찾기 D0 HwpKimjeongmoo
 
2012 Ds D0 01
2012 Ds D0 012012 Ds D0 01
2012 Ds D0 01chl132435
 
2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdfjinwookhong
 
5통신망에서 길 찾기
5통신망에서 길 찾기5통신망에서 길 찾기
5통신망에서 길 찾기herojoon1378
 
Project#5 통신망에서 길 찾기 Hwp
Project#5 통신망에서 길 찾기 HwpProject#5 통신망에서 길 찾기 Hwp
Project#5 통신망에서 길 찾기 HwpKimjeongmoo
 
2012 Ds B1 01
2012 Ds B1 012012 Ds B1 01
2012 Ds B1 01seonhyung
 
2012 Dm C3 03
2012 Dm C3 032012 Dm C3 03
2012 Dm C3 03chl132435
 
Javascript개발자의 눈으로 python 들여다보기
Javascript개발자의 눈으로 python 들여다보기Javascript개발자의 눈으로 python 들여다보기
Javascript개발자의 눈으로 python 들여다보기지수 윤
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdfjinwookhong
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdfkd19h
 
과제 1,2,3
과제 1,2,3과제 1,2,3
과제 1,2,3mil23
 
프로젝트 보고서
프로젝트 보고서프로젝트 보고서
프로젝트 보고서hyungoh kim
 
ffmpeg optimization using CUDA
ffmpeg optimization using CUDAffmpeg optimization using CUDA
ffmpeg optimization using CUDAyyooooon
 
2012 Dm C2 05
2012 Dm C2 052012 Dm C2 05
2012 Dm C2 05seonhyung
 
2012 Ds B2 02 Pdf
2012 Ds B2 02 Pdf2012 Ds B2 02 Pdf
2012 Ds B2 02 Pdfkd19h
 
2012 Ds B2 02
2012 Ds B2 022012 Ds B2 02
2012 Ds B2 02chl132435
 

Semelhante a 이산수학05 (20)

Project#5 최단거리 찾기 D0 Hwp
Project#5 최단거리 찾기 D0 HwpProject#5 최단거리 찾기 D0 Hwp
Project#5 최단거리 찾기 D0 Hwp
 
2012 Ds D0 01
2012 Ds D0 012012 Ds D0 01
2012 Ds D0 01
 
2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf2012 Dm A0 04 Pdf
2012 Dm A0 04 Pdf
 
5통신망에서 길 찾기
5통신망에서 길 찾기5통신망에서 길 찾기
5통신망에서 길 찾기
 
Project#5 통신망에서 길 찾기 Hwp
Project#5 통신망에서 길 찾기 HwpProject#5 통신망에서 길 찾기 Hwp
Project#5 통신망에서 길 찾기 Hwp
 
이산치1번
이산치1번이산치1번
이산치1번
 
2012 Ds B1 01
2012 Ds B1 012012 Ds B1 01
2012 Ds B1 01
 
이산치5번
이산치5번이산치5번
이산치5번
 
2012 Dm C3 03
2012 Dm C3 032012 Dm C3 03
2012 Dm C3 03
 
Javascript개발자의 눈으로 python 들여다보기
Javascript개발자의 눈으로 python 들여다보기Javascript개발자의 눈으로 python 들여다보기
Javascript개발자의 눈으로 python 들여다보기
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf
 
이산치2번
이산치2번이산치2번
이산치2번
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf
 
과제 1,2,3
과제 1,2,3과제 1,2,3
과제 1,2,3
 
프로젝트 보고서
프로젝트 보고서프로젝트 보고서
프로젝트 보고서
 
ffmpeg optimization using CUDA
ffmpeg optimization using CUDAffmpeg optimization using CUDA
ffmpeg optimization using CUDA
 
2012 Dm C2 05
2012 Dm C2 052012 Dm C2 05
2012 Dm C2 05
 
자구2번
자구2번자구2번
자구2번
 
2012 Ds B2 02 Pdf
2012 Ds B2 02 Pdf2012 Ds B2 02 Pdf
2012 Ds B2 02 Pdf
 
2012 Ds B2 02
2012 Ds B2 022012 Ds B2 02
2012 Ds B2 02
 

이산수학05

  • 1. project #4 통신망에서 길 찾기 이산치수학 C1조 20083458 민영지 20093474 박구남 20093526 정준용 20093530 최고봉
  • 2. 순서 •조원별 업무분담 •일정 계획 •문제 파악 •알고리즘 계획 •소스구현 •문제점 파악 / 해결법 토의 •최종소스
  • 3. 1)조원별 업무 분담 최종 보고서 작성 및 프로젝트 총괄 민영지 및 자료 조사 박구남 알고리즘 구상 및 소스 구현 정준용 알고리즘 구상 및 소스 구현 최고봉 알고리즘 구상 및 소스 구현 2)일정계획 과제에 대한 자료 조사 및 업무분담 05.14~05.16 일정 계획 프로젝트 알고리즘 구성 및 소스 구 05.16~05.21 현 05.21~05.23 최종 소스 확인 및 최종보고서 작성
  • 4. 3)문제파악 Degree 출력 -adjacent matrix 가 주어졌을 때 indegree/outdegree 를 출력하고 source/sink를 구별하시오 . -입력 4 AB, BD, CB, DC -출력 A 0 1 source B 2 1 C 1 1 D 1 1
  • 5. 4)알고리즘 계획 노드의 개수를 입력 받은 후 그 개수만큼의 빈 Node를 만든 다. 입력받은 Edge의 값과 비교하여 indegree와 outdegree의 값을 증가시킨다. 출력하는 부분에서 indegree의 값이 0이면 Source, outdegree의 값이 0이면 Sink를 출력하고 끝낸다. 5)소스 구현 1차 소스 #include <stdio.h> #include <stdlib.h> typedef struct Node { char name; struct Node *next; struct Node *prev; }Node; typedef struct rst { int indgree; int outdgree; }rst; Node **graph; void InitGraph(int n); Node *AppendNode(Node *Target, Node *aNode); int DeleteNode(Node *Target); void UnInitGraph(int n);
  • 6. void main() { int n, i, j, x; char name[3]; Node *Now, temp; rst *result; temp.name = 0; temp.next = NULL; temp.prev = NULL; printf("노드의 개수를 입력해주세요.n"); scanf("%d",&n); result = (rst *)malloc(n*sizeof(rst)); for( i = 0 ; i < n ; i++ ) { result[i].indgree = 0; result[i].outdgree = 0; } InitGraph(n); printf("Edge를 입력해주세요.(예, AB, BD)n입력을 끝내시려면 0을 입력하세요.n"); while(1) { scanf("%s",name); if( name[0] == '0' ) break; for( x = 0 ; x < n ; x++ ) { Now = graph[x]; if( graph[x]->name == name[0] ) { temp.name = name[1]; AppendNode(Now,&temp); result[x].outdgree++; break; } else if( graph[x]->next == NULL ) { graph[x]->name = name[0]; temp.name = name[1]; AppendNode(Now,&temp); result[x].outdgree++; break; } } } for( i = 0, j = 0 ; ; j++ ) {
  • 7. if( j >= n ) { j = -1; i++; if( i >= n ) break; continue; } Now = graph[j]->next; while(1) { if( Now == NULL ) break; if( graph[i]->name == Now->name ) result[i].indgree++; Now = Now->next; } } for( i = 0 ; i < n ; i++ ) { printf("%c %d %d ", graph[i]->name, result[i].indgree, result[i].outdgree); if( result[i].indgree == 0 ) printf("Source"); else if( result[i].outdgree == 0 ) printf("Sink"); printf("n"); } free(result); UnInitGraph(n); printf("n계속하시려면 아무키나 누르세요...."); getch(); system("PAUSE"); } void InitGraph(int n) { int i; graph = (Node **)malloc(n*sizeof(Node *)); for( i = 0 ; i < n ; i++ ) { graph[i] = (Node *)malloc(sizeof(Node)); graph[i]->next = NULL; graph[i]->prev = NULL; }
  • 8. } Node *AppendNode(Node *Target, Node *aNode) { Node *New; Node *Right; Node *tail; New = (Node *)malloc(sizeof(Node)); for(tail=Target;tail->next;tail=tail->next); *New = *aNode; Right = tail->next; New->next = Right; New->prev = tail; tail->next = New; if (Right) { Right->prev = New; } return New; } int DeleteNode(Node *Target) { Node *Left, *Right; if( Target == NULL ) return 0; Left = Target->prev; Right = Target->next; Left->next = Right; if (Right) Right->prev = Left; free(Target); return 1; } void UnInitGraph(int n) { int i; for( i = 0 ; i < n ; i++ ) while(DeleteNode(graph[i]->next)); free(graph); graph = NULL; }
  • 9. 1차 소스 문제점 - 입력하여 출력할 때 Sink가 되면 제대로 출력되지 않았다. for( i = 0 ; i < n ; i++ ) { printf("%c %d %d ", graph[i]->name, result[i].indgree, result[i].outdgree); if( result[i].indgree == 0 ) printf("Source"); else if( result[i].outdgree == 0 ) printf("Sink"); printf("n"); } 최종소스 #include <stdio.h> #include <stdlib.h> typedef struct Node { char name; struct Node *next; struct Node *prev; }Node; typedef struct rst { int indgree; int outdgree; }rst; Node **graph; void InitGraph(int n); Node *AppendNode(Node *Target, Node *aNode); int DeleteNode(Node *Target); void UnInitGraph(int n); void main() { int n, i, j, x; char name[3];
  • 10. Node *Now, temp; rst *result; temp.name = 0; temp.next = NULL; temp.prev = NULL; printf("노드의 개수를 입력해주세요.n"); scanf("%d",&n); result = (rst *)malloc(n*sizeof(rst)); for( i = 0 ; i < n ; i++ ) { result[i].indgree = 0; result[i].outdgree = 0; } InitGraph(n); printf("Edge를 입력해주세요.(예, AB, BD)n입력을 끝내시려면 0 을 입력하세요.n"); while(1) { scanf("%s",name); if( name[0] == '0' ) break; for( x = 0 ; x < n ; x++ ) { Now = graph[x]; if( graph[x]->name == name[0] ) { temp.name = name[1]; AppendNode(Now,&temp); result[x].outdgree++; break; } else if( graph[x]->next == NULL ) { graph[x]->name = name[0]; temp.name = name[1]; AppendNode(Now,&temp); result[x].outdgree++; break; } } for( x = 0 ; x < n ; x++ ) { Now = graph[x]; if( graph[x]->name == name[1] ) { break; }
  • 11. else if( graph[x]->next == NULL ) { graph[x]->name = name[1]; temp.name = '0'; AppendNode(Now,&temp); break; } } } for( i = 0, j = 0 ; ; j++ ) { if( j >= n ) { j = -1; i++; if( i >= n ) break; continue; } Now = graph[j]->next; while(1) { if( Now == NULL ) break; if( graph[i]->name == Now->name ) result[i].indgree++; Now = Now->next; } } for( i = 0 ; i < n ; i++ ) { printf("%c %d %d ",graph[i]->name,result[i].indgree,result[i].outdgree); if( result[i].indgree == 0 ) printf("Source "); if( result[i].outdgree == 0 ) printf("Sinkn"); else printf("n"); } free(result); UnInitGraph(n); printf("n계속하시려면 아무키나 누르세요...."); getch(); }
  • 12. void InitGraph(int n) { int i; graph = (Node **)malloc(n*sizeof(Node *)); for( i = 0 ; i < n ; i++ ) { graph[i] = (Node *)malloc(sizeof(Node)); graph[i]->next = NULL; graph[i]->prev = NULL; } } Node *AppendNode(Node *Target, Node *aNode) { Node *New; Node *Right; Node *tail; New = (Node *)malloc(sizeof(Node)); for(tail=Target;tail->next;tail=tail->next); *New = *aNode; Right = tail->next; New->next = Right; New->prev = tail; tail->next = New; if (Right) { Right->prev = New; } return New; } int DeleteNode(Node *Target) { Node *Left, *Right; if( Target == NULL ) return 0; Left = Target->prev; Right = Target->next; Left->next = Right; if (Right) Right->prev = Left; free(Target); return 1;
  • 13. } void UnInitGraph(int n) { int i; for( i = 0 ; i < n ; i++ ) while(DeleteNode(graph[i]->next)); free(graph); graph = NULL; } 코딩결과