Array and Linked?List.

Array and Linked?List.

Array

  • Arrays have a fixed size meaning that you need to specify the array size when you declare it.
  • All elements in an array must be of the same data type.
  • All elements in an array can be accessed directly by their index.
  • All elements in an array are sorted in a contiguous memory location.


Linked List?:

  • Linked lists can dynamically grow or shrink in size during the execution of a program.
  • Nodes in a linked list can be scattered in different memory locations.
  • Linked lists only use as much memory as needed for the current elements.
  • Linked lists can easily accommodate elements of different data types.


Example linked list in Dart?

class Node {
  late int data;
  Node? next;

  Node(int data) {
    this.data = data;
    this.next = null;
  }
}

class LinkedList {
  Node? head;

  LinkedList() {
    this.head = null;
  }

  // Insert a new node at the end of the linked list
  void append(int data) {
    Node newNode = Node(data);

    if (head == null) {
      head = newNode;
      return;
    }

    Node current = head!;
    while (current.next != null) {
      current = current.next!;
    }

    current.next = newNode;
  }

  // Print the elements of the linked list
  void printList() {
    Node? current = head;
    while (current != null) {
      print(current.data);
      current = current.next;
    }
  }
}

void main() {
  LinkedList linkedList = LinkedList();

  // Appending elements to the linked list
  linkedList.append(1);
  linkedList.append(2);
  linkedList.append(3);

  // Printing the linked list
  print("Linked List:");
  linkedList.printList();
}        

要查看或添加评论,请登录

Eslam Elezaby的更多文章

  • Color conversion in Flutter

    Color conversion in Flutter

    ?? Excited to share a handy code snippet for color conversion in Flutter! ?? Have you ever needed to convert color…

  • Stlesswidget lifecycle.

    Stlesswidget lifecycle.

    In Flutter, StatelessWidget is a basic building block for creating user interfaces. Unlike StatefulWidget…

  • StatefulWidget lifecycle:

    StatefulWidget lifecycle:

    1- This method is called only once during the widget's lifetime. the framework calls the method of the associated to…

  • OOP in Brief

    OOP in Brief

    Class: A blueprint or template for creating objects defines the properties and behaviors common to all objects. Object:…

  • What is SOLID ?

    What is SOLID ?

    Single Responsibility: a class should have only one responsibility or job. Open/Closed: Software entities ( classes…

社区洞察

其他会员也浏览了