자료구조

자료구조/by swift

swift PriorityQueue 구현

swift로 우선순위 큐를 구현했습니다. /* base on : Array, binary Tree */ class PriorityQueue{ private var elements: [T] = [] private let compare: (T, T) -> Bool var top: T?{elements.first} var isEmpty: Bool{elements.isEmpty} var count: Int{elements.count} init(){ compare = {$0 Bool){ self.compare = compare // 정렬 기준을 받아서 덮어쓰기 } func pus..

자료구조/by swift

swift Double-LinkedList 구현

https://en.wikipedia.org/wiki/Doubly_linked_list Doubly linked list - Wikipedia From Wikipedia, the free encyclopedia Linked list data structure In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains three fields: two link fields (refe en.wikipedia.org 위키피디아에서 어떤 메소드를 갖고 있어야 하며, 어떤 프로퍼티가 있는지만 확인하..

자료구조/by swift

swift queue 구현

저번에 이어서 이번에는 queue 구현 코드이다. class Queue{ private var backStack = Stack() private var frontStack = Stack() var isEmpty : Bool{ return backStack.isEmpty && frontStack.isEmpty } var front : Any?{ guard backStack.isEmpty || frontStack.isEmpty else {return nil} if !frontStack.isEmpty {return frontStack.top} while !backStack.isEmpty { if let top = backStack.pop() { frontStack.push(top) } } return fron..

자료구조/by swift

swift stack 구현

swift로 stack을 구현해봤습니다. struct Stack{ private var elements: [T] = [] mutating func push(_ element : T){ elements.append(element) } mutating func pop() -> T?{ return elements.popLast() ?? nil } var top: T?{ return elements.last ?? nil } var isEmpty : Bool{ return elements.isEmpty } } 위와 같이 구현해봤는데.. 흠 좋은 거 같기도 ㅎ고 아닌 거 같기도 하고 ..

자료구조/by C

c언어 stack 구현

내 입맛대로 stack 구현하기 1. empty 2. push 3. pop 4. size push를 할 때 자신의 입맛대로 매개변수 만들어서 넣으시면 됩니다. node에는 해당 문제에 따라서 저장할 값을 추가적으로 넣어주시면 됩니다. #include #include typedef long long ll; typedef struct node{ char value; int cnt;//몇 개 쌓아왔는지 struct node* pre; }node; typedef struct stack{ node *top; ll size; }stack; ll size(stack* s){ return s->size; } node* top(stack*s){ return s->top; } bool empty(stack*s){ retu..

자료구조/by C

c언어 double linked list 구현

흠.. 일단 그냥 막 내가 쓰고 싶은 대로 구현하긴 했다. prev, next를 통해서 전과 후로 갈 수 있고 insert,delete가 O(1)에 해결 가능하다. insert는 여러 버전이 있는데, index에 추가하는 것과, 가리키고 있는 node에 insert하는 것과, 제일 앞에 insert, 그리고 제일 뒤에 insert을 넣는 버전이 있다. #include typedef long long ll; typedef struct node{ char value;//값 struct node* prev;//전 struct node* next;//다음 }node; typedef struct Linkedlist{ node* front;//맨 앞 node* back;//맨 뒤 ll size;//크기 }Linke..

자료구조/by C

c언어 vector 구현

c++ vector를 c언어에서도 쓰고 싶어서 간단하게 만들어 봤습니다. #include typedef long long ll; typedef struct node{//해당하는 값을 arr로 갖는다. int v; }node; typedef struct vector{//인덱스 접근 가능해야 하며, size 존재, back, pop_back, node* arr;//값을 저장하는 곳 ll size;//크기 ll capacity;//전체 크기 }vector; void init(vector *q){ q->arr=(node *)malloc(sizeof(node)*(64)); q->size=0; q->capacity=64;//초기 크기 64개 } void push_back(vector *q,int v){//v가 추가..

자료구조/by C

c언어 queue 구현

나만의 queue 구현 코드 #include #define ll long long typedef struct newtype{ int v;//자료형 새로 추가하면 된다. }newtype; typedef struct node{ struct newtype x;// struct node* next;//다음 노드 가리키기 }node; typedef struct queue{//front,pop,size,empty,push node* front;//처음에 연결 node* back;//마지막에 바로 연결 ll size; }queue; void init(queue* q){ q->front=NULL; q->back=NULL; q->size=0; } void push(queue *q,newtype v){//qush하려면 n..

견우직녀달
'자료구조' 카테고리의 글 목록