What Is Linear and Non-Linear Data Structure With Example?

//

Larry Thompson

What Is Linear and Non-Linear Data Structure With Example?

In the world of computer science and programming, data structures play a vital role in organizing and storing data efficiently. Two common types of data structures are linear and non-linear structures.

In this article, we will explore what these structures are and provide examples to help you understand their functionalities.

Linear Data Structures

As the name suggests, linear data structures represent a sequential arrangement of elements where each element is connected to its previous and next element (except for the first and last elements). These structures follow a linear order or sequence.

1. Arrays:

One of the most basic and widely used linear data structures is an array. It is a collection of elements of the same type that are stored in contiguous memory locations.

Elements in an array can be accessed using their index value, making it easy to retrieve or modify specific elements.

Example:


<script>
    var fruits = ["apple", "banana", "orange", "mango"];
    console.log(fruits[2]);
</script>

This example demonstrates how an array stores multiple values (fruits) and retrieves the value at index 2 (orange) using JavaScript.

2. Linked Lists:

A linked list is another linear data structure where each element, known as a node, contains both the data and a reference to the next node in the sequence. Unlike arrays, linked lists do not require contiguous memory allocation.

Example:


<script>
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

var node1 = new Node("apple");
var node2 = new Node("banana");
var node3 = new Node("orange");
node1.next = node2;
node2.next = node3;

console.log(node1.data);
console.next.data);
</script>

This example demonstrates how linked lists connect nodes together, allowing you to access the data of each node by traversing through the list.

Non-Linear Data Structures

Non-linear data structures do not follow a sequential arrangement like linear structures. They allow for more complex relationships between elements, often representing hierarchies or interconnectedness.

1. Trees:

A tree is a non-linear data structure that represents a hierarchical structure. It consists of nodes connected by edges, forming a branching structure similar to an upside-down tree. The topmost node is called the root, and each node can have child nodes.


<script>
class TreeNode {
    constructor(data) {
        this.children = [];
    }

    addChild(child) {
        this.children.push(child);
    }
}

var rootNode = new TreeNode("Electronics");

var laptopNode = new TreeNode("Laptops");
rootNode.addChild(laptopNode);

var phoneNode = new TreeNode("Phones");
rootNode.addChild(phoneNode);

console.log(rootNode.children[0].data);
</script>

This example demonstrates how a tree structure is used to represent categories of electronics, with the root node being “Electronics” and child nodes representing different types of devices. Graphs:

A graph is a non-linear data structure that represents a collection of interconnected nodes or vertices. These connections are known as edges. Graphs are widely used in various applications, such as social networks and transportation systems.


<script>
class Graph {
    constructor() {
        this.vertices = [];
        this.edges = [];
    }

    addVertex(vertex) {
        this.vertices.push(vertex);
    }

    addEdge(fromVertex, toVertex) {
        this.edges.push([fromVertex, toVertex]);
    }
}

var myGraph = new Graph();

myGraph.addVertex("A");
myGraph.addVertex("B");
myGraph.addVertex("C");

myGraph.addEdge("A", "B");
myGraph.addEdge("B", "C");

console.log(myGraph.vertices);
console.edges);
</script>

This example demonstrates how a graph is created with vertices and edges. It allows you to represent relationships between various entities.

In conclusion, understanding linear and non-linear data structures is crucial for efficient data management in programming. Whether you need to store data sequentially or represent complex relationships, choosing the appropriate data structure can significantly impact the performance and functionality of your programs.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy