Keep Calm and Code in Delphi - #3

Keep Calm and Code in Delphi - #3

💡 Linked Lists

Linked Lists are one of the most powerful and flexible data structures, widely used to implement queues, stacks, or even in systems that require dynamic data manipulation.

Why use linked lists?

Efficient Insertion/Removal: Unlike arrays, linked lists allow quick insertions and deletions at any position without requiring the movement of elements.

Dynamic Memory Allocation: Linked lists grow as needed, making them a more efficient option compared to fixed-size arrays.

Implementing a Simple List:

type
  TNode = class
  public
    Data: Integer;
    Next: TNode;
    constructor Create( AData: Integer );
    procedure AddNode( AData: Integer );
    procedure PrintList;
end;

constructor TNode.Create( AData: Integer );
begin
  Data := AData;
  Next := nil;
end;

procedure TNode.AddNode( AData: Integer );
var
  NewNode: TNode;
begin
  NewNode := TNode.Create( AData ); // Creates a new node
  Self.Next := NewNode; // Links the new node to the current one
end;

procedure TNode.PrintList;
var
  Temp: TNode;
begin
  Temp := Self;
   while Temp <> nil do
   begin
     Writeln( Temp.Data );
     Temp := Temp.Next;
   end;
end;        

Example Usage:

var
  Head, Tail: TNode;

begin
  Head := TNode.Create(11); 
  Tail := Head; // Tail starts by pointing to the first node

  Tail.AddNode(18); 
  Tail := Tail.Next; // Update Tail to point to the second node

  Tail.AddNode(24);
  Tail := Tail.Next; // Update Tail to point to the third node

  Head.PrintList;
end.        

🔥Advantages:

🔹 Ease of Manipulation: Allows easy insertion and removal of elements at any position in the list.

🔹 Dynamic Structure: No need to define the size of the list in advance; memory is allocated as required.

🔹 Flexibility: Can be used to implement other structures, such as queues, stacks, and even graphs!

👉 Tip: While Delphi’s TList class is excellent for most use cases, understanding the mechanics of linked lists is invaluable for optimizing resources and gaining deeper insights into dynamic memory management!

♻️ Share this knowledge!

#Delphi #DelphiDevelopers #DataStructures #LinkedList #Programming #DelphiTips

Jackson Gomes

Senior Software Engineer / Senior Delphi Developer

5mo

Caracoles... me lembrou dos tempos de universidade. Fazia tempo que não via uma parada dessas 😅

To view or add a comment, sign in

More articles by Leandro Siqueira

  • Transforming Models for Real-World Impact

    As an AI Specialist and Technical Trainer, I'm excited to share techniques that empower tech learners to build better…

  • Unlocking Development Challenges with the '5 Whys' Framework

    Every developer, whether working on AI, web applications, or enterprise systems, faces roadblocks. But before…

    2 Comments
  • From Chaotic If-Else to Elegant, Maintainable Class Structure

    In the world of software development, there’s one concept that often differentiates novice developers from seasoned…

  • Cohesion

    When diving into object-oriented programming (OOP), you’ve likely encountered the term "cohesion" at some point. And…

  • The Open/Closed Principle

    🔥 𝖳𝗁𝖾 𝖮𝗉𝖾𝗇/𝖢𝗅𝗈𝗌𝖾𝖽 𝖯𝗋𝗂𝗇𝖼𝗂𝗉𝗅𝖾 (𝖮𝖢𝖯) 𝗂𝗌 𝗈𝗇𝖾 𝗈𝖿 𝗍𝗁𝖾 𝖿𝗈𝗎𝗇𝖽𝖺𝗍𝗂𝗈𝗇𝖺𝗅…

  • The Single Responsibility Principle

    🔥C𝗈𝖽𝖾 𝖼𝗅𝖺𝗋𝗂𝗍𝗒 𝖺𝗇𝖽 𝗆𝖺𝗂𝗇𝗍𝖺𝗂𝗇𝖺𝖻𝗂𝗅𝗂𝗍𝗒 𝖺𝗋𝖾 𝖼𝗋𝗎𝖼𝗂𝖺𝗅 𝖿𝗈𝗋 𝗍𝗁𝖾 𝗌𝗎𝖼𝖼𝖾𝗌𝗌 𝗈𝖿…

  • Keep Calm and Code In Delphi #4

    💡Enumerated Types Enumerated types (or simply "enums") allow you to create ordered sets of predefined values and are…

Insights from the community

Others also viewed

Explore topics