diff --git a/src/basicConcept/dataStructure/heap/heap.kt b/src/basicConcept/dataStructure/heap/heap.kt new file mode 100644 index 0000000..36dc5cb --- /dev/null +++ b/src/basicConcept/dataStructure/heap/heap.kt @@ -0,0 +1,57 @@ +import kotlin.math.* + +class Heap(){ + val arr=mutableListOf() + + 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[c2-1]){ + val temp=arr[c1-1] + arr[c1-1]=arr[idx-1] + arr[idx-1]=temp + idx=c1 + }else if(c2-1arr[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()) +} \ No newline at end of file diff --git a/src/basicConcept/dataStructure/queue/queue.kt b/src/basicConcept/dataStructure/queue/queue.kt new file mode 100644 index 0000000..5e1dd64 --- /dev/null +++ b/src/basicConcept/dataStructure/queue/queue.kt @@ -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() + + 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()) +} \ No newline at end of file diff --git a/src/basicConcept/dataStructure/stack/stack.kt b/src/basicConcept/dataStructure/stack/stack.kt new file mode 100644 index 0000000..f5a3bfa --- /dev/null +++ b/src/basicConcept/dataStructure/stack/stack.kt @@ -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() + + 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()) + +} \ No newline at end of file