I'm working on my first Ada program, just a simple priority Queue, but I can't seem to get it working. Here's what I've got so far: (I put it in C Quotes just so that the post wouldn't be so large, seeing as how there's no ada quotes)
Here's my PriorityQueue.abs file:
-- PriorityQueue.ads
-- Priority Queue Specification (Functions accessible by users)
package PriorityQueue is
function Push(Item, Priority : Integer) return Boolean;
function Pop(Garbage : Integer) return Integer;
end PriorityQueue;
This file compiles fine
Here's my PriorityQueue.adb file:
-- PriorityQueue.adb
-- Priority Queue Implementation
package body PriorityQueue is
-- Holds a Number and its Priority
Type ItemType is
record
Number, Priority : Integer := -1;
end record;
type Item is array (1..100) of ItemType;
begin
-- Pushes an Item into the Priority Queue
function Push(Number, Priority : Integer) is -- Line 15
Index, ItemSpot : Integer := 0;
begin
-- Make sure Priority is valid
if Priority < 0 then
Priority := 0;
end if;
-- Find where to insert the new Item
Index := 0;
loop
exit when Index = 101; -- Break out of loop if the Queue is full
exit when Priority <= Item(Index).Priority; -- Break out of loop if we found where to insert Item
exit when Item(Index).Priority = -1; -- Break out of loop if we found an empty spot
Index := Index + 1;
end loop;
if Index = 101 then
return false;
end if;
-- Save the spot to insert the new item in ItemSpot
ItemSpot := Index;
-- Move all Items above ItemSpot up one
-- Start from Index = 100 and go down to Index = 1, unless an "exit" is reached
for Index in 100..1 loop
exit when Index = ItemSpot; -- Exit loop once we've reached ItemSpot
Item(Index) := Item(Index - 1); -- Move Item up one spot in array
end loop;
-- Insert new Number and Priority
Item(ItemSpot).Number := Number;
Item(ItemSpot).Priority := Priority;
return true;
end Push;
function Pop(Garbage : Integer) return Integer is
Index, ItemNumber : Integer := 0;
begin
-- Save Number with the lowest priority number to be returned
ItemNumber := Item(0).Number;
-- Move all Items down one spot
for Index in 100..1 loop
Item(Index) := Item(Index-1);
end loop;
-- Return the Number
return ItemNumber;
end Pop;
end PriorityQueue; -- Line 70
And here are the compile errors I get while compiling PriorityQueue.adb:
priorityqueue.adb:15:04: declarations must come before begin
priorityqueue.adb:70:01: statement expected
And here is my main file First.adb:
-- Include text_io for input/output
with text_io, PriorityQueue;
use text_io, PriorityQueue;
-- Declare "Main"
procedure First is
Index : Integer := 0;
Queue : PriorityQueue; -- Line 8
begin
Queue.Push(31, 3); -- Line 11
Queue.Push(4, 4);
Queue.Push(0, 0);
Queue.Push(51, 5);
Queue.Push(52, 5);
Queue.Push(7, 7);
Queue.Push(1, 1);
Queue.Push(6, 6);
Queue.Push(2, 2);
Queue.Push(32, 3);
for Index in 1..10 loop
put(Queue.Pop(Index) + "is popped off the queue");
new_line;
end loop;
end First;
And here's the compile errors I get with First.adb:
First.adb:8:09: subtype mark required in this context
First.adb:8:09: found "PriorityQueue" in priorityqueue.adb
First.adb:11:03: invalid prefix in selected component "Queue" - I get one of these for each of the .Push functions.
From what I understand, the .abs is like the .h in C++, and the .adb is like the .cpp, is this right? Also, can you spot some of the mistakes in my code. Thanks.
[Edited by - deadlydog on October 8, 2004 11:22:35 PM]