Created
April 16, 2024 20:31
-
-
Save tkurtbond/394c0a678477ed1a7d8ce9bb73969996 to your computer and use it in GitHub Desktop.
Singly linked list implementation
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
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; | |
with Ada.Text_IO; use Ada.Text_IO; | |
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO; | |
procedure SLList is | |
function "+" (Source : in String) | |
return Unbounded_String renames To_Unbounded_String; | |
type Node; | |
type Node_Ref is access Node; | |
type Node is record | |
S: Unbounded_String; | |
Next: Node_Ref; | |
end record; | |
procedure Traverse (First : Node_Ref) is | |
P : Node_Ref := First; | |
begin | |
while P /= null loop | |
Put_Line (P.S); | |
P := P.Next; | |
end loop; | |
end Traverse; | |
procedure Insert (First : in out Node_Ref; New_Node : Node_Ref) is | |
P : Node_Ref; | |
begin | |
if First = null or else New_Node.S < First.S then | |
New_Node.Next := First; | |
First := New_Node; | |
else | |
P := First; | |
while P.Next /= null and then New_Node.S > P.Next.S loop | |
P := P.Next; | |
end loop; | |
New_Node.Next := P.Next; | |
p.Next := New_Node; | |
end if; | |
end Insert; | |
-- Build the list manually. | |
S3 : Node_Ref := new Node'(+"x", null); | |
S2 : Node_Ref := new Node'(+"m", S3); | |
S1 : Node_Ref := new Node'(+"a", S2); | |
-- Save a reference to the first item in the list. | |
First : Node_Ref := S1; | |
-- Create some other nodes to use later. | |
S4 : Node_Ref := new Node'(+"o", null); | |
S5 : Node_Ref := new Node'(+"z", null); | |
S6 : Node_Ref := new Node'(+"h", null); | |
begin | |
Put_Line (+"This is the Singly Linked List program."); | |
Traverse (First); | |
New_Line; | |
Insert (First, S4); | |
Insert (First, S5); | |
Insert (First, S6); | |
Traverse (First); | |
end SLList; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment