Skip to content

Commit

Permalink
Merge pull request #6 from kaiysh/master
Browse files Browse the repository at this point in the history
fixed SliceQueue
  • Loading branch information
kokizzu committed Sep 17, 2014
2 parents f8cfa34 + c13307f commit 22725e2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ ChangeLog:
* 2014-09-13 added singly linked list (Dikaimin)
* 2014-09-14 fixed InsertLast() and Print() method in singly linked list, added doubly linked list (Dikaimin)
* 2014-09-16 added queue (Rizal)
* 2014-09-16 fixed SliceQueue (Dikaimin)

TODO:
=====
* fix SliceQueue so it can have unlimited number of length
* fix SliceQueue so we won't need to store the Front (always zero) (use slice[1:] after dequeue)
* add fmt.Println("test name") on main on SinglyLinkedList and DoublyLinkedList
* add big Oh on each function
56 changes: 25 additions & 31 deletions SliceQueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,57 @@ type Any interface{}

type Queue struct {
data []Any
head, tail, count int
size int
}

func NewQueue(size int) *Queue {
// Create a new queue: O(1)
func NewQueue() *Queue {
var q Queue
q.data = make([]Any,size)
q.head, q.tail, q.count = 0, 0, 0
q.size = size
q.data = make([]Any,0,0)
return &q
}

// Return size: O(1)
func (q *Queue) Count() int {
return q.count
return len(q.data)
}

// Text whether queue is empty: O(1)
func (q *Queue) IsEmpty() bool {
if q.tail == 0 { return true }
if len(q.data) == 0 { return true }
return false
}

// Access first element: O(1)
func (q *Queue) Front() Any {
return q.data[q.head]
if len(q.data) == 0 { return nil }
return q.data[0]
}

// Access last element: O(1)
func (q *Queue) Back() Any {
return q.data[q.tail-1]
if len(q.data) == 0 { return nil }
return q.data[len(q.data)-1]
}

// Insert new element into the end of queue: O(1)
func (q *Queue) enqueue(data Any) {
if q.count < q.size {
q.data[q.tail] = data
q.tail = (q.tail+1)%q.size
q.count++
q.size++
}
q.data = append(q.data,data)
}

// Remove first element from queue: O(1)
func (q *Queue) dequeue() Any {
if q.count != 0 {
front := q.data[q.head]
q.head = (q.head+1)%q.size
q.count--
return front
}
return nil
if len(q.data) == 0 { return nil }
data := q.data[0]
q.data = q.data[1:]
return data
}

// Print first N elements from queue: O(N)
func (q *Queue) PrintN(n int) {
if (q.count == 0) { fmt.Println("NIL"); return }
if len(q.data) == 0 { fmt.Println("| Empty |"); return }
fmt.Print("|")
in := q.head
for i := 0; i < n; i++ {
fmt.Printf(" %d: %#v |", i, q.data[in])
in = (in+1)%q.size
if in == q.head { break }
for i := 0; (i < len(q.data)) && (i < n); i++ {
fmt.Printf(" %d: %#v |", i, q.data[i])
}
fmt.Println()
}
Expand All @@ -79,7 +74,7 @@ func RandInt(min, max int) int {
}

func main() {
q := NewQueue(100)
q := NewQueue()
fmt.Println("Enqueue Numbers")
for i := 0; i < 6; i++ {
a := RandInt(0,100)
Expand All @@ -102,5 +97,4 @@ func main() {
if i == 6 { fmt.Println() } else { fmt.Print(" ") }
}
q.PrintN(q.Count())

}

0 comments on commit 22725e2

Please sign in to comment.