Data structure

## What is data structure ?

A **data structure** is a way of **organizing** and **storing**** data** in a computer so that it can be accessed and modified efficiently. **Different types of data structures** are suited to different kinds of applications and have different advantages and disadvantages. Some common types of data structures include:

**Arrays:**An array is a collection of items stored at contiguous memory locations. The items can be accessed by their index in the array. Arrays are efficient for accessing individual elements, but they are not as efficient for inserting or deleting elements.**Linked Lists:**A linked list is a collection of items in which each item contains a reference to the next item. Linked lists are efficient for inserting and deleting elements, but they are not as efficient for accessing individual elements.**Stack:**A stack is a collection of elements, with two main operations: push and pop. Elements can be added to the top of the stack using the push operation and removed from the top using the pop operation. Stacks are useful for implementing undo functionality, back functionality in web browser.**Queue:**A queue is a collection of elements, with two main operations: enqueue and dequeue. Elements can be added to the rear of the queue using the enqueue operation and removed from the front using the dequeue operation. Queues are useful for scheduling and implementing first in first out functionality.**Trees:**A tree is a collection of elements with a hierarchical structure. Each element in a tree is called a node, and each node has zero or more child nodes. Trees are useful for hierarchical data and searching specific elements.**Graphs:**A graph is a collection of vertices and edges. Vertices are the nodes of the graph, and edges are the lines connecting the vertices. Graphs are useful for representing networks of relationships, such as in social networks, transportation networks, and the internet.**Hash tables:**A hash table is a data structure that allows for efficient insertion, deletion, and retrieval of elements. It uses a hash function to map the elements to indices in an array, so that each element can be quickly located.

These are just a **few examples** of the many different types of **data structures** that are used in computer science. Each type has its own strengths and weaknesses, and the choice of which one to use will depend on the specific requirements of the application.

## Example of uses of data structure in Python

In Python, **various data structures** can be implemented using **built-in data types** and **classes**. Here are a few examples:

**Arrays:** Python lists can be used as arrays. For example:

my_array = [1, 2, 3, 4, 5] print(my_array[2]) # prints 3

**Linked Lists:** Python does not have a built-in linked list class, but we can create one using classes:

class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def add_node(self, data): new_node = Node(data) new_node.next = self.head self.head = new_node my_list = LinkedList() my_list.add_node(1) my_list.add_node(2) my_list.add_node(3)

**Stack:** Python lists can be used as a stack with the append() and pop() methods. For example:

my_stack = [] my_stack.append(1) my_stack.append(2) my_stack.append(3) print(my_stack.pop()) # prints 3

**Queue:** Python lists can be used as a queue with the append() and pop(0) methods. For example:

my_queue = [] my_queue.append(1) my_queue.append(2) my_queue.append(3) print(my_queue.pop(0)) # prints 1

**Trees:** Python does not have a built-in tree class, but we can create one using classes:

class Node: def __init__(self, data): self.data = data self.left = None self.right = None class Tree: def __init__(self): self.root = None def add_node(self, data): new_node = Node(data) if self.root is None: self.root = new_node else: current_node = self.root while True: if data < current_node.data: if current_node.left is None: current_node.left = new_node break else: current_node = current_node.left else: if current_node.right is None: current_node.right = new_node break else: current_node = current_node.right my_tree = Tree() my_tree.add_node(5) my_tree.add_node(3) my_tree.add_node(7)

**Graphs:** Python does not have a built-in graph class, but one can be implemented using classes and dictionaries:

class Graph: def __init__(self): self.vertices = {} def add_vertex(self, vertex): self.

**Younes Derfoufi**

**my-courses.net**

[…] (or bug) – Compiler – Data structure – Debugger – Encapsulation – Software engineering – MiddleWare – ORM – OOP object-oriented […]