queue

package
v0.12.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package queue is used to demonstrate various implementations of a queue data structure.

SliceQueue: uses an implementation that utilizes a slice as the underlying data Type.

Example
package main

import (
	"fmt"

	"github.com/eng618/go-eng/ds/queue"
)

func main() {
	q := queue.SliceQueue{}

	q.Enqueue("first")
	q.Enqueue("second")

	dq, err := q.Dequeue()
	if err != nil {
		fmt.Println("Dequeue err:", err)
	}

	fmt.Println("Just dequeued:", dq)

	fmt.Println("Current length is:", q.Length())
	q.Print()

}
Output:

Just dequeued: first
Current length is: 1
second

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LinkedQueue added in v0.5.0

type LinkedQueue struct {
	// contains filtered or unexported fields
}

LinkedQueue is a structure used to interact with the queue package.

func (*LinkedQueue) Dequeue added in v0.5.0

func (q *LinkedQueue) Dequeue() (value interface{}, err error)

Dequeue is a method to get the next item in a LinkedQueue.

func (*LinkedQueue) Enqueue added in v0.5.0

func (q *LinkedQueue) Enqueue(value interface{})

Enqueue is a method to add an item to a LinkedQueue.

func (*LinkedQueue) Length added in v0.5.0

func (q *LinkedQueue) Length() int

Length returns the current length of a LinkedQueue.

func (*LinkedQueue) Peek added in v0.6.0

func (q *LinkedQueue) Peek() (value interface{}, err error)

Peek shows the head or next item in the queue.

type SliceQueue

type SliceQueue struct {
	// contains filtered or unexported fields
}

SliceQueue is the structure to used to create a new queue. Once you have a SliceQueue, you can begin to use all the methods associated with it.

func (*SliceQueue) Dequeue

func (q *SliceQueue) Dequeue() (v interface{}, err error)

Dequeue is a method to get the next item in a SliceQueue.

Example
package main

import (
	"fmt"

	"github.com/eng618/go-eng/ds/queue"
)

func main() {
	q := queue.SliceQueue{}

	q.Enqueue("first")
	q.Enqueue("second")
	q.Enqueue("third")

	first, err := q.Dequeue()
	if err != nil {
		fmt.Println("Error:", err)
	}

	fmt.Println(first)

	second, err := q.Dequeue()
	if err != nil {
		fmt.Println("Error:", err)
	}

	fmt.Println(second)

	third, err := q.Dequeue()
	if err != nil {
		fmt.Println("Error:", err)
	}

	fmt.Println(third)

	fourth, err := q.Dequeue()
	if err != nil {
		fmt.Println("Error:", err)
	}

	fmt.Println(fourth)

	q.Print()

}
Output:

first
second
third
Error: attempted to dequeue on an empty queue
<nil>
Queue is empty

func (*SliceQueue) Enqueue

func (q *SliceQueue) Enqueue(v interface{}) (ok bool)

Enqueue is a method to add an item to a SliceQueue.

Example
package main

import (
	"github.com/eng618/go-eng/ds/queue"
)

func main() {
	q := queue.SliceQueue{}

	q.Enqueue(1)
	q.Enqueue(2)
	q.Enqueue("hello")
	q.Enqueue(true)

	q.Print()

}
Output:

1
2
hello
true

func (*SliceQueue) Length

func (q *SliceQueue) Length() int

Length returns the current length of a SliceQueue.

func (*SliceQueue) Peek added in v0.6.0

func (q *SliceQueue) Peek() (value interface{}, err error)

Peek shows the head or next item in the queue.

Example
package main

import (
	"fmt"

	"github.com/eng618/go-eng/ds/queue"
)

func main() {
	q := &queue.SliceQueue{}

	q.Enqueue("boo")
	q.Enqueue("who?")

	if val, err := q.Peek(); err != nil {
		fmt.Println("That wasn't scary...err:", err)
	} else {
		fmt.Println(val, "AHHHH!!!!!!")
	}

}
Output:

boo AHHHH!!!!!!

func (*SliceQueue) Print

func (q *SliceQueue) Print()

Print allows you to print all the items in a SliceQueue to the screen.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL