Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix circular queue name #105

Merged
merged 2 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Welcome to **Data Structures and Algorithms in Go**! 🎉 This project is design
* [Longest Valid Parentheses](./stack/longest_valid_parentheses_test.go)
* [Queues](./queue/README.md)
* [A Queue Using Stacks](./queue/queue_using_stacks_test.go)
* [Implement a Queue Using a Circular Array](./queue/queue_using_circular_array_test.go)
* [Implement a Circular Queue Array](./circular_queue_using_array_test.go)
* [Is Binary Tree Symmetrical](./queue/is_tree_symmetrical_test.go)
* [Generate Binary Numbers](./queue/generate_binary_numbers_test.go)
* [Find The Maximum Sub-array of Length K](./queue/maximum_of_sub_arrays_test.go)
Expand Down
10 changes: 5 additions & 5 deletions queue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ Queues are widely utilized in solving graph-related problems and managing capaci

## Rehearsal

* [A Queue Using Stacks](queue_using_stacks_test.go), [Solution](queue_using_stacks.go)
* [Implement a Queue Using a Circular Array](queue_using_circular_array_test.go), [Solution](queue_using_circular_array.go)
* [Is Binary Tree Symmetrical](is_tree_symmetrical_test.go), [Solution](is_tree_symmetrical.go)
* [Generate Binary Numbers](generate_binary_numbers_test.go), [Solution](generate_binary_numbers.go)
* [Find The Maximum Sub-array of Length K](maximum_of_sub_arrays_test.go), [Solution](maximum_of_sub_arrays.go)
* [A Queue Using Stacks](./queue_using_stacks_test.go), [Solution](./queue_using_stacks.go)
* [Implement a Circular Queue Array](./circular_queue_using_array_test.go), [Solution](./circular_queue_using_array.go)
* [Is Binary Tree Symmetrical](./is_tree_symmetrical_test.go), [Solution](./is_tree_symmetrical.go)
* [Generate Binary Numbers](./generate_binary_numbers_test.go), [Solution](./generate_binary_numbers.go)
* [Find The Maximum Sub-array of Length K](./maximum_of_sub_arrays_test.go), [Solution](./maximum_of_sub_arrays.go)
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package queue

import "errors"

// UsingCircularArray is a queue that is made using a circular array.
type UsingCircularArray struct {
circular []int
size int
front int
rear int
// CircularQueue is a queue that is made using a circular array.
type CircularQueue struct {
data []int
size int
front int
rear int
}

const emptyValue = 0
Expand All @@ -16,22 +16,22 @@ const emptyValue = 0
var ErrQueueAtMaxCapacity = errors.New("queue is at max capacity")

// NewCircularQueue returns a fixed size circular queue.
func NewCircularQueue(size int) *UsingCircularArray {
func NewCircularQueue(size int) *CircularQueue {
circular := make([]int, size)
for i := range circular {
circular[i] = emptyValue
}

return &UsingCircularArray{
circular: circular,
rear: -1,
size: 0,
front: 0,
return &CircularQueue{
data: circular,
rear: -1,
size: 0,
front: 0,
}
}

// enqueue solves the problem in O(1) time and O(1) space.
func (queue *UsingCircularArray) enqueue(n int) error {
func (queue *CircularQueue) enqueue(n int) error {
if queue.isFull() {
return ErrQueueAtMaxCapacity
}
Expand All @@ -41,15 +41,15 @@ func (queue *UsingCircularArray) enqueue(n int) error {
queue.rear = 0
}

queue.circular[queue.rear] = n
queue.data[queue.rear] = n
queue.size++
return nil
}

// dequeue solves the problem in O(1) time and O(1) space.
func (queue *UsingCircularArray) dequeue() (int, error) {
tmp := queue.circular[queue.front]
queue.circular[queue.front] = emptyValue
func (queue *CircularQueue) dequeue() (int, error) {
tmp := queue.data[queue.front]
queue.data[queue.front] = emptyValue
queue.front++
if queue.front == queue.capacity() {
queue.front = 0
Expand All @@ -58,10 +58,10 @@ func (queue *UsingCircularArray) dequeue() (int, error) {
return tmp, nil
}

func (queue *UsingCircularArray) isFull() bool {
func (queue *CircularQueue) isFull() bool {
return queue.size == queue.capacity()
}

func (queue *UsingCircularArray) capacity() int {
return len(queue.circular)
func (queue *CircularQueue) capacity() int {
return len(queue.data)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,15 @@ package queue

import "testing"

type testCase struct {
enqueue []int
dequeueTimes int
expectedLastDequeuedItem int
expectEnqueueErr bool
expectDequeueErr bool
}

const (
operationTypeEnqueue = iota
operationTypeDequeue
)

var queueOperations = []struct {
operationType int
operationValue int
}{
{operationTypeEnqueue, 3},
{operationTypeEnqueue, 2},
{operationTypeEnqueue, 1},
{operationTypeDequeue, 0},
{operationTypeDequeue, 0},
{operationTypeDequeue, 0},
{operationTypeEnqueue, 3},
{operationTypeEnqueue, 2},
{operationTypeEnqueue, 1},
{operationTypeDequeue, 0},
{operationTypeDequeue, 0},
{operationTypeDequeue, 0},
{operationTypeEnqueue, 5},
{operationTypeDequeue, 0},
{operationTypeEnqueue, 1},
{operationTypeDequeue, 0},
{operationTypeEnqueue, 1},
}

/*
TestCircularQueue tests solution(s) with the following signature and problem description:

func (queue *UsingCircularArray) enqueue(n int) error
func (queue *UsingCircularArray) dequeue() (int, error)

Implements a queue using a circular array.
Given a size, implement a circular queue using an array.
A circular queue also called a ring buffer is different from a normal queue in that
the last element is connected to the first element.
*/
func TestCircularQueue(t *testing.T) {
// Tests a queue by enqueues given items,
Expand Down Expand Up @@ -76,7 +42,43 @@ func TestCircularQueue(t *testing.T) {
}
}

func enqueueDequeueAndCheckValue(t *testing.T, queue *UsingCircularArray, testID int, test *testCase) {
type testCase struct {
enqueue []int
dequeueTimes int
expectedLastDequeuedItem int
expectEnqueueErr bool
expectDequeueErr bool
}

const (
operationTypeEnqueue = iota
operationTypeDequeue
)

var queueOperations = []struct {
operationType int
operationValue int
}{
{operationTypeEnqueue, 3},
{operationTypeEnqueue, 2},
{operationTypeEnqueue, 1},
{operationTypeDequeue, 0},
{operationTypeDequeue, 0},
{operationTypeDequeue, 0},
{operationTypeEnqueue, 3},
{operationTypeEnqueue, 2},
{operationTypeEnqueue, 1},
{operationTypeDequeue, 0},
{operationTypeDequeue, 0},
{operationTypeDequeue, 0},
{operationTypeEnqueue, 5},
{operationTypeDequeue, 0},
{operationTypeEnqueue, 1},
{operationTypeDequeue, 0},
{operationTypeEnqueue, 1},
}

func enqueueDequeueAndCheckValue(t *testing.T, queue *CircularQueue, testID int, test *testCase) {
t.Helper()
for _, n := range test.enqueue {
if err := queue.enqueue(n); err != nil {
Expand Down
Loading