DEV Community

Cover image for Data Structures and Algorithms
Sharon Chang'ach
Sharon Chang'ach

Posted on

Data Structures and Algorithms


A data structure is a data organization, management, and storage format that enables efficient access and modification. It shows the relationships among the data, and the functions or operations that can be applied to the data. Algorithms on the other hand are a set of instructions that solve a problem.

Data Structures

  • sets

  • lists

  • stacks

  • linked lists

  • queues

  • dictionaries

  • Trees


A set is an unordered collection of unique elements. This means that it does not allow duplication.

basket={'apple', 'eggs', 'banana', 'orange'}
same =set(['apple', 'eggs', 'banana', 'orange'])
Enter fullscreen mode Exit fullscreen mode


A list is a container data type that stores a sequence of elements. This means that it allows different types of elements in the list.

List = [1, 2, 3, "GFG", 2.3]
Enter fullscreen mode Exit fullscreen mode


[1, 2, 3, 'GFG', 2.3]
Enter fullscreen mode Exit fullscreen mode


Think of a loading books into a box. Books are loaded one by one and the first one in becomes the last one when the loading is done. This is basically the working of a stack as well.
The functions in a stack are:

  • empty() – prints out whether the stack is empty.
  • size() – prints out the number of elements in the stack.
  • top() – prints out the topmost element.
  • append() – Inserts the element at the top of a stack.
  • pop() – Deletes the topmost element.
Enter fullscreen mode Exit fullscreen mode


Enter fullscreen mode Exit fullscreen mode

Linked list

It is a linear data structure, in which the elements are not stored at contiguous memory locations.


Consider a line in a bank. The first person is at the front of the line and as people join, they are added from the rear. This is basically what a queue is. The structure reduces from the front.

  • Enqueue: Adds an item to the queue.
  • Dequeue: Removes an item from the queue.
  • Front: prints out the front item from queue
  • Rear: prints out the last item from queue


It is a useful data structure for storing (key, values) pairs.

# Creating a Dictionary
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("Creating Dictionary: ")

# accessing a element using key
print("Accessing a element using key:")

# accessing a element using get()
# method
print("Accessing a element using get:")

# creation using Dictionary comprehension
myDict = {x: x**2 for x in [1,2,3,4,5]}
Enter fullscreen mode Exit fullscreen mode


Creating Dictionary: 
{'Name': 'Geeks', 1: [1, 2, 3, 4]}
Accessing a element using key:
Accessing a element using get:
[1, 2, 3, 4]
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Enter fullscreen mode Exit fullscreen mode


A tree is a hierarchical data structure.
There are two types of trees:

  • Binary Search Tree

  • Red Black Trees
    It has several functions which include:
    Traversal, search, insertion, deletion and update.

Functions in data structures

  1. Insertion
    It is the addition of elements in the data structures.

  2. Deletion
    It is the removal of elements from the data structures

  3. Update
    It is when an element is changed in the data structures.

  4. Search
    It is where an element is searched in the data structure.

Top comments (0)