C++ 136

C++ Fundamentals of Data Structures(C++ 자료구조론) 6.5 TopologicalOrder(위상정렬) 예제

/*AOV Topological, 위상정렬*/#include #include #include using namespace std; class Graph{ int vertices; //정점의 개수 list *adj; void topologicalSortUtil(int v, bool visited[], stack &Stack)//위상정렬에서 쓰이는 함수 { visited[v] = true; //현재 노드를 방문했다고 표시 //정점에 연결되어있는 모든 정점에 대해 재귀 list::iterator i; for (i = adj[v].begin(); i != adj[v].end(); i++) if (!visited[*i]) topologicalSortUtil(*i, visited, Stack); Stack.push..

C++로 쉽게 풀어쓴 자료구조 프로그래밍 프로젝트 6

[1번] [listNode.h]/*단순 연결리스트를 위한 Node 클래스프로그램 6.5와 6.6에서 구현한 연결 리스트를 이용한 리스트 클래스를 화장하여다음의 연산들을 추가하고 동작을 확인하라(1) 연결 리스트의 모든 노드의 순서를 반대로 바꾸는 연산을 구혆하라(2) 다른 리스트 객체 that의 노드 정보를 현재 리스트에 병합하는 다음 연산을 구현하라 이 연산 후 that 리스트는 공백 리스트가 되어야 한다.*/#include using namespace std; class Node{private: Node *link; //다음 노드를 가리키는 포인터 변수 int data; //노드의 데이터 필드public: Node(int val = 0) :data(val), link(NULL) { } int getD..

C++로 쉽게 풀어쓴 자료구조 6장 연습문제

[Node.h]/*1.단순 연결 리스트에 정수가 저장되어 있다. 단순 연결 리스트의 모든 데이터 값을 더한 합을 출력하는 프로그램을 작성한다2. 단순 연결리스트에 특정한 데이터 값을 갖는 노드의 개수를 계산하는 함수를 작성한다*/#include using namespace std; class Node{private: Node *link; //다음 노드를 가리키는 포인터 변수 int data; //노드의 데이터 필드public: Node(int val = 0) :data(val), link(NULL) { } int getData() { return data; } Node *getLink() { return link; } void setLink(Node *next) { link = next; } void d..

C++로 쉽게 풀어쓴 자료구조 프로그래밍 프로젝트 5

[Location2D.h]/*미로 탐색을 위한 2차원 좌표 클래스*/struct Location2D{ int row; //현재 위치의 행 번호 int col; //현재 위치의 열 벊호 Location2D(int r = 0, int c = 0) { row = r; col = c; } //위치 p가 자신의 이웃인지 검사하는 함수 bool isNeighbor(Location2D &p) { return ((row == p.row && (col == p.col - 1 || col == p.col + 1)) || (col == p.col && (row == p.row - 1 || row == p.row + 1))); } //위치 p가 자신과 같은 위치인지를 검사하는 함수(연산자 오버로딩 사용) bool operat..

C++로 쉽게 풀어쓴 자료구조 프로그래밍 프로젝트 4

[문제 1, 2] CircularStudent.h/*가장 간단한 방법의 미팅 주선 프로그램을 만들려고 한다.남학생과 여학생의 큐를 각각 만들어 학생이 등록하면 큐에 넣는다.가장 먼저 등록한 남학생과 여학생을 뽑아 미팅을 주선하는 일을 반복한다.만약 여학생 큐가 비었거나 남학생 큐가 비었으면 한쪽 큐에 있는 학생들은 기다려야 한다. 조건(1) 등록한 학생 정보를 수동으로 입력하도록 사용자 인터페이스를 완성하라(2) 원형 큐를 사용하여 남학생 큐와 여학생 큐를 구현해보라*/#include #include #include using namespace std; #define MAX_QUEUE_SIZE 100 inline void error(char *str){ cout > gender; if (gender ==..

C++ Fundamentals of Data Structures(C++ 자료구조론) 6.4 Shortest Paths(최단 거리) 예제

#include #include // printf의 %-10s 같이 동일한 간격 출력 위해#include #include using namespace std; class MatrixWDigraph{private: string cityName[8]; int length[8][8]; //인접 도시와의 비용 int dist[8]; //시작 정점에서의 최단 경로 bool s[8]; //최단 경로를 찾으면 s[i]는 true //갱신된 정점의 비용 출력 void displayValues(int u) { char before; cout

C++ Fundamentals of Data Structures(C++ 자료구조론) 6.3 Prim's Algorithm 예제

/*Prim's Algorithm, 프림 알고리즘*/#include using namespace std; class Prim{private: typedef struct _graph { int v1; //정점 1 int v2; //정점 2 int weight; //가중치 }Graph; Graph g[20]; int total_edges, total_vertexes; //전체 간선, 정점 int visited[20]; //방문했는가 int temp[20]; //임시로 g[i].weight를 저장하여 min을 찾게 도와주는 배열public: Prim() { //Default } void Initialize() { cout > g[i].v1 >> g[i].v2; cout > g[i].weight; } for (..