Created
December 15, 2022 23:22
-
-
Save abdulrahmanAlotaibi/6191f007d14f8111e8b26c3fbadc4381 to your computer and use it in GitHub Desktop.
Queue ADT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type node struct { | |
data int | |
next *node | |
} | |
type queue struct { | |
head *node | |
tail *node | |
size int | |
} | |
func main(){ | |
q:= queue{nil,nil,10} | |
fmt.Println(q) | |
q.enqueue(10) | |
q.enqueue(20) | |
q.enqueue(0) | |
fmt.Println(*q.find(10)) | |
q.display() | |
} | |
func (q *queue) enqueue(data int){ | |
n:= node{data, nil} | |
if q.head == nil { | |
q.head = &n | |
q.tail = &n | |
}else { | |
q.tail.next = &n | |
q.tail = q.tail.next | |
} | |
q.size++ | |
} | |
func (q *queue) dequeue () int { | |
data := q.head.data | |
q.head = q.head.next | |
q.size-- | |
if q.size == 0 { | |
q.tail = nil | |
} | |
return data | |
} | |
func (q *queue) find(data int) *node{ | |
tmp := q.head | |
for tmp != nil { | |
if tmp.data == data { | |
return tmp | |
} | |
tmp = tmp.next | |
} | |
return nil | |
} | |
func (q *queue) display(){ | |
tmp := q.head | |
for tmp.next != nil { | |
fmt.Print(tmp.data ," --> ") | |
tmp = tmp.next | |
} | |
if &tmp != nil { | |
fmt.Print(tmp.data ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment