3월, 2023의 게시물 표시

C++ 예제 (이진탐색트리, BST)

이미지
개요 안녕하세요. C++ 자료구조 (Data Structure) 중 검색에 특화된 BST ( B inary S earch T ree)를 구현한 예제입니다. Root 기준으로 작은 값은 왼쪽, 큰값은 오른쪽, 중복허용X 연결리스트 (Linked List) 로 구성 삽입, 순회, 검색, 삭제 가능 노드 삭제 시 자식노드의 수 (0, 1, 2) 에 따라 삭제법이 다름에 유의   Visualization Link : https://www.cs.usfca.edu/~galles/visualization/Algorithms.html   개발 환경 C++17, Qt Creator 9.0.1, MinGW 11.2.0 64bit Windows 11 Pro   bst.h #ifndef BST_H #define BST_H struct Node { Node(int _v=0, Node *_L=nullptr, Node *_R=nullptr) : v(_v), L(_L), R(_R) {} int v; Node *L, *R; }; class BST { public: BST(); ~BST(); private: Node *pRoot; public: inline Node* root() {return pRoot;} void insert(int v); void preorder(Node *); void inorder(Node *p); void postorder(Node *p); Node* search(Node *p, int v); void remove(int v); private: Node* insert(Node *p, int v); void deleteNode(Node *p); Node* remove(No

C++ 예제 (Stack 자료구조)

이미지
개요 안녕하세요. C++ 자료구조 (Data Structure) 의 가장 기본인 Stack 을 구현한 예제입니다. 마지막에 들어간 값이 먼저 나오는 LIFO (Last In First Out) 구조. 배열 (Array) 로 구성 Push, Pop 이 일어날 때 배열의 값이동(X), 인덱스 (top) 이동(O) 생성자에서 동적할당하는 클래스는 복사생성자, 대입연산자를 따로 정의 ( Queue 예제를 참조 하여 Do it yourself. 아래 예제에서 생략) 개발 환경 C++17, Qt Creator 9.0.1, MinGW 11.2.0 64bit Windows 11 Pro   stack.h #ifndef STACK_H #define STACK_H const int MAX = 5; class Stack { public: Stack(int _size=MAX); ~Stack(); public: void push(int v); int pop(); void print(); inline int length() {return size;} private: bool isFull(); bool isEmpty(); private: int top, size; int *p; }; #endif // STACK_H stack.cpp #include "stack.h" #include <iostream> #include <cstring> using namespace std; Stack::Stack(int _size) : top(0), size(_size), p(nullptr) { cout << "Contructor" << '\n'; p = new int[size];

C++ 예제 (Queue 자료구조)

이미지
개요 안녕하세요. C++ 자료구조 (Data Structure) 의 가장 기본인 Queue 를 구현한 예제입니다. 먼저 들어간 값이 먼저 나오는 FIFO (First In First Out) 구조. 배열 (Array) 로 구성 Push, Pop 이 일어날 때 배열의 값이동(X), 인덱스 (head, tail) 이동(O) 생성자에서 동적할당하는 클래스는 복사생성자, 대입연산자를 따로 정의 개발 환경 C++17, Qt Creator 9.0.1, MinGW 11.2.0 64bit Windows 11 Pro   queue.h #ifndef QUEUE_H #define QUEUE_H const int MAX = 5; class Queue { public: Queue(int _size=MAX); ~Queue(); Queue(const Queue& other); Queue& operator=(const Queue& other); private: int size; int head, tail; int *p; public: void print(); void push(int v); int pop(); }; #endif // QUEUE_H queue.cpp #include "queue.h" #include <iostream> #include <cstring> using namespace std; Queue::Queue(int _size) : size(_size), head(0), tail(0), p(nullptr) { cout << "Constructor" << '\n'; p = new int[size]; memset(p

이 블로그의 인기 게시물

Qt Designer 설치하기

C++ 예제 (소켓 서버, 이미지, 파일전송)