Skip to content

Commit

Permalink
Merge pull request emirpasic#148 from AryanAhadinia/issue_146
Browse files Browse the repository at this point in the history
Add Queue
  • Loading branch information
emirpasic authored Apr 13, 2022
2 parents 74d62f4 + 3c9a1ea commit 77ed4cc
Show file tree
Hide file tree
Showing 10 changed files with 1,024 additions and 0 deletions.
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Implementation of various data structures and algorithms in Go.
- [ArrayList](#arraylist)
- [SinglyLinkedList](#singlylinkedlist)
- [DoublyLinkedList](#doublylinkedlist)
- [Queues](#queues)
- [LinkedListQueue](#linkedlistqueue)
- [ArrayQueue](#arrayqueue)
- [Sets](#sets)
- [HashSet](#hashset)
- [TreeSet](#treeset)
Expand Down Expand Up @@ -76,6 +79,9 @@ Containers are either ordered or unordered. All ordered containers provide [stat
| | [ArrayList](#arraylist) | yes | yes* | yes | index |
| | [SinglyLinkedList](#singlylinkedlist) | yes | yes | yes | index |
| | [DoublyLinkedList](#doublylinkedlist) | yes | yes* | yes | index |
| [Queues](#queues) |
| | [LinkedListQueues](#linkedlistqueues) | yes | yes | no | index |
| | [ArrayQueues](#arrayqueues) | yes | yes* | no | index |
| [Sets](#sets) |
| | [HashSet](#hashset) | no | no | no | index |
| | [TreeSet](#treeset) | yes | yes* | yes | index |
Expand Down Expand Up @@ -416,6 +422,80 @@ func main() {
}
```

### Queues

A queue that represents a first-in-first-out (FIFO) data structure. The usual enqueue and dequeue operations are provided, as well as a method to peek at the first item in the queue.

Implements [Container](#containers) interface.

```go
type Queue interface {
Enqueue(value interface{})
Dequeue() (value interface{}, ok bool)
Peek() (value interface{}, ok bool)

containers.Container
// Empty() bool
// Size() int
// Clear()
// Values() []interface{}
}
```

#### LinkedListQueue

A [queue](#queues) based on a [linked list](#singlylinkedlist).

Implements [Queue](#queues), [IteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.

```go
package main

import ll1 "github.com/emirpasic/gods/stacks/linkedlistqueue"

func main() {
queue := llq.New() // empty
queue.Enqueue(1) // 1
queue.Enqueue(2) // 1, 2
queue.Values() // 1, 2
_, _ = queue.Peek() // 1, true
_, _ = queue.Dequeue() // 1, true
_, _ = queue.Dequeue() // 2, true
_, _ = queue.Dequeue() // nil, false
queue.Enqueue(1) // 1
queue.Clear() // empty
queue.Empty() // true
queue.Size() // 0
}
```

#### ArrayQueue

A [queue](#queues) based on a [array list](#arraylist).

Implements [Queue](#queues), [IteratorWithIndex](#iteratorwithindex), [JSONSerializer](#jsonserializer) and [JSONDeserializer](#jsondeserializer) interfaces.

```go
package main

import ll1 "github.com/emirpasic/gods/stacks/linkedlistqueue"

func main() {
queue := llq.New() // empty
queue.Enqueue(1) // 1
queue.Enqueue(2) // 1, 2
queue.Values() // 1, 2
_, _ = queue.Peek() // 1, true
_, _ = queue.Dequeue() // 1, true
_, _ = queue.Dequeue() // 2, true
_, _ = queue.Dequeue() // nil, false
queue.Enqueue(1) // 1
queue.Clear() // empty
queue.Empty() // true
queue.Size() // 0
}
```

### Maps

A Map is a data structure that maps keys to values. A map cannot contain duplicate keys and each key can map to at most one value.
Expand Down
87 changes: 87 additions & 0 deletions queues/arrayqueue/arrayqueue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2021, Aryan Ahadinia. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package arrayqueue implements a queue backed by a array-list.
//
// Structure is not thread safe.
//
// Reference: https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
package arrayqueue

import (
"fmt"
"strings"

"github.com/emirpasic/gods/lists/arraylist"
"github.com/emirpasic/gods/queues"
)

func assertQueueImplementation() {
var _ queues.Queue = (*Queue)(nil)
}

// Queue holds elements in an array-list
type Queue struct {
list *arraylist.List
}

// New instantiates a new empty queue
func New() *Queue {
return &Queue{list: arraylist.New()}
}

// Enqueue adds a value to the end of the queue
func (queue *Queue) Enqueue(value interface{}) {
queue.list.Add(value)
}

// Dequeue removes first element of the queue and returns it, or nil if queue is empty.
// Second return parameter is true, unless the queue was empty and there was nothing to dequeue.
func (queue *Queue) Dequeue() (value interface{}, ok bool) {
value, ok = queue.list.Get(0)
queue.list.Remove(0)
return
}

// Peek returns first element of the queue without removing it, or nil if queue is empty.
// Second return parameter is true, unless the queue was empty and there was nothing to peek.
func (queue *Queue) Peek() (value interface{}, ok bool) {
return queue.list.Get(0)
}

// Empty returns true if queue does not contain any elements.
func (queue *Queue) Empty() bool {
return queue.list.Empty()
}

// Size returns number of elements within the queue.
func (queue *Queue) Size() int {
return queue.list.Size()
}

// Clear removes all elements from the queue.
func (queue *Queue) Clear() {
queue.list.Clear()
}

// Values returns all elements in the queue (FIFO order).
func (queue *Queue) Values() []interface{} {
return queue.list.Values()
}

// String returns a string representation of container
func (queue *Queue) String() string {
str := "ArrayQueue\n"
values := []string{}
for _, value := range queue.list.Values() {
values = append(values, fmt.Sprintf("%v", value))
}
str += strings.Join(values, ", ")
return str
}

// Check that the index is within bounds of the list
func (queue *Queue) withinRange(index int) bool {
return index >= 0 && index < queue.list.Size()
}
Loading

0 comments on commit 77ed4cc

Please sign in to comment.