├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── appveyor.yml ├── array-minimum-distance ├── README.md ├── array-minimum-distance.go └── array-minimum-distance_test.go ├── array-rotation ├── README.md ├── array-rotation.go └── array-rotation_test.go ├── array-smallest-missing-number ├── README.md ├── array-smallest-missing-number.go └── array-smallest-missing-number_test.go ├── binary-search-tree-1-insertion ├── binary-search-tree-1-insertion.go └── binary-search-tree-1-insertion_test.go ├── binary-tree-1-introduction ├── binary-tree-1-introduction.go └── binary-tree-1-introduction_test.go ├── binary-tree-2-traversals-in-pre-post-order └── binary-tree-traversals-2-in-pre-post-order.go ├── binary-tree-2-traversals-level-order └── binary-tree-traversals-2-level-order.go ├── binary-tree-3-doubly-linked-list └── binary-tree-3-doubly-linked-list.go ├── binary-tree-4-delete └── binary-tree-4-delete.go ├── binary-tree-5-find-min-max └── binary-tree-5-find-min-max.go ├── coverage.txt ├── linked-list-1-introduction ├── README.md ├── linked-list-1-introduction.go └── resources │ └── linked-list.png ├── linked-list-2-inserting-a-node ├── README.md ├── linked-list-2-inserting-a-node.go └── resources │ ├── inserting-a-node-1.png │ ├── inserting-a-node-2.png │ └── inserting-a-node-3.png ├── linked-list-3-deleting-a-node ├── README.md ├── linked-list-3-deleting-a-node.go └── resources │ └── deleting-a-node.png ├── linked-list-circular-1-introduction └── README.md ├── linked-list-circular-2-traversal ├── README.md └── linked-list-circular-2-traversal.go ├── linked-list-circular-singly-1-insertion ├── README.md └── linked-list-circular-singly-1-insertion.go ├── linked-list-find-length ├── README.md ├── linked-list-find-length.go └── resources │ └── find-length.png ├── linked-list-merge-two-sorted ├── README.md ├── linked-list-merge-two-sorted.go └── linked-list-merge-two-sorted.go_test.go ├── linked-list-reverse ├── README.md ├── linked-list-reverse.go └── linked-list-reverse_test.go ├── merge-sort ├── README.md ├── merge-sort.go ├── merge-sort_test.go └── resources │ └── merge-sort.png ├── quick-sort ├── README.md ├── quick-sort.go ├── quick-sort_test.go └── resources │ └── quick-sort.png └── stack ├── stack.go └── stack_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.dll 4 | *.so 5 | *.dylib 6 | 7 | # Test binary, build with `go test -c` 8 | *.test 9 | 10 | # Output of the go coverage tool, specifically when used with LiteIDE 11 | *.out 12 | 13 | # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 14 | .glide/ 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | go: 3 | - 1.10.x 4 | - 1.11.x 5 | script: 6 | - go build -v ./... 7 | - go test -v -cover -race ./... 8 | after_success: 9 | - bash <(curl -s https://codecov.io/bash) 10 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/17ec0461bcf3eaa1152df8c178d5d751882c485d/CONTRIBUTING.md -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Furkan Türkal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Data Structures with Go

2 | 3 | [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php) 4 | [![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.png?v=103)](https://github.com/ellerbrock/open-source-badges/) 5 | [![Build status](https://ci.appveyor.com/api/projects/status/jv28205majdbvvj0?svg=true)](https://ci.appveyor.com/project/Dentrax/data-structures-with-go) 6 | [![Build Status](https://travis-ci.org/Dentrax/Data-Structures-with-Go.svg?branch=master)](https://travis-ci.org/Dentrax/Data-Structures-with-Go) 7 | [![codecov](https://codecov.io/gh/Dentrax/Data-Structures-with-Go/branch/master/graph/badge.svg)](https://codecov.io/gh/Dentrax/Data-Structures-with-Go) 8 | [![Go Report Card](https://goreportcard.com/badge/github.com/Dentrax/Data-Structures-with-Go)](https://goreportcard.com/report/github.com/Dentrax/Data-Structures-with-Go) 9 | [![Sourcegraph](https://img.shields.io/badge/view%20on-Sourcegraph-brightgreen.svg)](https://sourcegraph.com/github.com/Dentrax/Data-Structures-with-Go) 10 | 11 | Click here for **[GO Language](https://golang.org/)** 12 | 13 | Click here for **[Guide & Theory](https://goo.gl/Ej9kzs)** 14 | 15 | Click here for **[VSCode IDE](https://code.visualstudio.com/)** 16 | 17 | [What It Is](#what-it-is) 18 | 19 | [How To Use](#how-to-use) 20 | 21 | [About](#about) 22 | 23 | [Collaborators](#collaborators) 24 | 25 | [Branches](#branches) 26 | 27 | [Copyright & Licensing](#copyright--licensing) 28 | 29 | [Contributing](#contributing) 30 | 31 | [Contact](#contact) 32 | 33 | ## What It Is 34 | 35 | **Data Structures with Go** 36 | 37 | Data-Structures-with-Go guide for GO language is an easy and advanced way to learn Data Structures. 38 | 39 | **Uses : `GO Language`** -> **[Official GO Language Web Site](https://golang.org/)** 40 | 41 | ## How To Use 42 | 43 | Just research the **[main repository](https://github.com/Dentrax/Data-Structures-with-Go)** and learn it gradually. Thats all. 44 | 45 | ## About 46 | 47 | Data-Structures-with-Go was created to serve three purposes: 48 | 49 | **Data-Structures-with-Go is a basically Data-Structures learning repository which all structures coded in Go language** 50 | 51 | 1. To act as a guide to learn basic Data Structures with enhanced and rich content. 52 | 53 | 2. To provide a simplest and easiest way to learn. 54 | 55 | 3. There is no any Data Structures guide coded in Go language on the internet. 56 | 57 | ## Collaborators 58 | 59 | **Project Manager** - Furkan Türkal (GitHub: **[dentrax](https://github.com/dentrax)**) 60 | 61 | ## Branches 62 | 63 | We publish source for the **[Data-Structures-with-Go]** in single rolling branch: 64 | 65 | The **[master branch](https://github.com/dentrax/Data-Structures-with-Go/tree/master)** is extensively tested by our QA team and makes a great starting point for learning the GO language. Also tracks [live changes](https://github.com/dentrax/Data-Structures-with-Go/commits/master) by our team. 66 | 67 | ## Copyright & Licensing 68 | 69 | The base project code is copyrighted by Furkan 'Dentrax' Türkal and is covered by single licence. 70 | 71 | All program code (i.e. Go) is licensed under MIT License unless otherwise specified. Please see the **[LICENSE.md](https://github.com/Dentrax/Data-Structures-with-Go/blob/master/LICENSE)** file for more information. 72 | 73 | **References** 74 | 75 | While this repository is being prepared, it may have been quoted from some sources. 76 | If there is an unspecified source, please contact me. 77 | 78 | ## Contributing 79 | 80 | Please check the [CONTRIBUTING.md](CONTRIBUTING.md) file for contribution instructions and naming guidelines. 81 | 82 | ## Contact 83 | 84 | Data-Structures-with-Go was created by Furkan 'Dentrax' Türkal 85 | 86 | * 87 | 88 | You can contact by URL: 89 | **[CONTACT](https://github.com/dentrax)** 90 | 91 | Best Regards 92 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: "{build}" 2 | skip_tags: true 3 | clone_depth: 1 4 | 5 | environment: 6 | GOVERSION: 1.11 7 | 8 | build: false 9 | deploy: false 10 | 11 | install: 12 | - go build -v ./... 13 | -------------------------------------------------------------------------------- /array-minimum-distance/README.md: -------------------------------------------------------------------------------- 1 |

Array Minimum Distance Source

2 | 3 | [What It Is](#what-it-is) 4 | 5 | ## What It Is 6 | 7 | * Given an unsorted array `arr[]` and two numbers x and y, find the minimum distance between `x` and `y` in `arr[]`. The array might also contain duplicates. You may assume that both `x` and `y` are different and present in `arr[]`. 8 | 9 | Examples 10 | -------------------------- 11 | 12 | > * Input: arr[] = {1, 2}, x = 1, y = 2 13 | > * Output: Minimum distance between 1 and 2 is 1. 14 | 15 | > * Input: arr[] = {3, 4, 5}, x = 3, y = 5 16 | > * Output: Minimum distance between 3 and 5 is 2. 17 | 18 | > * Input: arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}, x = 3, y = 6 19 | > * Output: Minimum distance between 3 and 6 is 4. 20 | 21 | > * Input: arr[] = {2, 5, 3, 5, 4, 4, 2, 3}, x = 3, y = 2 22 | > * Output: Minimum distance between 3 and 2 is 1. 23 | 24 | METHOD 1 (Simple) 25 | -------------------------- 26 | 27 | Use two loops: The outer loop picks all the elements of arr[] one by one. The inner loop picks all the elements after the element picked by outer loop. If the elements picked by outer and inner loops have same values as x or y then if needed update the minimum distance calculated so far. 28 | 29 | **Algorithm Complexity** 30 | 31 | | Complexity | Notation | 32 | | ----------------- |:---------:| 33 | | `Time Complexity` | `O(n^2)` | -------------------------------------------------------------------------------- /array-minimum-distance/array-minimum-distance.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | const MaxUint = ^uint(0) 13 | const MinUint = 0 14 | const MaxInt = int(MaxUint >> 1) 15 | const MinInt = -MaxInt - 1 16 | 17 | func Abs(x int) int { 18 | if x < 0 { 19 | return -x 20 | } 21 | if x == 0 { 22 | return 0 23 | } 24 | return x 25 | } 26 | 27 | func minDist (arr []int, n, x, y int) int { 28 | var min_dist int = MaxInt 29 | 30 | for i := 0; i < n; i++ { 31 | for j := i + 1; j < n; j++ { 32 | if((x == arr[i] && y == arr[j] || y == arr[i] && x == arr[j]) && min_dist > Abs(i - j)) { 33 | min_dist = Abs(i - j) 34 | } 35 | } 36 | } 37 | 38 | return min_dist 39 | } 40 | 41 | func main() { 42 | arr := []int{3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3} 43 | var n int = len(arr) 44 | var x int = 3 45 | var y int = 6 46 | fmt.Printf("Minimum distance between %d and %d is %d", x, y, minDist(arr, n, x, y)) 47 | } -------------------------------------------------------------------------------- /array-minimum-distance/array-minimum-distance_test.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import ( 11 | "reflect" 12 | "testing" 13 | ) 14 | 15 | func TestAbs(t *testing.T) { 16 | var testDatas = []struct { 17 | Input int 18 | Output int 19 | }{ 20 | {1, 1}, 21 | {7, 7}, 22 | {-1, 1}, 23 | {-7, 7}, 24 | {0, 0}, 25 | } 26 | for _, data := range testDatas { 27 | expected := data.Output 28 | actual := Abs(data.Input) 29 | 30 | if !reflect.DeepEqual(expected, actual) { 31 | t.Errorf("Abs: Expected: %d, Actual: %d", expected, actual) 32 | } 33 | 34 | } 35 | } 36 | 37 | func TestMinDist(t *testing.T) { 38 | var testDatas = []struct { 39 | Array []int 40 | Num1 int 41 | Num2 int 42 | Distance int 43 | }{ 44 | {[]int{3, 4, 5, 6}, 3, 6, 3}, 45 | {[]int{1, 2, 3}, 1, 3, 2}, 46 | {[]int{9, 7, 5, 3, 1}, 1, 7, 3}, 47 | {[]int{-1, -7, 5, 5, 1, 5}, -1, 5, 2}, 48 | {[]int{-1, -7, 5, 5, -7, -1}, -1, -7, 1}, 49 | } 50 | for _, data := range testDatas { 51 | expected := data.Distance 52 | actual := minDist(data.Array, len(data.Array), data.Num1, data.Num2) 53 | 54 | if !reflect.DeepEqual(expected, actual) { 55 | t.Errorf("MinDist: Expected: %d, Actual: %d", expected, actual) 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /array-rotation/README.md: -------------------------------------------------------------------------------- 1 |

Array Rotation Source

2 | 3 | [What It Is](#what-it-is) 4 | 5 | ## What It Is 6 | 7 | > * Input: Write a function `rotate(ar[], d, n)` that rotates `arr[]` of size `n` by `d` elements 8 | > * Output: `1 2 3 4 5 6 7` 9 | 10 | > * Input: Rotation of the above array by `2` will make array 11 | > * Output: `3 4 5 6 7 1 2` 12 | 13 | METHOD 1 (Use temp array) 14 | -------------------------- 15 | 16 | Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 17 | 18 | * 1) Store `d` elements in a temp array 19 | temp[] = [1, 2] 20 | * 2) Shift rest of the `arr[]` 21 | arr[] = [3, 4, 5, 6, 7, 6, 7] 22 | * 3) Store back the `d` elements 23 | arr[] = [3, 4, 5, 6, 7, 1, 2] 24 | 25 | **Algorithm Complexity** 26 | 27 | | Complexity | Notation | 28 | | ----------------- |:---------:| 29 | | `Time Complexity` | `O(n)` | 30 | | `Auxiliary Space` | `O(d)` | 31 | 32 | 33 | METHOD 2 (Rotate one by one) 34 | -------------------------- 35 | 36 | ```go 37 | leftRotate(arr[], d, n) 38 | start 39 | For i = 0 to i < d 40 | Left rotate all elements of arr[] by one 41 | end 42 | ``` 43 | 44 | To rotate by one, store arr[0] in a temporary variable temp, move arr[1] to arr[0], arr[2] to arr[1] …and finally temp to arr[n-1] 45 | Let us take the same example arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2 46 | Rotate arr[] by one 2 times 47 | We get `[2, 3, 4, 5, 6, 7, 1]` after first rotation and `[3, 4, 5, 6, 7, 1, 2]` after second rotation. 48 | 49 | **Algorithm Complexity** 50 | 51 | | Complexity | Notation | 52 | | ----------------- |:---------:| 53 | | `Time Complexity` | `O(n*d)` | 54 | | `Auxiliary Space` | `O(1)` | -------------------------------------------------------------------------------- /array-rotation/array-rotation.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | func leftRotate(arr []int, d int, n int){ 13 | for i := 0; i < d; i++ { 14 | leftRotateByOne(arr, n) 15 | } 16 | } 17 | 18 | func leftRotateByOne(arr []int, n int){ 19 | var i, temp int 20 | temp = arr[0] 21 | for i = 0; i < n - 1; i++ { 22 | arr[i] = arr[i + 1] 23 | } 24 | arr[i] = temp 25 | } 26 | 27 | func printArray(arr []int, size int){ 28 | if(len(arr) < size){ 29 | fmt.Println("[ArrayRotation::printArray()] -> Index out of range. Max : ", len(arr)) 30 | return; 31 | } 32 | for i := 0; i < size; i++ { 33 | fmt.Print(arr[i]) 34 | } 35 | } 36 | 37 | func main() { 38 | arr := []int{1, 2, 3, 4, 5, 6, 7} 39 | fmt.Println("-INPUT-") 40 | printArray(arr, len(arr) - 1) 41 | leftRotate(arr, 2 , 7) 42 | fmt.Println() 43 | fmt.Println("-OUTPUT-") 44 | printArray(arr, 7) 45 | } 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /array-rotation/array-rotation_test.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import ( 11 | "reflect" 12 | "testing" 13 | ) 14 | 15 | func TestLeftRotate(t *testing.T) { 16 | var testDatas = []struct { 17 | ArrayIn []int 18 | Count int 19 | Depth int 20 | ArrayOut []int 21 | }{ 22 | {[]int{1, 2, 3, 4, 5, 6, 7}, 2, 7, []int{3, 4, 5, 6, 7, 1, 2}}, 23 | {[]int{1, 2, 3, 4, 5, 6, 7}, 2, 6, []int{3, 4, 5, 6, 1, 2, 7}}, 24 | {[]int{1, 2, 3, 4, 5, 6, 7}, 1, 2, []int{2, 1, 3, 4, 5, 6, 7}}, 25 | {[]int{1, 2, 3, 4, 5, 6, 7}, 7, 7, []int{1, 2, 3, 4, 5, 6, 7}}, 26 | {[]int{1, 2, 3, 4, 5, 6, 7}, 7, 6, []int{2, 3, 4, 5, 6, 1, 7}}, 27 | } 28 | for _, data := range testDatas { 29 | expected := data.ArrayOut 30 | leftRotate(data.ArrayIn, data.Count, data.Depth) 31 | actual := data.ArrayIn 32 | 33 | if !reflect.DeepEqual(expected, actual) { 34 | t.Errorf("LeftRotate: Expected: %d, Actual: %d", expected, actual) 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /array-smallest-missing-number/README.md: -------------------------------------------------------------------------------- 1 |

Array Smallest Missing Number Source

2 | 3 | [What It Is](#what-it-is) 4 | 5 | ## What It Is 6 | 7 | Given a sorted array of `n` distinct integers where each integer is in the range from `0 to m-1 and m > n`. Find the smallest number that is missing from the array. 8 | 9 | Examples 10 | -------------------------- 11 | 12 | > * Input: {0, 1, 2, 6, 9}, n = 5, m = 10 13 | > * Output: `3` 14 | 15 | > * Input: {4, 5, 10, 11}, n = 4, m = 12 16 | > * Output: `0` 17 | 18 | > * Input: {0, 1, 2, 3}, n = 4, m = 5 19 | > * Output: `4` 20 | 21 | > * Input: {0, 1, 2, 3, 4, 5, 6, 7, 10}, n = 9, m = 11 22 | > * Output: `8` 23 | 24 | METHOD 1 (Use Binary Search) 25 | -------------------------- 26 | 27 | `For i = 0 to m-1`, do binary search for i in the array. If i is not present in the array then `return i`. 28 | 29 | **Algorithm Complexity** 30 | 31 | | Complexity | Notation | 32 | | ----------------- |:------------:| 33 | | `Time Complexity` | `O(m log n)` | 34 | 35 | METHOD 2 (Linear Search) 36 | -------------------------- 37 | 38 | If `arr[0] is not 0`, `return 0`. Otherwise traverse the input array starting from index 0, and for each pair of elements a[i] and a[i+1], find the difference between them. if the difference is greater than 1 then `a[i] + 1` is the missing number. 39 | 40 | **Algorithm Complexity** 41 | 42 | | Complexity | Notation | 43 | | ----------------- |:------------:| 44 | | `Time Complexity` | `O(n)` | 45 | 46 | METHOD 3 (Use Modified Binary Search) 47 | -------------------------- 48 | 49 | In the standard Binary Search process, the element to be searched is compared with the middle element and on the basis of comparison result, we decide whether to search is over or to go to left half or right half. 50 | In this method, we modify the standard Binary Search algorithm to compare the middle element with its index and make decision on the basis of this comparison. 51 | 52 | 1) If the first element is not same as its index then return first index 53 | 2) Else get the middle index say mid 54 | a) If arr[mid] greater than mid then the required element lies in left half. 55 | b) Else the required element lies in right half. 56 | 57 | `Note: This method doesn’t work if there are duplicate elements in the array.` 58 | 59 | **Algorithm Complexity** 60 | 61 | | Complexity | Notation | 62 | | ----------------- |:------------:| 63 | | `Time Complexity` | `O(log n)` | 64 | -------------------------------------------------------------------------------- /array-smallest-missing-number/array-smallest-missing-number.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | import "os" 12 | 13 | func findFirstMissing(arr []int, start, end int) int { 14 | if start < 0 { 15 | fmt.Println("Start must be greater than 0") 16 | os.Exit(1) 17 | return 0 18 | } 19 | 20 | if start > end { 21 | return end + 1 22 | } 23 | 24 | if start != arr[start] { 25 | return start 26 | } 27 | 28 | mid := (start + end) / 2 29 | 30 | if arr[mid] == mid { 31 | return findFirstMissing(arr, mid+1, end) 32 | } 33 | 34 | return findFirstMissing(arr, start, end) 35 | } 36 | 37 | func main() { 38 | arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 10} 39 | var n int = len(arr) 40 | fmt.Printf("Smallest missing element is %d", findFirstMissing(arr, 0, n-1)) 41 | } 42 | -------------------------------------------------------------------------------- /array-smallest-missing-number/array-smallest-missing-number_test.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import ( 11 | "reflect" 12 | "testing" 13 | ) 14 | 15 | func TestFindFirstMissing(t *testing.T) { 16 | var testDatas = []struct { 17 | ArrayIn []int 18 | Start int 19 | End int 20 | Out int 21 | }{ 22 | {[]int{0, 1, 2, 4, 5}, 0, 4, 3}, 23 | {[]int{0, 1, 2, 3, 4, 6, 8, 9, 10}, 0, 5, 5}, 24 | {[]int{0, 1, 2, 3, 4, 6, 8, 9, 10}, 0, 8, 5}, 25 | {[]int{0, 1, 2, 3, 4, 5, 6, 8, 9, 10}, 0, 8, 7}, 26 | {[]int{1, 2, 3, 4, 5}, 0, 5, 0}, 27 | } 28 | for _, data := range testDatas { 29 | expected := data.Out 30 | actual := findFirstMissing(data.ArrayIn, data.Start, data.End) 31 | 32 | if !reflect.DeepEqual(expected, actual) { 33 | t.Errorf("FindFirstMissing: Expected: %d, Actual: %d", expected, actual) 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /binary-search-tree-1-insertion/binary-search-tree-1-insertion.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | left *Node 15 | right *Node 16 | } 17 | 18 | //Returns an initialized list 19 | func (n *Node) Init(data int) *Node { 20 | n.data = data 21 | n.left = nil 22 | n.right = nil 23 | return n 24 | } 25 | 26 | //Returns an new list 27 | func New(data int) *Node { 28 | return new(Node).Init(data) 29 | } 30 | 31 | //Function to search a given key in a given BST 32 | func Search(root *Node, key int) *Node { 33 | //1. Base Cases: root is null or key is present at root 34 | if root == nil || root.data == key { 35 | //fmt.Println("The given previous node cannot be NULL") 36 | return root 37 | } 38 | 39 | //2. Key is greater than root's key 40 | if root.data < key { 41 | return Search(root.right, key) 42 | } 43 | 44 | //3. Key is smaller than root's key 45 | return Search(root.left, key) 46 | } 47 | 48 | //A utility function to do inorder traversal of BST 49 | func PrintInOrder(root *Node) { 50 | if root != nil { 51 | PrintInOrder(root.left) 52 | fmt.Printf("%d \n", root.data) 53 | PrintInOrder(root.right) 54 | } 55 | } 56 | 57 | //A utility function to insert a new node with given key in BST 58 | func Insert(node *Node, key int) *Node { 59 | //1. If the tree is empty, return a new node 60 | if node == nil { 61 | return New(key) 62 | } 63 | 64 | //2. Otherwise, recur down the tree 65 | if key < node.data { 66 | node.left = Insert(node.left, key) 67 | } else if key > node.data { 68 | node.right = Insert(node.right, key) 69 | } 70 | 71 | //3. Return the (unchanged) node pointer 72 | return node 73 | } 74 | 75 | func main() { 76 | /* Let us create following BST 77 |               50 78 |            /     \ 79 |           30      70 80 |          /  \    /  \ 81 |        20   40  60   80 */ 82 | 83 | //To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node)); 84 | root := New(50) 85 | Insert(root, 30) 86 | Insert(root, 20) 87 | Insert(root, 40) 88 | Insert(root, 70) 89 | Insert(root, 60) 90 | Insert(root, 80) 91 | 92 | //Print inoder traversal of the BST 93 | PrintInOrder(root) 94 | } 95 | -------------------------------------------------------------------------------- /binary-search-tree-1-insertion/binary-search-tree-1-insertion_test.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import ( 11 | "reflect" 12 | "testing" 13 | ) 14 | 15 | func TestBinarySearchTreeNew(t *testing.T) { 16 | var testDatas = []struct { 17 | Node *Node 18 | ArrayIn []int 19 | Test int 20 | Out int 21 | }{ 22 | {New(0), []int{20, 4, 15, 85}, 15, 15}, 23 | {New(3), []int{10, 20, 30, 40, 50}, 50, 50}, 24 | {New(7), []int{7}, 7, 7}, 25 | } 26 | 27 | for _, data := range testDatas { 28 | for i := 0; i < len(data.ArrayIn); i++ { 29 | Insert(data.Node, data.ArrayIn[i]) 30 | } 31 | 32 | actual := Search(data.Node, data.Test) 33 | 34 | expected := data.Out 35 | 36 | if !reflect.DeepEqual(expected, actual.data) { 37 | t.Errorf("BinarySearchTree: Expected: %d, Actual: %d", expected, actual) 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /binary-tree-1-introduction/binary-tree-1-introduction.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | left *Node 15 | right *Node 16 | } 17 | 18 | //Returns an initialized list 19 | func (n *Node) Init(data int) *Node { 20 | n.data = data 21 | n.left = nil 22 | n.right = nil 23 | return n 24 | } 25 | 26 | //Returns an new list 27 | func New(data int) *Node { 28 | return new(Node).Init(data) 29 | } 30 | 31 | func main() { 32 | //To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node)); 33 | root := New(1) 34 | /* 35 |         1 36 |       /   \ 37 |      NULL  NULL 38 | */ 39 | 40 | root.left = New(2) 41 | root.right = New(3) 42 | /* 2 and 3 become left and right children of 1 43 |            1 44 |          /   \ 45 |         2      3 46 |      /    \    /  \ 47 |     NULL NULL NULL NULL 48 | */ 49 | 50 | root.left.left = New(4) 51 | /* 4 becomes left child of 2 52 |            1 53 |        /       \ 54 |       2          3 55 |     /   \       /  \ 56 |    4    NULL  NULL  NULL 57 |   /  \ 58 | NULL NULL 59 | */ 60 | 61 | fmt.Println("Root data : ", root.data) 62 | fmt.Println("Root->Left data : ", root.left.data) 63 | fmt.Println("Root->Right data : ", root.right.data) 64 | fmt.Println("Root->Left->Left data : ", root.left.left.data) 65 | } 66 | -------------------------------------------------------------------------------- /binary-tree-1-introduction/binary-tree-1-introduction_test.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import ( 11 | "reflect" 12 | "testing" 13 | ) 14 | 15 | func TestBinaryTreeNew(t *testing.T) { 16 | var testDatas = []struct { 17 | Node *Node 18 | Data int 19 | Left *Node 20 | Right *Node 21 | }{ 22 | {New(-1), -1, New(-1), New(-1)}, 23 | {New(0), 0, New(0), New(0)}, 24 | {New(1), 1, New(1), New(1)}, 25 | {New(7), 7, New(7), New(7)}, 26 | } 27 | 28 | for _, data := range testDatas { 29 | data.Node.right = data.Right 30 | data.Node.left = data.Left 31 | 32 | expected := data.Data 33 | 34 | actualLeft := data.Node.left.data 35 | actualRight := data.Node.right.data 36 | 37 | if !reflect.DeepEqual(expected, actualLeft) || !reflect.DeepEqual(expected, actualRight) { 38 | t.Errorf("BinaryTreeNew: Expected: %d, ActualLeft: %d, ActualRight: %d", expected, actualLeft, actualRight) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /binary-tree-2-traversals-in-pre-post-order/binary-tree-traversals-2-in-pre-post-order.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | left *Node 15 | right *Node 16 | } 17 | 18 | //Returns an initialized list 19 | func (n *Node) Init(data int) *Node { 20 | n.data = data 21 | n.left = nil 22 | n.right = nil 23 | return n 24 | } 25 | 26 | //Returns an new list 27 | func New(data int) *Node { 28 | return new(Node).Init(data) 29 | } 30 | 31 | func PrintPostOrder(node *Node) { 32 | //1. Check if the given node is NULL 33 | if node == nil { 34 | return 35 | } 36 | 37 | //2. First recur on left subtree 38 | PrintPostOrder(node.left) 39 | 40 | //3. Then recur on right subtree 41 | PrintPostOrder(node.right) 42 | 43 | //4. Now deal with the node 44 | fmt.Printf("%d ", node.data) 45 | } 46 | 47 | func PrintInOrder(node *Node) { 48 | //1. Check if the given node is NULL 49 | if node == nil { 50 | return 51 | } 52 | 53 | //2. First recur on left child 54 | PrintInOrder(node.left) 55 | 56 | //3. Then print the data of node 57 | fmt.Printf("%d ", node.data) 58 | 59 | //4. Now recur on right child 60 | PrintInOrder(node.right) 61 | } 62 | 63 | func PrintPreOrder(node *Node) { 64 | //1. Check if the given node is NULL 65 | if node == nil { 66 | return 67 | } 68 | 69 | //2. First print data of node 70 | fmt.Printf("%d ", node.data) 71 | 72 | //3. Then recur on left sutree 73 | PrintPreOrder(node.left) 74 | 75 | //4. Now recur on right subtree 76 | PrintPreOrder(node.right) 77 | } 78 | 79 | func main() { 80 | 81 | //To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node)); 82 | root := New(1) 83 | 84 | root.left = New(2) 85 | root.right = New(3) 86 | 87 | root.left.left = New(4) 88 | root.left.right = New(5) 89 | 90 | fmt.Println("\nPreorder traversal of binary tree is :") 91 | PrintPreOrder(root) 92 | fmt.Println("\nInorder traversal of binary tree is :") 93 | PrintInOrder(root) 94 | fmt.Println("\nPostorder traversal of binary tree is :") 95 | PrintPostOrder(root) 96 | } 97 | -------------------------------------------------------------------------------- /binary-tree-2-traversals-level-order/binary-tree-traversals-2-level-order.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | left *Node 15 | right *Node 16 | } 17 | 18 | //Returns an initialized list 19 | func (n *Node) Init(data int) *Node { 20 | n.data = data 21 | n.left = nil 22 | n.right = nil 23 | return n 24 | } 25 | 26 | //Returns an new list 27 | func New(data int) *Node { 28 | return new(Node).Init(data) 29 | } 30 | 31 | /* Compute the "height" of a tree -- the number of 32 |     nodes along the longest path from the root node 33 |     down to the farthest leaf node.*/ 34 | func GetHeight(node *Node) int { 35 | //1. Check if the given node is NULL 36 | if node == nil { 37 | return 0 38 | } 39 | 40 | //2. Compute the height of each subtree 41 | lheight := GetHeight(node.left) 42 | rheight := GetHeight(node.right) 43 | 44 | //3. Use the larger one 45 | if lheight > rheight { 46 | return lheight + 1 47 | } 48 | return rheight + 1 49 | } 50 | 51 | /* Print nodes at a given level */ 52 | func PrintGivenLevel(root *Node, level int) { 53 | //1. Check if the given root is NULL 54 | if root == nil { 55 | return 56 | } 57 | 58 | if level == 1 { 59 | fmt.Printf("%d ", root.data) 60 | } else if level > 1 { 61 | PrintGivenLevel(root.left, level-1) 62 | PrintGivenLevel(root.right, level-1) 63 | } 64 | } 65 | 66 | /* Function to print level order traversal a tree*/ 67 | func PrintLevelOrder(root *Node) { 68 | //1. Check if the given root is NULL 69 | if root == nil { 70 | fmt.Println("The given root node cannot be NULL") 71 | return 72 | } 73 | 74 | h := GetHeight(root) 75 | 76 | for i := 1; i <= h; i++ { 77 | PrintGivenLevel(root, i) 78 | } 79 | } 80 | 81 | func main() { 82 | 83 | //To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node)); 84 | root := New(1) 85 | 86 | root.left = New(2) 87 | root.right = New(3) 88 | 89 | root.left.left = New(4) 90 | root.left.right = New(5) 91 | 92 | fmt.Println("\nLevel Order traversal of binary tree is :") 93 | PrintLevelOrder(root) 94 | } 95 | -------------------------------------------------------------------------------- /binary-tree-3-doubly-linked-list/binary-tree-3-doubly-linked-list.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | left *Node 15 | right *Node 16 | } 17 | 18 | //Returns an initialized list 19 | func (n *Node) Init(data int) *Node { 20 | n.data = data 21 | n.left = nil 22 | n.right = nil 23 | return n 24 | } 25 | 26 | //Returns an new list 27 | func New(data int) *Node { 28 | return new(Node).Init(data) 29 | } 30 | 31 | // Standard Inorder traversal of tree 32 | func InOrder(root *Node) { 33 | //1. Check if the given node is NULL 34 | if root == nil { 35 | return 36 | } 37 | 38 | InOrder(root.left) 39 | fmt.Printf("%d ", root.data) 40 | InOrder(root.right) 41 | } 42 | 43 | // Changes left pointers to work as previous pointers in converted DLL 44 | // The function simply does inorder traversal of Binary Tree and updates 45 | // left pointer using previously visited node 46 | var pre *Node = nil 47 | 48 | func FixPrevPtr(root *Node) { 49 | //1. Check if the given root is NULL 50 | if root == nil { 51 | return 52 | } 53 | 54 | FixPrevPtr(root.left) 55 | root.left = pre 56 | pre = root 57 | FixPrevPtr(root.right) 58 | } 59 | 60 | // Changes right pointers to work as next pointers in converted DLL 61 | var prev *Node = nil 62 | 63 | func FixNextPtr(root *Node) *Node { 64 | // Find the right most node in BT or last node in DLL 65 | for root != nil && root.right != nil { 66 | root = root.right 67 | } 68 | 69 | // Start from the rightmost node, traverse back using left pointers. 70 | // While traversing, change right pointer of nodes. 71 | for root != nil && root.left != nil { 72 | prev = root 73 | root = root.left 74 | root.right = prev 75 | } 76 | 77 | // The leftmost node is head of linked list, return it 78 | return root 79 | } 80 | 81 | func BTToDLL(root *Node) *Node { 82 | // Set the previous pointer 83 | FixPrevPtr(root) 84 | 85 | // Set the next pointer and return head of DLL 86 | return FixNextPtr(root) 87 | } 88 | 89 | func PrintList(root *Node) { 90 | for root != nil { 91 | fmt.Printf("%d ", root.data) 92 | root = root.right 93 | } 94 | } 95 | 96 | func main() { 97 | 98 | //To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node)); 99 | root := New(10) 100 | 101 | root.left = New(12) 102 | root.right = New(15) 103 | 104 | root.left.left = New(25) 105 | root.left.right = New(30) 106 | 107 | root.right.left = New(36) 108 | 109 | fmt.Println("\nInorder Tree Traversal") 110 | InOrder(root) 111 | 112 | head := BTToDLL(root) 113 | fmt.Println("\nDLL Traversal") 114 | PrintList(head) 115 | } 116 | -------------------------------------------------------------------------------- /binary-tree-4-delete/binary-tree-4-delete.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | left *Node 15 | right *Node 16 | } 17 | 18 | //Returns an initialized list 19 | func (n *Node) Init(data int) *Node { 20 | n.data = data 21 | n.left = nil 22 | n.right = nil 23 | return n 24 | } 25 | 26 | //Returns an new list 27 | func New(data int) *Node { 28 | return new(Node).Init(data) 29 | } 30 | 31 | /* Compute the "height" of a tree -- the number of 32 |     nodes along the longest path from the root node 33 |     down to the farthest leaf node.*/ 34 | func DeleteTree(node *Node) { 35 | //1. Check if the given node is NULL 36 | if node == nil { 37 | return 38 | } 39 | 40 | //2. First delete both subtrees 41 | DeleteTree(node.left) 42 | DeleteTree(node.right) 43 | 44 | //3. Then delete the node 45 | fmt.Printf("\nDeleting node: %d", node.data) 46 | //free(node); 47 | node = nil 48 | } 49 | 50 | func main() { 51 | 52 | //To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node)); 53 | root := New(1) 54 | 55 | root.left = New(2) 56 | root.right = New(3) 57 | 58 | root.left.left = New(4) 59 | root.left.right = New(5) 60 | 61 | DeleteTree(root) 62 | root = nil 63 | 64 | fmt.Println("\nTree deleted") 65 | } 66 | -------------------------------------------------------------------------------- /binary-tree-5-find-min-max/binary-tree-5-find-min-max.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | const MaxUint = ^uint(0) 13 | const MinUint = 0 14 | const MaxInt = int(MaxUint >> 1) 15 | const MinInt = -MaxInt - 1 16 | 17 | type Node struct { 18 | data int 19 | left *Node 20 | right *Node 21 | } 22 | 23 | //Returns an initialized list 24 | func (n *Node) Init(data int) *Node { 25 | n.data = data 26 | n.left = nil 27 | n.right = nil 28 | return n 29 | } 30 | 31 | //Returns an new list 32 | func New(data int) *Node { 33 | return new(Node).Init(data) 34 | } 35 | 36 | // Returns minimum value in a given Binary Tree 37 | func FindMin(root *Node) int { 38 | //1. Check if the given node is NULL 39 | if root == nil { 40 | return MaxInt 41 | } 42 | 43 | //2. Return maximum of 3 values: 1) Root's data 2) Max in Left Subtree 3) Max in right subtree 44 | res := root.data 45 | lres := FindMin(root.left) 46 | rres := FindMin(root.right) 47 | 48 | if lres < res { 49 | res = lres 50 | } 51 | if rres < res { 52 | res = rres 53 | } 54 | 55 | return res 56 | } 57 | 58 | // Returns maximum value in a given Binary Tree 59 | func FindMax(root *Node) int { 60 | //1. Check if the given node is NULL 61 | if root == nil { 62 | return MinInt 63 | } 64 | 65 | //2. Return maximum of 3 values: 1) Root's data 2) Max in Left Subtree 3) Max in right subtree 66 | res := root.data 67 | lres := FindMax(root.left) 68 | rres := FindMax(root.right) 69 | 70 | if lres > res { 71 | res = lres 72 | } 73 | if rres > res { 74 | res = rres 75 | } 76 | 77 | return res 78 | } 79 | 80 | func main() { 81 | 82 | //To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node)); 83 | root := New(2) 84 | 85 | root.left = New(7) 86 | root.right = New(5) 87 | 88 | root.left.right = New(6) 89 | root.right.right = New(9) 90 | 91 | root.left.right.left = New(1) 92 | root.left.right.right = New(11) 93 | 94 | root.right.right.left = New(1) 95 | 96 | fmt.Printf("\nMaximum element is %d", FindMax(root)) 97 | fmt.Printf("\nMinimum element is %d", FindMin(root)) 98 | } 99 | -------------------------------------------------------------------------------- /coverage.txt: -------------------------------------------------------------------------------- 1 | mode: atomic 2 | Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:13.54,14.15 1 11 3 | Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:20.2,20.17 1 11 4 | Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:24.2,24.25 1 11 5 | Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:28.2,30.21 2 6 6 | Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:34.2,34.42 1 0 7 | Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:14.15,18.3 3 0 8 | Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:20.17,22.3 1 0 9 | Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:24.25,26.3 1 5 10 | Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:30.21,32.3 1 6 11 | Data-Structures-with-Go/array-smallest-missing-number/array-smallest-missing-number.go:37.13,41.2 3 0 12 | Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:17.21,18.11 1 20 13 | Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:21.2,21.12 1 3 14 | Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:24.2,24.10 1 2 15 | Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:18.11,20.3 1 17 16 | Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:21.12,23.3 1 1 17 | Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:27.43,30.25 2 5 18 | Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:38.2,38.17 1 5 19 | Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:30.25,31.30 1 24 20 | Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:31.30,32.92 1 49 21 | Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:32.92,34.5 1 5 22 | Data-Structures-with-Go/array-minimum-distance/array-minimum-distance.go:41.13,47.2 5 0 23 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:19.37,24.2 4 12 24 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:27.26,29.2 1 12 25 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:32.40,34.37 1 11 26 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:40.2,40.21 1 8 27 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:45.2,45.31 1 1 28 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:34.37,37.3 1 3 29 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:40.21,42.3 1 7 30 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:49.31,50.17 1 0 31 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:50.17,54.3 3 0 32 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:58.40,60.17 1 33 33 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:65.2,65.21 1 24 34 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:72.2,72.13 1 24 35 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:60.17,62.3 1 9 36 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:65.21,67.3 1 2 37 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:67.8,67.28 1 22 38 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:67.28,69.3 1 21 39 | Data-Structures-with-Go/binary-search-tree-1-insertion/binary-search-tree-1-insertion.go:75.13,94.2 8 0 40 | Data-Structures-with-Go/binary-tree-1-introduction/binary-tree-1-introduction.go:19.37,24.2 4 12 41 | Data-Structures-with-Go/binary-tree-1-introduction/binary-tree-1-introduction.go:27.26,29.2 1 12 42 | Data-Structures-with-Go/binary-tree-1-introduction/binary-tree-1-introduction.go:31.13,65.2 8 0 43 | Data-Structures-with-Go/array-rotation/array-rotation.go:12.41,13.25 1 5 44 | Data-Structures-with-Go/array-rotation/array-rotation.go:13.25,15.3 1 19 45 | Data-Structures-with-Go/array-rotation/array-rotation.go:18.39,21.28 3 19 46 | Data-Structures-with-Go/array-rotation/array-rotation.go:24.2,24.15 1 19 47 | Data-Structures-with-Go/array-rotation/array-rotation.go:21.28,23.3 1 100 48 | Data-Structures-with-Go/array-rotation/array-rotation.go:27.37,28.21 1 0 49 | Data-Structures-with-Go/array-rotation/array-rotation.go:32.2,32.28 1 0 50 | Data-Structures-with-Go/array-rotation/array-rotation.go:28.21,31.3 2 0 51 | Data-Structures-with-Go/array-rotation/array-rotation.go:32.28,34.3 1 0 52 | Data-Structures-with-Go/array-rotation/array-rotation.go:37.13,45.2 7 0 53 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:18.29,21.2 2 46 54 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:24.18,26.2 1 46 55 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:29.29,31.2 1 0 56 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:34.29,36.44 2 0 57 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:39.2,39.16 1 0 58 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:36.44,38.3 1 0 59 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:42.42,57.2 4 30 60 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:71.51,83.2 4 9 61 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:88.42,98.6 4 4 62 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:117.2,117.19 1 4 63 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:98.6,99.15 1 13 64 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:108.3,108.23 1 9 65 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:114.3,114.19 1 9 66 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:99.15,102.9 2 2 67 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:103.9,103.22 1 11 68 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:103.22,105.9 2 2 69 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:108.23,110.4 1 3 70 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:110.9,112.4 1 6 71 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:121.25,122.15 1 0 72 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:122.15,126.3 3 0 73 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:129.33,131.15 2 0 74 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:135.2,135.13 1 0 75 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:131.15,134.3 2 0 76 | Data-Structures-with-Go/linked-list-merge-two-sorted/linked-list-merge-two-sorted.go:138.13,157.2 12 0 77 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:18.29,21.2 2 19 78 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:24.18,26.2 1 19 79 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:29.29,31.2 1 16 80 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:34.29,36.44 2 0 81 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:39.2,39.16 1 0 82 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:36.44,38.3 1 0 83 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:42.42,57.2 4 10 84 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:59.31,64.21 4 3 85 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:71.2,71.18 1 3 86 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:64.21,69.3 4 13 87 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:75.24,76.15 1 0 88 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:76.15,79.3 2 0 89 | Data-Structures-with-Go/linked-list-reverse/linked-list-reverse.go:82.13,101.2 10 0 90 | Data-Structures-with-Go/merge-sort/merge-sort.go:12.36,22.25 6 19 91 | Data-Structures-with-Go/merge-sort/merge-sort.go:26.2,26.25 1 19 92 | Data-Structures-with-Go/merge-sort/merge-sort.go:31.2,35.23 4 19 93 | Data-Structures-with-Go/merge-sort/merge-sort.go:47.2,47.13 1 19 94 | Data-Structures-with-Go/merge-sort/merge-sort.go:54.2,54.13 1 19 95 | Data-Structures-with-Go/merge-sort/merge-sort.go:22.25,24.3 1 34 96 | Data-Structures-with-Go/merge-sort/merge-sort.go:26.25,28.3 1 28 97 | Data-Structures-with-Go/merge-sort/merge-sort.go:35.23,36.19 1 35 98 | Data-Structures-with-Go/merge-sort/merge-sort.go:43.3,43.6 1 35 99 | Data-Structures-with-Go/merge-sort/merge-sort.go:36.19,39.4 2 17 100 | Data-Structures-with-Go/merge-sort/merge-sort.go:39.9,42.4 2 18 101 | Data-Structures-with-Go/merge-sort/merge-sort.go:47.13,51.3 3 17 102 | Data-Structures-with-Go/merge-sort/merge-sort.go:54.13,58.3 3 10 103 | Data-Structures-with-Go/merge-sort/merge-sort.go:61.37,62.11 1 42 104 | Data-Structures-with-Go/merge-sort/merge-sort.go:62.11,71.3 4 19 105 | Data-Structures-with-Go/merge-sort/merge-sort.go:74.36,75.28 1 0 106 | Data-Structures-with-Go/merge-sort/merge-sort.go:78.2,78.18 1 0 107 | Data-Structures-with-Go/merge-sort/merge-sort.go:75.28,77.3 1 0 108 | Data-Structures-with-Go/merge-sort/merge-sort.go:81.13,92.2 7 0 109 | Data-Structures-with-Go/quick-sort/quick-sort.go:12.27,16.2 3 38 110 | Data-Structures-with-Go/quick-sort/quick-sort.go:18.47,25.34 3 15 111 | Data-Structures-with-Go/quick-sort/quick-sort.go:33.2,35.16 2 15 112 | Data-Structures-with-Go/quick-sort/quick-sort.go:25.34,27.22 1 49 113 | Data-Structures-with-Go/quick-sort/quick-sort.go:27.22,30.4 2 23 114 | Data-Structures-with-Go/quick-sort/quick-sort.go:43.43,44.17 1 34 115 | Data-Structures-with-Go/quick-sort/quick-sort.go:44.17,51.3 3 15 116 | Data-Structures-with-Go/quick-sort/quick-sort.go:54.38,55.28 1 0 117 | Data-Structures-with-Go/quick-sort/quick-sort.go:58.2,58.18 1 0 118 | Data-Structures-with-Go/quick-sort/quick-sort.go:55.28,57.3 1 0 119 | Data-Structures-with-Go/quick-sort/quick-sort.go:61.13,69.2 5 0 120 | Data-Structures-with-Go/stack/stack.go:24.44,29.2 4 4 121 | Data-Structures-with-Go/stack/stack.go:32.32,34.2 1 4 122 | Data-Structures-with-Go/stack/stack.go:37.32,39.2 1 14 123 | Data-Structures-with-Go/stack/stack.go:42.33,44.2 1 14 124 | Data-Structures-with-Go/stack/stack.go:46.35,47.19 1 14 125 | Data-Structures-with-Go/stack/stack.go:50.2,51.31 2 14 126 | Data-Structures-with-Go/stack/stack.go:47.19,49.3 1 0 127 | Data-Structures-with-Go/stack/stack.go:54.28,55.20 1 14 128 | Data-Structures-with-Go/stack/stack.go:58.2,60.13 3 14 129 | Data-Structures-with-Go/stack/stack.go:55.20,57.3 1 0 130 | Data-Structures-with-Go/stack/stack.go:63.13,74.2 8 0 131 | -------------------------------------------------------------------------------- /linked-list-1-introduction/README.md: -------------------------------------------------------------------------------- 1 |

LinkedList | SET 1 (INTRODUCTION) Source

2 | 3 | [What It Is](#what-it-is) 4 | 5 | ## What It Is 6 | 7 | Like arrays, LinkedList is a linear dat structure. Unlike arrays, LinkedList elements are not stored at contigous location; the elements are linked using pointers. 8 | 9 | ![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-1-introduction/resources/linked-list.png) 10 | 11 | Why Linked List ? 12 | -------------------------- 13 | 14 | Arrays can be used to store linear data of similar types, but arrays have following limitations ; 15 | 16 | * 1) The size of the arrays is fixed. So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to upper limit irrespective of the usage. 17 | 18 | * 2) Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted. 19 | 20 | **Example** 21 | 22 | > * ID[] = [1000, 1010, 1050, 2000, 2040] 23 | 24 | And if we want to insert a new ID 10005, the to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000). 25 | 26 | Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in ID[], everythink after 1010 has to moved. 27 | 28 | Advantages Over Arrays 29 | -------------------------- 30 | 31 | * 1) Dynamic size 32 | 33 | * 2) Ease of insertion/deletion 34 | 35 | Drawbacks 36 | -------------------------- 37 | 38 | * 1) Random access is not allowed. We have to access elements sequientally starting from the first node. So we cannot do binary search with LinkedLists. 39 | 40 | * 2) Extra memory space for a pointer is required with each element of the list. -------------------------------------------------------------------------------- /linked-list-1-introduction/linked-list-1-introduction.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | next *Node 15 | } 16 | 17 | //Returns an initialized list 18 | func (n *Node) Init() *Node { 19 | n.data = -1 20 | return n 21 | } 22 | 23 | //Returns an new list 24 | func New() *Node { 25 | return new(Node).Init() 26 | } 27 | 28 | //Returns the first node in list 29 | func (n *Node) Next() *Node { 30 | return n.next 31 | } 32 | 33 | //Returns the last node in list if exist, otherwise returns current 34 | func (n *Node) Back() *Node { 35 | current := n.next 36 | for current != nil && current.next != nil { 37 | current = current.next 38 | } 39 | return current 40 | } 41 | 42 | //This function prints contents of linked list starting from the given node 43 | func printList(n *Node){ 44 | for n != nil { 45 | fmt.Println(n.data) 46 | n = n.next 47 | } 48 | } 49 | 50 | func main() { 51 | //To allocate dynamically a new Node in C language : head = (struct Node*) malloc(sizeof(struct Node)); 52 | head := New() 53 | second := New() 54 | third := New() 55 | 56 | //Assign data in first node 57 | head.data = 1 58 | //Link first node with second node 59 | head.next = second 60 | 61 | second.data = 2 62 | second.next = third 63 | 64 | third.data = 3 65 | third.next = nil 66 | 67 | printList(head) 68 | } -------------------------------------------------------------------------------- /linked-list-1-introduction/resources/linked-list.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/17ec0461bcf3eaa1152df8c178d5d751882c485d/linked-list-1-introduction/resources/linked-list.png -------------------------------------------------------------------------------- /linked-list-2-inserting-a-node/README.md: -------------------------------------------------------------------------------- 1 |

LinkedList | SET 2 (INSERTING A NODE) Source

2 | 3 | [What It Is](#what-it-is) 4 | 5 | ## What It Is 6 | 7 | We have introduced Linked Lists in the **[previous post](https://github.com/Dentrax/Data-Structures-with-Go/tree/master/linked-list-1-introduction)**. We also created a simple linked list with 3 nodes and discussed linked list traversal. 8 | All programs discussed in this post consider following representations of linked list . 9 | 10 | In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways ; 11 | 12 | * 1) At the front of the linked list 13 | * 2) After a given node. 14 | * 3) At the end of the linked list. 15 | 16 | 17 | Add a node at the front (A 4 steps process) 18 | -------------------------- 19 | 20 | The new node is always added before the head of the given Linked List. And newly added node becomes the new head of the Linked List. For example if the given Linked List is `10->15->20->25` and we add an item 5 at the front, then the Linked List becomes `5->10->15->20->25`. Let us call the function that adds at the front of the list is `push()`. The `push()` must receive a pointer to the head pointer, because push must change the head pointer to point to the new node. 21 | 22 | ![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-2-inserting-a-node/resources/inserting-a-node-1.png) 23 | 24 | **Algorithm Complexity** 25 | 26 | | Complexity | Notation | 27 | | ----------------- |:---------:| 28 | | `Time Complexity` | `O(1)` | 29 | 30 | 31 | Add a node after a given node (5 steps process) 32 | -------------------------- 33 | 34 | We are given pointer to a node, and the new node is inserted after the given node. 35 | 36 | ![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-2-inserting-a-node/resources/inserting-a-node-2.png) 37 | 38 | **Algorithm Complexity** 39 | 40 | | Complexity | Notation | 41 | | ----------------- |:---------:| 42 | | `Time Complexity` | `O(1)` | 43 | 44 | 45 | Add a node at the end (6 steps process) 46 | -------------------------- 47 | 48 | The new node is always added after the last node of the given Linked List. For example if the given Linked List is `5->10->15->20->25` and we add an item 30 at the end, then the Linked List becomes `5->10->15->20->25->30`. 49 | Since a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node. 50 | 51 | ![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-2-inserting-a-node/resources/inserting-a-node-3.png) 52 | 53 | **Algorithm Complexity** 54 | 55 | | Complexity | Notation | 56 | | ----------------- |:---------:| 57 | | `Time Complexity` | `O(n)` | -------------------------------------------------------------------------------- /linked-list-2-inserting-a-node/linked-list-2-inserting-a-node.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | next *Node 15 | } 16 | 17 | //Returns an initialized list 18 | func (n *Node) Init() *Node { 19 | n.data = -1 20 | return n 21 | } 22 | 23 | //Returns an new list 24 | func New() *Node { 25 | return new(Node).Init() 26 | } 27 | 28 | //Returns the first node in list 29 | func (n *Node) Next() *Node { 30 | return n.next 31 | } 32 | 33 | //Returns the last node in list if exist, otherwise returns current 34 | func (n *Node) Back() *Node { 35 | current := n.next 36 | for current != nil && current.next != nil { 37 | current = current.next 38 | } 39 | return current 40 | } 41 | 42 | func Push(head_ref **Node, new_data int) { 43 | //new_node := Node{data: new_data, next: (*head_ref)} 44 | //*head_ref = new_node 45 | 46 | //1. Allocate new node 47 | new_node := New() 48 | 49 | //2. Put in the data 50 | new_node.data = new_data 51 | 52 | //3. Make next of new node as head 53 | new_node.next = (*head_ref) 54 | 55 | //4. Move the head to point to new_node 56 | *head_ref = new_node 57 | } 58 | 59 | func InsertAfter(prev_node *Node, new_data int) { 60 | //1. Check if the given prev_node is NULL 61 | if(prev_node == nil){ 62 | fmt.Println("The given previous node cannot be NULL") 63 | return 64 | } 65 | 66 | //2. Allocate new node 67 | new_node := New() 68 | 69 | //3. Put in the data 70 | new_node.data = new_data 71 | 72 | //4. Make next of new node as next of prev_node 73 | new_node.next = prev_node.next 74 | 75 | //5. Move the next of prev_node as new_node 76 | prev_node.next = new_node 77 | } 78 | 79 | func Append(head_ref **Node, new_data int) { 80 | //1. Allocate new node 81 | new_node := New() 82 | 83 | last := *head_ref 84 | 85 | //2. Put in the data 86 | new_node.data = new_data 87 | 88 | //3. this new node is going to be last node, so make next of it as NULL 89 | new_node.next = nil 90 | 91 | //4. If the LinkedList is empty, then make the new node as head 92 | if(*head_ref == nil){ 93 | *head_ref = new_node 94 | return 95 | } 96 | 97 | //5. Else traverse till the last node 98 | for(last.next != nil){ 99 | last = last.next 100 | } 101 | 102 | //6. Change the next of last node 103 | last.next = new_node 104 | return 105 | } 106 | 107 | //This function prints contents of linked list starting from the given node 108 | func printList(n *Node){ 109 | for n != nil { 110 | fmt.Println(n.data) 111 | n = n.next 112 | } 113 | } 114 | 115 | func main() { 116 | //Start with the empty list 117 | head := New() 118 | 119 | //Insert 6. 120 | //So LinkedList becomes [NULL->6] 121 | Append(&head, 6) 122 | 123 | //Insert 7 at the beginning. 124 | //So LinkedList becomes [7->NULL->6] 125 | Push(&head, 7) 126 | 127 | //Insert 1 at the beginning. 128 | //So LinkedList becomes [1->7->NULL->6] 129 | Push(&head, 1) 130 | 131 | //Insert 4 at the end. 132 | //So LinkedList becomes [1->7->NULL->6->4] 133 | Append(&head, 4) 134 | 135 | //Insert 8, after 7 136 | //So LinkedList becomes [1->7->8->NULL->6->4] 137 | InsertAfter(head.next, 8) 138 | 139 | fmt.Println("Created LinkedList is: ") 140 | 141 | printList(head) 142 | } -------------------------------------------------------------------------------- /linked-list-2-inserting-a-node/resources/inserting-a-node-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/17ec0461bcf3eaa1152df8c178d5d751882c485d/linked-list-2-inserting-a-node/resources/inserting-a-node-1.png -------------------------------------------------------------------------------- /linked-list-2-inserting-a-node/resources/inserting-a-node-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/17ec0461bcf3eaa1152df8c178d5d751882c485d/linked-list-2-inserting-a-node/resources/inserting-a-node-2.png -------------------------------------------------------------------------------- /linked-list-2-inserting-a-node/resources/inserting-a-node-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/17ec0461bcf3eaa1152df8c178d5d751882c485d/linked-list-2-inserting-a-node/resources/inserting-a-node-3.png -------------------------------------------------------------------------------- /linked-list-3-deleting-a-node/README.md: -------------------------------------------------------------------------------- 1 |

LinkedList | SET 3 (DELETING A NODE) Source

2 | 3 | [What It Is](#what-it-is) 4 | 5 | ## What It Is 6 | 7 | We have discussed **[Linked List Introduction](https://github.com/Dentrax/Data-Structures-with-Go/tree/master/linked-list-1-introduction)** and **[Linked List Insertion](https://github.com/Dentrax/Data-Structures-with-Go/tree/master/linked-list-2-inserting-a-node)** in previous posts on singly linked list. 8 | Let us formulate the problem statement to understand the deletion process. Given a `key`, delete the first occurrence of this key in linked list. 9 | 10 | To delete a node from linked list, we need to do following steps. 11 | 12 | * 1) Find previous node of the node to be deleted. 13 | * 2) Changed next of previous node. 14 | * 3) Free memory for the node to be deleted. 15 | 16 | > * Input: Linked List = `[7 -> 1 -> 3 -> 2]` 17 | > * Output: Created Linked List `[2 -> 3 -> 1 -> 7]` 18 | > * Output: Linked List after Deletion of 1: `[2 -> 3 -> 7]` 19 | 20 | > * Input: Position = 1, Linked List = `[8 -> 2 -> 3 -> 1 -> 7]` 21 | > * Output: Linked List = `[8 -> 3 -> 1 -> 7]` 22 | 23 | > * Input: Position = 0, Linked List = `[8 -> 2 -> 3 -> 1 -> 7]` 24 | > * Output: Linked List = `[2 -> 3 -> 1 -> 7]` 25 | 26 | 27 | 28 | **Algorithm Complexity** 29 | 30 | | Complexity | Notation | 31 | | ----------------- |:---------:| 32 | | `Time Complexity` | `O(1)` | 33 | -------------------------------------------------------------------------------- /linked-list-3-deleting-a-node/linked-list-3-deleting-a-node.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | next *Node 15 | } 16 | 17 | //Returns an initialized list 18 | func (n *Node) Init() *Node { 19 | n.data = -1 20 | return n 21 | } 22 | 23 | //Returns an new list 24 | func New() *Node { 25 | return new(Node).Init() 26 | } 27 | 28 | //Returns the first node in list 29 | func (n *Node) Next() *Node { 30 | return n.next 31 | } 32 | 33 | //Returns the last node in list if exist, otherwise returns current 34 | func (n *Node) Back() *Node { 35 | current := n.next 36 | for current != nil && current.next != nil { 37 | current = current.next 38 | } 39 | return current 40 | } 41 | 42 | func Push(head_ref **Node, new_data int) { 43 | //new_node := Node{data: new_data, next: (*head_ref)} 44 | //*head_ref = new_node 45 | 46 | //1. Allocate new node 47 | new_node := New() 48 | 49 | //2. Put in the data 50 | new_node.data = new_data 51 | 52 | //3. Make next of new node as head 53 | new_node.next = (*head_ref) 54 | 55 | //4. Move the head to point to new_node 56 | *head_ref = new_node 57 | } 58 | 59 | func DeleteNodeWithData(head_ref **Node, delete_data int) { 60 | //Store head node 61 | temp := *head_ref 62 | prev := *head_ref 63 | 64 | //If head node itself holds the delete_data to be deleted 65 | if(temp != nil && temp.data == delete_data){ 66 | *head_ref = temp.next; 67 | temp = nil 68 | return 69 | } 70 | 71 | //Search for the delete_data to be deleted, keep track of the previous node as we need to change prev->next 72 | for(temp != nil && temp.data != delete_data){ 73 | prev = temp 74 | temp = temp.next 75 | } 76 | 77 | // If delete_data was not present in linked list 78 | if(temp == nil){ 79 | return 80 | } 81 | 82 | //Unlink the node from linked list 83 | prev.next = temp.next 84 | 85 | temp = nil 86 | } 87 | 88 | func DeleteNodeWithPosition(head_ref **Node, delete_position int){ 89 | //If LinkedList is empty 90 | if(*head_ref == nil){ 91 | return 92 | } 93 | 94 | //Store head node 95 | temp := *head_ref 96 | 97 | //If head needs to be removed 98 | if(delete_position == 0){ 99 | *head_ref = temp.next 100 | temp = nil 101 | return 102 | } 103 | 104 | //Find previous node of the node to be deleted 105 | for i := 0; temp != nil && i < delete_position - 1; i++ { 106 | temp = temp.next 107 | } 108 | 109 | //If position is more than number of nodes 110 | if(temp == nil || temp.next == nil){ 111 | return 112 | } 113 | 114 | //Node temp->next is the node to be deleted, Store pointer to the next of node to be deleted 115 | next := temp.next.next 116 | 117 | //Unlink the node from linked list 118 | temp.next = nil 119 | 120 | //Unlink the deleted node from list 121 | temp.next = next 122 | } 123 | 124 | //This function prints contents of linked list starting from the given node 125 | func printList(n *Node){ 126 | for n != nil { 127 | fmt.Println(n.data) 128 | n = n.next 129 | } 130 | } 131 | 132 | func main() { 133 | //Start with the empty list 134 | head := New() 135 | 136 | Push(&head, 7) 137 | 138 | Push(&head, 1) 139 | 140 | Push(&head, 3) 141 | 142 | Push(&head, 2) 143 | 144 | Push(&head, 8) 145 | 146 | fmt.Println("Created LinkedList is: ") 147 | 148 | printList(head) 149 | 150 | DeleteNodeWithData(&head, 1) 151 | 152 | DeleteNodeWithPosition(&head, 4) 153 | 154 | fmt.Println("LinkedList after Deletion of 1: ") 155 | 156 | printList(head) 157 | } -------------------------------------------------------------------------------- /linked-list-3-deleting-a-node/resources/deleting-a-node.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/17ec0461bcf3eaa1152df8c178d5d751882c485d/linked-list-3-deleting-a-node/resources/deleting-a-node.png -------------------------------------------------------------------------------- /linked-list-circular-1-introduction/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/17ec0461bcf3eaa1152df8c178d5d751882c485d/linked-list-circular-1-introduction/README.md -------------------------------------------------------------------------------- /linked-list-circular-2-traversal/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/17ec0461bcf3eaa1152df8c178d5d751882c485d/linked-list-circular-2-traversal/README.md -------------------------------------------------------------------------------- /linked-list-circular-2-traversal/linked-list-circular-2-traversal.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | next *Node 15 | } 16 | 17 | //Returns an initialized list 18 | func (n *Node) Init() *Node { 19 | n.data = -1 20 | return n 21 | } 22 | 23 | //Returns an new list 24 | func New() *Node { 25 | return new(Node).Init() 26 | } 27 | 28 | //Returns the first node in list 29 | func (n *Node) Next() *Node { 30 | return n.next 31 | } 32 | 33 | //Returns the last node in list if exist, otherwise returns current 34 | func (n *Node) Back() *Node { 35 | current := n.next 36 | for current != nil && current.next != nil { 37 | current = current.next 38 | } 39 | return current 40 | } 41 | 42 | //This function prints contents of linked list starting from the given node 43 | func printList(n *Node) { 44 | for n != nil { 45 | fmt.Println(n.data) 46 | n = n.next 47 | } 48 | } 49 | 50 | func Push(head_ref **Node, new_data int) { 51 | //1. Allocate new node 52 | new_node := New() 53 | temp := *head_ref 54 | 55 | new_node.data = new_data 56 | new_node.next = *head_ref 57 | 58 | //2. If linked list is not NULL then set the next of last node 59 | if *head_ref != nil { 60 | for temp.next != *head_ref { 61 | temp = temp.next 62 | } 63 | temp.next = new_node 64 | } else { 65 | //For the fist node 66 | new_node.next = new_node 67 | } 68 | //4. Move the head to point to new_node 69 | *head_ref = new_node 70 | } 71 | 72 | func main() { 73 | //Start with the empty list 74 | head := New() 75 | 76 | Push(&head, 12) 77 | Push(&head, 56) 78 | Push(&head, 2) 79 | Push(&head, 11) 80 | 81 | fmt.Println("Created Circular LinkedList is: ") 82 | 83 | printList(head) 84 | } 85 | -------------------------------------------------------------------------------- /linked-list-circular-singly-1-insertion/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/17ec0461bcf3eaa1152df8c178d5d751882c485d/linked-list-circular-singly-1-insertion/README.md -------------------------------------------------------------------------------- /linked-list-circular-singly-1-insertion/linked-list-circular-singly-1-insertion.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | next *Node 15 | } 16 | 17 | //Returns an initialized list 18 | func (n *Node) Init() *Node { 19 | n.data = -1 20 | return n 21 | } 22 | 23 | //Returns an new list 24 | func New() *Node { 25 | return new(Node).Init() 26 | } 27 | 28 | //Returns the first node in list 29 | func (n *Node) Next() *Node { 30 | return n.next 31 | } 32 | 33 | //Returns the last node in list if exist, otherwise returns current 34 | func (n *Node) Back() *Node { 35 | current := n.next 36 | for current != nil && current.next != nil { 37 | current = current.next 38 | } 39 | return current 40 | } 41 | 42 | //Function to insert node in an empty List 43 | func AddToEmpty(last *Node, data int) *Node { 44 | if(last != nil){ 45 | return last 46 | } 47 | 48 | //Creating a node 49 | temp := New() 50 | 51 | //Assigning the data 52 | temp.data = data 53 | last = temp 54 | 55 | //Creating the link 56 | last.next = last 57 | 58 | return last 59 | } 60 | 61 | //Function to insert node in the beginning of the List 62 | func AddBegin(last *Node, data int) *Node { 63 | if(last == nil){ 64 | return AddToEmpty(last, data) 65 | } 66 | 67 | //Creating a node 68 | temp := New() 69 | 70 | //Assigning the data 71 | temp.data = data 72 | 73 | //List was empty. We link single node to itself 74 | temp.next = last.next 75 | last.next = temp 76 | 77 | return last 78 | } 79 | 80 | //Function to insert node in the end of the List 81 | func AddEnd(last *Node, data int) *Node { 82 | if(last == nil){ 83 | return AddToEmpty(last, data) 84 | } 85 | 86 | //Creating a node 87 | temp := New() 88 | 89 | //Assigning the data 90 | temp.data = data 91 | 92 | //List was empty. We link single node to itself 93 | temp.next = last.next 94 | last.next = temp 95 | last = temp 96 | 97 | return last 98 | } 99 | 100 | //Function to insert node in the end of the List 101 | func AddAfter(last *Node, data int, item int) *Node { 102 | if(last == nil){ 103 | return nil 104 | } 105 | 106 | //Creating a node 107 | temp := New() 108 | p := New() 109 | 110 | p = last.next 111 | 112 | for(p != last.next){ 113 | if(p.data == item){ 114 | //Assigning the data 115 | temp.data = data 116 | //Adjusting the links 117 | temp.next = p.next 118 | 119 | //Adding newly allocated node after p 120 | p.next = temp 121 | 122 | //Checking for the last node 123 | if(p == last){ 124 | last = temp 125 | } 126 | 127 | return last 128 | } 129 | p = p.next 130 | } 131 | 132 | fmt.Printf("\n%d not present in the list.\n", item) 133 | 134 | return last 135 | } 136 | 137 | func Traverse(last *Node){ 138 | p := New() 139 | 140 | //If list is empty, return 141 | if(last == nil){ 142 | fmt.Println("List is empty") 143 | return 144 | } 145 | 146 | //Pointing to first Node of the list 147 | p = last.next 148 | 149 | //Traversing the list 150 | for p != nil { 151 | fmt.Println(p.data) 152 | p = p.next 153 | } 154 | } 155 | 156 | //This function prints contents of linked list starting from the given node 157 | func printList(n *Node){ 158 | for n != nil { 159 | fmt.Println(n.data) 160 | n = n.next 161 | } 162 | } 163 | 164 | func main() { 165 | last := New() 166 | 167 | last = AddEnd(last, 12) 168 | last = AddToEmpty(last, 6) 169 | last = AddBegin(last, 4) 170 | last = AddBegin(last, 2) 171 | last = AddEnd(last, 8) 172 | last = AddAfter(last, 10, 8) 173 | 174 | Traverse(last) 175 | } -------------------------------------------------------------------------------- /linked-list-find-length/README.md: -------------------------------------------------------------------------------- 1 |

LinkedList Find Length Source

2 | 3 | [What It Is](#what-it-is) 4 | 5 | ## What It Is 6 | 7 | For example, the function should return 5 for linked list `[1 -> 3 -> 1 -> 2 -> 1]` 8 | 9 | ![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/linked-list-find-length/resources/find-length.png) 10 | 11 | 12 | METHOD 1 (Iterative Solution) 13 | -------------------------- 14 | 15 | * 1) Initialize count as 0 16 | * 2) Initialize a node pointer, current = head. 17 | * 3) Do following while current is not NULL 18 | a) current = current -> next 19 | b) count++; 20 | * 4) Return count 21 | 22 | > * Input: Linked List = `[1 -> 3 -> 1 -> 2 -> 1]` 23 | > * Output: count of nodes is `5` 24 | 25 | **Algorithm Complexity** 26 | 27 | | Complexity | Notation | 28 | | ----------------- |:---------:| 29 | | `Time Complexity` | `O(n)` | 30 | -------------------------------------------------------------------------------- /linked-list-find-length/linked-list-find-length.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | next *Node 15 | } 16 | 17 | //Returns an initialized list 18 | func (n *Node) Init() *Node { 19 | n.data = -1 20 | return n 21 | } 22 | 23 | //Returns an new list 24 | func New() *Node { 25 | return new(Node).Init() 26 | } 27 | 28 | //Returns the first node in list 29 | func (n *Node) Next() *Node { 30 | return n.next 31 | } 32 | 33 | //Returns the last node in list if exist, otherwise returns current 34 | func (n *Node) Back() *Node { 35 | current := n.next 36 | for current != nil && current.next != nil { 37 | current = current.next 38 | } 39 | return current 40 | } 41 | 42 | func Push(head_ref **Node, new_data int) { 43 | //new_node := Node{data: new_data, next: (*head_ref)} 44 | //*head_ref = new_node 45 | 46 | //1. Allocate new node 47 | new_node := New() 48 | 49 | //2. Put in the data 50 | new_node.data = new_data 51 | 52 | //3. Make next of new node as head 53 | new_node.next = (*head_ref) 54 | 55 | //4. Move the head to point to new_node 56 | *head_ref = new_node 57 | } 58 | 59 | func GetCount(head *Node) int { 60 | var count int = 0 61 | current := head 62 | 63 | for(current != nil){ 64 | count++ 65 | current = current.next 66 | } 67 | 68 | return count 69 | } 70 | 71 | //This function prints contents of linked list starting from the given node 72 | func printList(n *Node){ 73 | for n != nil { 74 | fmt.Println(n.data) 75 | n = n.next 76 | } 77 | } 78 | 79 | func main() { 80 | //Start with the empty list 81 | head := New() 82 | 83 | Push(&head, 1) 84 | 85 | Push(&head, 3) 86 | 87 | Push(&head, 1) 88 | 89 | Push(&head, 2) 90 | 91 | Push(&head, 1) 92 | 93 | fmt.Printf("Count of Nodes is %d", GetCount(head)) 94 | } -------------------------------------------------------------------------------- /linked-list-find-length/resources/find-length.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/17ec0461bcf3eaa1152df8c178d5d751882c485d/linked-list-find-length/resources/find-length.png -------------------------------------------------------------------------------- /linked-list-merge-two-sorted/README.md: -------------------------------------------------------------------------------- 1 |

Merge Two Sorted Source

2 | 3 | [What It Is](#what-it-is) 4 | 5 | ## What It Is 6 | 7 | Write a `SortedMerge()` function that takes two lists, each of which is sorted in increasing order, and merges the two together into one list which is in increasing order. `SortedMerge()` should return the new list. The new list should be made by splicing 8 | together the nodes of the first two lists. 9 | 10 | > * Input: LinkedList a `5->10->15` 11 | > * Input: LinkedList b `2->3->20` 12 | > * Output: `2->3->5->10->15->20` 13 | 14 | There are many cases to deal with: either `a` or `b` may be empty, during processing either `a` or `b` may run out first, and finally there’s the problem of starting the result list empty, and building it up while going through `a` and `b`. 15 | 16 | METHOD 1 (Using Dummy Nodes) 17 | -------------------------- 18 | 19 | The strategy here uses a temporary dummy node as the start of the result list. The pointer Tail always points to the last node in the result list, so appending new nodes is easy. 20 | The dummy node gives tail something to point to initially when the result list is empty. This dummy node is efficient, since it is only temporary, and it is allocated in the stack. The loop proceeds, removing one node from either `a` or `b`, and adding it to tail. When 21 | we are done, the result is in dummy.next 22 | 23 | **Algorithm Complexity** 24 | 25 | | Complexity | Notation | 26 | | ----------------- |:---------:| 27 | | `Time Complexity` | `O(n)` | -------------------------------------------------------------------------------- /linked-list-merge-two-sorted/linked-list-merge-two-sorted.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | next *Node 15 | } 16 | 17 | //Returns an initialized list 18 | func (n *Node) Init() *Node { 19 | n.data = -1 20 | return n 21 | } 22 | 23 | //Returns an new list 24 | func New() *Node { 25 | return new(Node).Init() 26 | } 27 | 28 | //Returns the first node in list 29 | func (n *Node) Next() *Node { 30 | return n.next 31 | } 32 | 33 | //Returns the last node in list if exist, otherwise returns current 34 | func (n *Node) Back() *Node { 35 | current := n.next 36 | for current != nil && current.next != nil { 37 | current = current.next 38 | } 39 | return current 40 | } 41 | 42 | func Push(head_ref **Node, new_data int) { 43 | //new_node := Node{data: new_data, next: (*head_ref)} 44 | //*head_ref = new_node 45 | 46 | //1. Allocate new node 47 | new_node := New() 48 | 49 | //2. Put in the data 50 | new_node.data = new_data 51 | 52 | //3. Make next of new node as head 53 | new_node.next = (*head_ref) 54 | 55 | //4. Move the head to point to new_node 56 | *head_ref = new_node 57 | } 58 | 59 | //Pull off the front node of the source and put it in dest 60 | /* MoveNode() function takes the node from the front of the 61 |    source, and move it to the front of the dest. 62 |    It is an error to call this with the source list empty. 63 | 64 |    Before calling MoveNode(): 65 |    source == {1, 2, 3} 66 |    dest == {1, 2, 3} 67 | 68 |    Affter calling MoveNode(): 69 |    source == {2, 3} 70 |    dest == {1, 1, 2, 3} */ 71 | func MoveNode(dest_ref **Node, source_ref **Node) { 72 | //The front source code 73 | new_node := *source_ref 74 | 75 | //Advance the source pointer 76 | *source_ref = new_node.next 77 | 78 | //Link the old dest off the new node 79 | new_node.next = *dest_ref 80 | 81 | //Move dest to point to the new node 82 | *dest_ref = new_node 83 | } 84 | 85 | /* Takes two lists sorted in increasing order, and splices 86 |    their nodes together to make one big sorted list which 87 |    is returned.  */ 88 | func SortedMerge(a *Node, b *Node) *Node { 89 | //A dummy first node to hang the result on 90 | dummy := New() 91 | 92 | //Tail points to the last result node 93 | tail := dummy 94 | 95 | //So tail.next is the place to add new nodes to result 96 | dummy.next = nil 97 | 98 | for { 99 | if a == nil { 100 | //If either list runs out, use the other list 101 | tail.next = b 102 | break 103 | } else if b == nil { 104 | tail.next = a 105 | break 106 | } 107 | 108 | if a.data <= b.data { 109 | MoveNode(&(tail.next), &a) 110 | } else { 111 | MoveNode(&(tail.next), &b) 112 | } 113 | 114 | tail = tail.next 115 | } 116 | 117 | return dummy.next 118 | } 119 | 120 | //This function prints contents of linked list starting from the given node 121 | func printList(n *Node) { 122 | for n != nil { 123 | fmt.Print(n.data) 124 | fmt.Print(" ,") 125 | n = n.next 126 | } 127 | } 128 | 129 | func GetDataList(n *Node) []int { 130 | data := []int{} 131 | for n != nil { 132 | data = append(data, n.data) 133 | n = n.next 134 | } 135 | return data 136 | } 137 | 138 | func main() { 139 | //Start with the empty list 140 | res := New() 141 | a := New() 142 | b := New() 143 | 144 | Push(&a, 15) 145 | Push(&a, 10) 146 | Push(&a, 5) 147 | 148 | Push(&b, 20) 149 | Push(&b, 3) 150 | Push(&b, 2) 151 | 152 | //Remove duplicates from LinkedList 153 | res = SortedMerge(a, b) 154 | 155 | fmt.Println("Merged LinkedList is:") 156 | printList(res) 157 | } 158 | -------------------------------------------------------------------------------- /linked-list-merge-two-sorted/linked-list-merge-two-sorted.go_test.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import ( 11 | "reflect" 12 | "testing" 13 | ) 14 | 15 | func TestMergeTwoSortedNew(t *testing.T) { 16 | var testDatas = []struct { 17 | ArrayIn1 []int 18 | ArrayIn2 []int 19 | Out []int 20 | }{ 21 | {[]int{20, 4, 15, 85}, []int{6, 7}, []int{20, 4, 15, 85, -1, 6, 7}}, 22 | {[]int{10, 20, 30}, []int{5, 6}, []int{10, 20, 30, -1, 5, 6}}, 23 | {[]int{7}, []int{7}, []int{7, -1, 7}}, 24 | {[]int{}, []int{}, []int{-1}}, 25 | } 26 | 27 | for _, data := range testDatas { 28 | 29 | a := New() 30 | b := New() 31 | 32 | for i := 0; i < len(data.ArrayIn1); i++ { 33 | Push(&a, data.ArrayIn1[i]) 34 | } 35 | 36 | for i := 0; i < len(data.ArrayIn2); i++ { 37 | Push(&b, data.ArrayIn2[i]) 38 | } 39 | 40 | actual := SortedMerge(a, b) 41 | 42 | expected := New() 43 | 44 | for i := 0; i < len(data.Out); i++ { 45 | Push(&expected, data.Out[i]) 46 | } 47 | 48 | if !reflect.DeepEqual(actual, expected) { 49 | t.Errorf("MergeTwoSorted: Expected: %v, Actual: %v", GetDataList(expected), GetDataList(actual)) 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /linked-list-reverse/README.md: -------------------------------------------------------------------------------- 1 |

LinkedList Reverse Source

2 | 3 | [What It Is](#what-it-is) 4 | 5 | ## What It Is 6 | 7 | Given pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. 8 | 9 | 10 | > * Input: Head of following linked list `[1->2->3->4->NULL]` 11 | > * Output: Linked list should be changed to `[4->3->2->1->NULL]` 12 | 13 | > * Input: Head of following linked list `[1->2->3->4->5->NULL]` 14 | > * Output: Linked list should be changed to `[5->4->3->2->1->NULL]` 15 | 16 | **Algorithm Complexity** 17 | 18 | | Complexity | Notation | 19 | | ----------------- |:---------:| 20 | | `Time Complexity` | `O(n)` | 21 | | `Auxiliary Space` | `O(1)` | -------------------------------------------------------------------------------- /linked-list-reverse/linked-list-reverse.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | type Node struct { 13 | data int 14 | next *Node 15 | } 16 | 17 | //Returns an initialized list 18 | func (n *Node) Init() *Node { 19 | n.data = -1 20 | return n 21 | } 22 | 23 | //Returns an new list 24 | func New() *Node { 25 | return new(Node).Init() 26 | } 27 | 28 | //Returns the first node in list 29 | func (n *Node) Next() *Node { 30 | return n.next 31 | } 32 | 33 | //Returns the last node in list if exist, otherwise returns current 34 | func (n *Node) Back() *Node { 35 | current := n.next 36 | for current != nil && current.next != nil { 37 | current = current.next 38 | } 39 | return current 40 | } 41 | 42 | func Push(head_ref **Node, new_data int) { 43 | //new_node := Node{data: new_data, next: (*head_ref)} 44 | //*head_ref = new_node 45 | 46 | //1. Allocate new node 47 | new_node := New() 48 | 49 | //2. Put in the data 50 | new_node.data = new_data 51 | 52 | //3. Make next of new node as head 53 | new_node.next = (*head_ref) 54 | 55 | //4. Move the head to point to new_node 56 | *head_ref = new_node 57 | } 58 | 59 | func Reverse(head_ref **Node) { 60 | prev := New() 61 | current := *head_ref 62 | next := New() 63 | 64 | for(current != nil){ 65 | next = current.next 66 | current.next = prev 67 | prev = current 68 | current = next 69 | } 70 | 71 | *head_ref = prev 72 | } 73 | 74 | //This function prints contents of linked list starting from the given node 75 | func printList(n *Node){ 76 | for n != nil { 77 | fmt.Println(n.data) 78 | n = n.next 79 | } 80 | } 81 | 82 | func main() { 83 | //Start with the empty list 84 | head := New() 85 | 86 | Push(&head, 20) 87 | 88 | Push(&head, 4) 89 | 90 | Push(&head, 15) 91 | 92 | Push(&head, 85) 93 | 94 | fmt.Println("Given LinkedList is:") 95 | printList(head) 96 | 97 | Reverse(&head) 98 | 99 | fmt.Println("Reversed LinkedList is:") 100 | printList(head) 101 | } -------------------------------------------------------------------------------- /linked-list-reverse/linked-list-reverse_test.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import ( 11 | "reflect" 12 | "testing" 13 | ) 14 | 15 | func TestLinkedListReverse(t *testing.T) { 16 | var testDatas = []struct { 17 | Node *Node 18 | ArrayIn []int 19 | ArrayOut []int 20 | }{ 21 | {New(), []int{20, 4, 15, 85}, []int{-1, 20, 4, 15, 85, -1}}, 22 | {New(), []int{10, 20, 30, 40, 50}, []int{-1, 10, 20, 30, 40, 50, -1}}, 23 | {New(), []int{7}, []int{-1, 7, -1}}, 24 | } 25 | 26 | for _, data := range testDatas { 27 | for i := 0; i < len(data.ArrayIn); i++ { 28 | Push(&data.Node, data.ArrayIn[i]) 29 | } 30 | 31 | Reverse(&data.Node) 32 | 33 | n := data.Node 34 | i := 0 35 | for n != nil { 36 | expected := data.ArrayOut[i] 37 | actual := n.data 38 | 39 | if !reflect.DeepEqual(expected, actual) { 40 | t.Errorf("LinkedListReverse: Expected: %d, Actual: %d", expected, actual) 41 | } 42 | 43 | n = n.Next() 44 | i++ 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /merge-sort/README.md: -------------------------------------------------------------------------------- 1 |

Merge Sort Source

2 | 3 | [What It Is](#what-it-is) 4 | 5 | ## What It Is 6 | 7 | Like **[QuickSort](https://github.com/Dentrax/Data-Structures-with-Go/tree/master/quick-sort)**, `MergeSort` is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The `merge()` function is used for merging two halves. The `merge(arr, l, m, r)` is key process that assumes that `arr[l..m]` and `arr[m+1..r]` are sorted and merges the two sorted sub-arrays into one. See following C implementation for details 8 | 9 | ![Preview Thumbnail](https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif) 10 | 11 | MergeSort(arr[], l, r) 12 | -------------------------- 13 | 14 | * If r > l 15 | * 1. Find the middle point to divide the array into two halves: 16 | middle m = (l+r)/2 17 | * 2. Call mergeSort for first half: 18 | Call mergeSort(arr, l, m) 19 | * 3. Call mergeSort for second half: 20 | Call mergeSort(arr, m+1, r) 21 | * 4. Merge the two halves sorted in step 2 and 3: 22 | Call merge(arr, l, m, r) 23 | 24 | 25 | The following diagram from **[Wikipedia](https://en.wikipedia.org/wiki/File:Merge_sort_algorithm_diagram.svg)** shows the complete merge sort process for an example array {38, 27, 43, 3, 9, 82, 10}. If we take a closer look at the diagram, we can see that the array is recursively divided in two halves till the size becomes 1. Once the size becomes 1, the merge processes comes into action and starts merging arrays back till the complete array is merged 26 | 27 | ![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/merge-sort/resources/merge-sort.png) 28 | 29 | > * Input: {12, 11, 13, 5, 6, 7} 30 | > * Output: Given array is `12 11 13 5 6 7` 31 | > * Output: Sorted array is `5 6 7 11 12 13` 32 | 33 | **Algorithm Complexity** 34 | 35 | | Complexity | Notation | 36 | | ----------------- |:------------:| 37 | | `Time Complexity` | `O(n log n)` | -------------------------------------------------------------------------------- /merge-sort/merge-sort.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | func Merge(arr []int, l, m, r int) { 13 | var i, j, k int 14 | var n1 int = m - l + 1 15 | var n2 int = r - m 16 | 17 | //Create temp arrays 18 | L := make([]int, n1) 19 | R := make([]int, n2) 20 | 21 | //Copy data to temp arrays L[] and R[] 22 | for i = 0; i < n1; i++ { 23 | L[i] = arr[l+i] 24 | } 25 | 26 | for j = 0; j < n2; j++ { 27 | R[j] = arr[m+1+j] 28 | } 29 | 30 | //Merge the temp arrays back into arr[l..r] 31 | i = 0 //Initial index of first subarray 32 | j = 0 //Initial index of second subarray 33 | k = l //Initial index of merged subarray 34 | 35 | for i < n1 && j < n2 { 36 | if L[i] <= R[j] { 37 | arr[k] = L[i] 38 | i++ 39 | } else { 40 | arr[k] = R[j] 41 | j++ 42 | } 43 | k++ 44 | } 45 | 46 | //Copy the remaining elements of L[], if there are any 47 | for i < n1 { 48 | arr[k] = L[i] 49 | i++ 50 | k++ 51 | } 52 | 53 | //Copy the remaining elements of R[], if there are any 54 | for j < n2 { 55 | arr[k] = R[j] 56 | j++ 57 | k++ 58 | } 59 | } 60 | 61 | func MergeSort(arr []int, l, r int) { 62 | if l < r { 63 | //Same as (l + r) / 2 but avoids overflow for large l and h 64 | var m int = l + (r-l)/2 65 | 66 | //Sort first and second halves 67 | MergeSort(arr, l, m) 68 | MergeSort(arr, m+1, r) 69 | 70 | Merge(arr, l, m, r) 71 | } 72 | } 73 | 74 | func PrintArray(A []int, size int) { 75 | for i := 0; i < size; i++ { 76 | fmt.Printf("%d ", A[i]) 77 | } 78 | fmt.Printf("\n") 79 | } 80 | 81 | func main() { 82 | arr := []int{12, 11, 13, 5, 6, 7} 83 | var arr_size int = len(arr) 84 | 85 | fmt.Println("Given array size is: ") 86 | PrintArray(arr, arr_size) 87 | 88 | MergeSort(arr, 0, arr_size-1) 89 | 90 | fmt.Println("Sorted array is: ") 91 | PrintArray(arr, arr_size) 92 | } 93 | -------------------------------------------------------------------------------- /merge-sort/merge-sort_test.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import ( 11 | "reflect" 12 | "testing" 13 | ) 14 | 15 | func TestMergeSort(t *testing.T) { 16 | var testDatas = []struct { 17 | ArrayIn []int 18 | ArrayOut []int 19 | }{ 20 | {[]int{1, 3, 2, 4}, []int{1, 2, 3, 4}}, 21 | {[]int{3, 2, 1, 4}, []int{1, 2, 3, 4}}, 22 | {[]int{9, 8, 6, 5, 7, 4, 3, 0, 2, 1}, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}, 23 | {[]int{-3, -2, -1, -4, 0}, []int{-4, -3, -2, -1, 0}}, 24 | } 25 | for _, data := range testDatas { 26 | expected := data.ArrayOut 27 | MergeSort(data.ArrayIn, 0, len(data.ArrayIn)-1) 28 | actual := data.ArrayIn 29 | 30 | if !reflect.DeepEqual(expected, actual) { 31 | t.Errorf("MergeSort: Expected: %d, Actual: %d", expected, actual) 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /merge-sort/resources/merge-sort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/17ec0461bcf3eaa1152df8c178d5d751882c485d/merge-sort/resources/merge-sort.png -------------------------------------------------------------------------------- /quick-sort/README.md: -------------------------------------------------------------------------------- 1 |

Quick Sort Source

2 | 3 | [What It Is](#what-it-is) 4 | 5 | ## What It Is 6 | 7 | Like **[MergeSort](https://github.com/Dentrax/Data-Structures-with-Go/tree/master/merge-sort)**, `QuickSort` is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways. 8 | 9 | * Always pick first element as pivot. 10 | * Always pick last element as pivot (implemented below) 11 | * Pick a random element as pivot. 12 | * Pick median as pivot. 13 | 14 | The key process in quickSort is `partition()`. Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time. 15 | 16 | ![Preview Thumbnail](https://upload.wikimedia.org/wikipedia/commons/6/6a/Sorting_quicksort_anim.gif) 17 | 18 | QuickSort(arr[], start, end) 19 | -------------------------- 20 | 21 | **Partition Algorithm** 22 | 23 | There can be many ways to do partition, following pseudo code adopts the method given in CLRS book. The logic is simple, we start from the leftmost element and keep track of index of smaller (or equal to) elements as i. While traversing, if we find a smaller element, we swap current element with arr[i]. Otherwise we ignore current element. 24 | 25 | ![Preview Thumbnail](https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/master/quick-sort/resources/quick-sort.png) 26 | 27 | > * Input: {10, 7, 8, 9, 1, 5} 28 | > * Output: Sorted array is `1 5 7 8 9 10` 29 | 30 | **Algorithm Complexity** 31 | 32 | | Complexity | Notation | 33 | | ----------------- |:------------:| 34 | | `Time Complexity` | `O(n log n)` | 35 | | `Auxiliary Space` | `O(n)` | -------------------------------------------------------------------------------- /quick-sort/quick-sort.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | func Swap(a *int, b *int) { 13 | t := *a 14 | *a = *b 15 | *b = t 16 | } 17 | 18 | func Partition(arr []int, start, end int) int { 19 | 20 | pivot := arr[end] 21 | 22 | //Index of smaller element 23 | var i int = (start - 1) 24 | 25 | for j := start; j <= end-1; j++ { 26 | //If current element is smaller than or equal to pivot 27 | if arr[j] <= pivot { 28 | i++ 29 | Swap(&arr[i], &arr[j]) 30 | } 31 | } 32 | 33 | Swap(&arr[i+1], &arr[end]) 34 | 35 | return (i + 1) 36 | } 37 | 38 | /*The main function that implements QuickSort 39 | arr[] -> Array to be sorted 40 | start -> Starting index 41 | end -> Ending index 42 | */ 43 | func QuickSort(arr []int, start, end int) { 44 | if start < end { 45 | //pi is partitioning index, arr[p] is now at right place 46 | var pi int = Partition(arr, start, end) 47 | 48 | //Separately sort elements before partition and after partition 49 | QuickSort(arr, start, pi-1) 50 | QuickSort(arr, pi+1, end) 51 | } 52 | } 53 | 54 | func PrintArray(arr []int, size int) { 55 | for i := 0; i < size; i++ { 56 | fmt.Printf("%d ", arr[i]) 57 | } 58 | fmt.Printf("\n") 59 | } 60 | 61 | func main() { 62 | arr := []int{10, 7, 8, 9, 1, 5} 63 | var n int = len(arr) 64 | 65 | QuickSort(arr, 0, n-1) 66 | 67 | fmt.Println("Sorted array is: ") 68 | PrintArray(arr, n) 69 | } 70 | -------------------------------------------------------------------------------- /quick-sort/quick-sort_test.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import ( 11 | "reflect" 12 | "testing" 13 | ) 14 | 15 | func TestQuickSort(t *testing.T) { 16 | var testDatas = []struct { 17 | ArrayIn []int 18 | ArrayOut []int 19 | }{ 20 | {[]int{1, 3, 2, 4}, []int{1, 2, 3, 4}}, 21 | {[]int{3, 2, 1, 4}, []int{1, 2, 3, 4}}, 22 | {[]int{9, 8, 6, 5, 7, 4, 3, 0, 2, 1}, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}, 23 | {[]int{-3, -2, -1, -4, 0}, []int{-4, -3, -2, -1, 0}}, 24 | } 25 | for _, data := range testDatas { 26 | expected := data.ArrayOut 27 | QuickSort(data.ArrayIn, 0, len(data.ArrayIn)-1) 28 | actual := data.ArrayIn 29 | 30 | if !reflect.DeepEqual(expected, actual) { 31 | t.Errorf("QuickSort: Expected: %d, Actual: %d", expected, actual) 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /quick-sort/resources/quick-sort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dentrax/Data-Structures-with-Go/17ec0461bcf3eaa1152df8c178d5d751882c485d/quick-sort/resources/quick-sort.png -------------------------------------------------------------------------------- /stack/stack.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import "fmt" 11 | 12 | const MaxUint = ^uint(0) 13 | const MinUint = 0 14 | const MaxInt = int(MaxUint >> 1) 15 | const MinInt = -MaxInt - 1 16 | 17 | type Stack struct { 18 | top int 19 | capacity uint 20 | array []int 21 | } 22 | 23 | //Returns an initialized list 24 | func (s *Stack) Init(capacity uint) *Stack { 25 | s.top = -1 26 | s.capacity = capacity 27 | s.array = make([]int, capacity) 28 | return s 29 | } 30 | 31 | //Returns an new list 32 | func New(capacity uint) *Stack { 33 | return new(Stack).Init(capacity) 34 | } 35 | 36 | // Stack is full when top is equal to the last index 37 | func IsFull(stack *Stack) bool { 38 | return stack.top == int(stack.capacity)-1 39 | } 40 | 41 | // Stack is empty when top is equal to -1 42 | func IsEmpty(stack *Stack) bool { 43 | return stack.top == -1 44 | } 45 | 46 | func Push(stack *Stack, item int) { 47 | if IsFull(stack) { 48 | return 49 | } 50 | stack.top++ 51 | stack.array[stack.top] = item 52 | } 53 | 54 | func Pop(stack *Stack) int { 55 | if IsEmpty(stack) { 56 | return MinInt 57 | } 58 | temp := stack.array[stack.top] 59 | stack.top-- 60 | return temp 61 | } 62 | 63 | func main() { 64 | stack := New(100) 65 | 66 | Push(stack, 10) 67 | fmt.Println("Pushed to stack : 10") 68 | Push(stack, 20) 69 | fmt.Println("Pushed to stack : 20") 70 | Push(stack, 30) 71 | fmt.Println("Pushed to stack : 30") 72 | 73 | fmt.Printf("Popped from stack : %d", Pop(stack)) 74 | } 75 | -------------------------------------------------------------------------------- /stack/stack_test.go: -------------------------------------------------------------------------------- 1 | // ==================================================== 2 | // Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal 3 | // This program comes with ABSOLUTELY NO WARRANTY; This is free software, 4 | // and you are welcome to redistribute it under certain conditions; See 5 | // file LICENSE, which is part of this source code package, for details. 6 | // ==================================================== 7 | 8 | package main 9 | 10 | import ( 11 | "reflect" 12 | "testing" 13 | ) 14 | 15 | func TestStack(t *testing.T) { 16 | var testDatas = []struct { 17 | Stack *Stack 18 | ArrayIn []int 19 | ArrayOut []int 20 | }{ 21 | {New(3), []int{10, 20, 30}, []int{30, 20, 10}}, 22 | {New(5), []int{10, 20, 30, 40, 50}, []int{50, 40, 30, 20, 10}}, 23 | {New(1), []int{7}, []int{7}}, 24 | {New(5), []int{15, 10, 22, 33, 77}, []int{77, 33, 22, 10, 15}}, 25 | } 26 | 27 | for _, data := range testDatas { 28 | for i := uint(0); i < data.Stack.capacity; i++ { 29 | Push(data.Stack, data.ArrayIn[i]) 30 | } 31 | for i := uint(0); i < data.Stack.capacity; i++ { 32 | expected := data.ArrayOut[i] 33 | actual := Pop(data.Stack) 34 | 35 | if !reflect.DeepEqual(expected, actual) { 36 | t.Errorf("Stack: Expected: %d, Actual: %d", expected, actual) 37 | } 38 | } 39 | } 40 | } 41 | --------------------------------------------------------------------------------