From e0dd50831fd19e1145897de8af909002c2821376 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 7 Dec 2019 17:27:41 +0100 Subject: Basic Queue package --- queue.adb | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'queue.adb') diff --git a/queue.adb b/queue.adb index e69de29..2869984 100644 --- a/queue.adb +++ b/queue.adb @@ -0,0 +1,50 @@ +package body Queue is + + procedure Enqueue(queue: in out T_Queue; data: T_Data) is + element: T_List_Node; + new_back: T_List; + begin + element.data := data; + element.next := null; + new_back := new T_List_Node'(element); + + if Empty(queue) then + queue.front := new_back; + queue.back := new_back; + return; + end if; + + queue.back.next := new_back; + queue.back := new_back; + end Enqueue; + + procedure Dequeue(queue: in out T_Queue) is + begin + if Empty(queue) then + return; + end if; + if queue.front.next = null then + queue.front := null; + queue.back := null; + return; + end if; + queue.front := queue.front.next; + end Dequeue; + + function Length(queue: T_Queue) return Natural is + tmp: T_List := queue.front; + i: Natural := 0; + begin + while tmp /= null loop + tmp := tmp.next; + i := i + 1; + end loop; + return i; + end Length; + + function Empty(queue: T_Queue) return Boolean is + begin + return queue.front = null and queue.back = null; + end Empty; + +end Queue; -- cgit