Skip to content

Commit

Permalink
study: stack,queue,heap
Browse files Browse the repository at this point in the history
# id: 문제 id를 숫자로 작성
# categories : 해당 문제의 유형을 ,로 구분하여 작성
# tags : 해당 문제의 태그를 ,로 구분하여 작성
# time : 해당 문제 풀이에 걸린 시간을 분단위 숫자로 작성
# try : 해당 문제에 몇번의 시도를 했는지 숫자로 작성
# help: 해당 문제에 외부의 도움을 받았는지 true/false로 작성
# url : 해당 문제의 url을 작성
id:
categories: []
tags: []
time:
try:
help: false
url:
  • Loading branch information
gogumaC committed Sep 4, 2024
1 parent 0899766 commit 6268fed
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/basicConcept/dataStructure/heap/heap.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import kotlin.math.*

class Heap(){
val arr=mutableListOf<Int>()

fun push(e:Int){
arr.add(e)
var cIndex=arr.size
if(arr.size>1){
while(cIndex!=1){
val parent=cIndex/2
if(arr[parent-1]<arr[cIndex-1]){
val temp=arr[parent-1]
arr[parent-1]=e
arr[cIndex-1]=temp
cIndex=parent
}else break
}
}
}

fun pop():Int?{
if(arr.isEmpty()) return null
val res=arr[0]
val leaf=arr.removeLast()
if(arr.isEmpty()) return res
arr[0]=leaf
var idx=1
while(arr.isNotEmpty() && idx<arr.size){
val c1=2.0.pow(idx).toInt()
val c2=c1+1
if(c1-1<arr.size && arr[idx-1]<arr[c1-1] && arr[c1-1]>arr[c2-1]){
val temp=arr[c1-1]
arr[c1-1]=arr[idx-1]
arr[idx-1]=temp
idx=c1
}else if(c2-1<arr.size && arr[idx-1]<arr[c2-1] && arr[c2-1]>arr[c1-1]){
val temp=arr[c2-1]
arr[c2-1]=arr[idx-1]
arr[idx-1]=temp
idx=c2
}else break
}
return res
}
}

fun main(){

val h=Heap()
h.push(2)
h.push(1)
h.push(3)
h.push(1)
println(h.arr)
println(h.pop())
}
50 changes: 50 additions & 0 deletions src/basicConcept/dataStructure/queue/queue.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.util.*

class ArrayQueue(val size:Int){
val arr=IntArray(size)
var front=0
var rear=0
var qSize=0
fun enqueue(e:Int){
if(qSize==size){
println("queue is full")
return
}
arr[rear]=e
qSize++
rear=(rear+1)%size
}

fun dequeue():Int?{
if(qSize==0) return null
val e=arr[front]
qSize--
front=(front+1)%size
return e

}
}

class LinkedListQueue(){
val arr=LinkedList<Int>()

fun enqueue(e:Int){
arr.add(e)
}

fun dequeue():Int?{
if(arr.isEmpty())return null
return arr.removeLast()
}
}

fun main() {
val q=LinkedListQueue()
q.enqueue(0)
q.enqueue(1)
q.enqueue(2)
println(q.dequeue())
println(q.dequeue())
println(q.dequeue())
println(q.dequeue())
}
93 changes: 93 additions & 0 deletions src/basicConcept/dataStructure/stack/stack.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import java.util.*

class ArrayStack(size:Int){
val arr=IntArray(size)
var top=-1

fun push(e:Int){
if(top==arr.size-1){
println("stack is full")
return
}

top++
arr[top]=e
}

fun pop():Int?{
if(top==-1){
println("stack is empty")
return null
}
val e=arr[top]
top--
return e
}
}

class DynamicArrayStack(){

var size=2
var arr=IntArray(size)
var top=-1

fun push(e:Int){
if(top==arr.size-1){
swapBigger()
}

top++
arr[top]=e
}

fun swapBigger(){
val newArr=IntArray(size*2)
size*=2
for(i in arr.indices){
newArr[i]=arr[i]
}

arr=newArr
}



fun pop():Int?{
if(top==-1){
println("stack is empty")
return null
}
val e=arr[top]
top--
return e
}

}

class LinkedListStack(){

val arr=LinkedList<Int>()

fun push(e:Int){
arr.add(e)
}

fun pop():Int?{
if(arr.isEmpty()) return null
return arr.removeLast()
}

}

fun main() {
val stack=DynamicArrayStack()

stack.push(1)
stack.push(2)
stack.push(3)
println(stack.pop())
println(stack.pop())
println(stack.pop())
println(stack.pop())

}

0 comments on commit 6268fed

Please sign in to comment.