목록C, C++ /C++ (6)
배우고픈 공돌이
#include using std::cout;using std::endl; void swap(int &ra, int &rb){int tmp = ra;ra = rb;rb = tmp;} int main(){int a = 100;int b = 200; cout
list.h #ifndef LIST_H#define LIST_H typedef struct node{struct node* next;}node_t; typedef struct {node_t *ptr;int element_size;}list_t; void initList(list_t *pListi, int element_size);void cleanupList(list_t *pList);void printList(const list_t *pList,void (*print)(const void*)); void insertFirstNode(list_t *pList, const void *pData);void insertNode(list_t *pList, const void *pPrevData, const vo..
queue.h #ifndef QUEUE_H#define QUEUE_H class Queue{private:int* pArr;int rear;int front;int size;public:Queue(int size);~Queue();void push(int data);int pop();}; #endif queue.cpp #include "queue.h"#include Queue::Queue(int size){//this-> pArr = (int*) malloc(sizeof(int)*size);this->pArr = new int[size];assert( this->pArr ); this-> size = size;this-> front =0;this-> rear = 0; } Queue::~Queue(){//..
stack.h #ifndef STACK_H#define STACK_H class Stack{private://information hidingint* pArr;int tos;int size;public:Stack(int size);//void initStack(int size);~Stack();//void cleanupStack();void push(int data);int pop();}; #endif stack.cpp #include "stack.h"#include Stack::Stack(int size){//this-> pArr = (int*) malloc(sizeof(int)*size);this-> pArr = new int[size];assert( this->pArr ); this-> size =..
3. malloc - free -> new - delete void Stack::initStack(int size){//this-> pArr = (int*) malloc(sizeof(int)*size);this-> pArr = new int[size];} void Stack::cleanupStack(){//free( this->pArr );delete [] this->pArr;} 4. init, close function -> using constructor, destructor -main.cpp int main(){ //initStack(&s1,10);initStack(&s2,100);s1.Stack(10); s2.Stack(100); //cleanupStack(&s1); cleanupStack(&s2..
c로된 코드를 c++로 변경하기 위해 8단계 정도로 나눠볼 수 있다. 1. .c -> .cpp gcc 컴파일러를 이용하여 .c파일을 컴파일 했다면, .c파일을 .cpp파일로 확장자 변경하고 g++ 컴파일러를 사용하면 컴파일 된다. 2. 함수 -> 구조체변수(객체)의 멤버함수로 변경 아래의 c코드를 예를 들어 변경한다. -main.c #include #include "stack.h" int main(void){stack_t s1, s2; initStack(&s1,10);initStack(&s2,100); push(&s1,100); push(&s1,200);push(&s1,300);push(&s2,900);push(&s2,800);push(&s2,700);printf("s1 1st pop : %d \n",p..