DEV Community

Mercy wanyonyi
Mercy wanyonyi

Posted on • Updated on

Introduction to Data Structures and Algorithims with Python

Data Structures and Algorithims have always been approached with a skeptical attitude of being very hard. It is probaly the terms that scare most people. I have however learnt that the best way is to take a bottom-up approcah: starting from the basic stuff to the complex ones.

This article will help you understand the basics of data structures and algorithims, their importance and eventually how you can implement them in a real-life situation.

What are Data Structures?

Simply put, Data Structures are ways in which data is organised in order to easily access, manipulate and share it. They therefore allow effective operations in onces code. At the end of this article, you will realise that using the right data structure improves the effectiveness of your code by far.


In my previous article, we introduces ourselves to some basic data structures that are a necessary prerequisite to this course.

The data structures that we will talk about here are categorised into two groups as shown below:

Image description
We shall discuss linear data structures in this article

Linear Data Structures

These are structures that work on one data then moves to the next hence the 'linear'. Among them we have:


In python, Arrays are more or less like lists. However, an array is immutable and contains data of the same type unlike a list can contain different types of data. Arrays are very important data structures especiassly when it comes to recursive functions.

Image description
The example above is an array of integers with thier indices shown below.
The following are ways ways in which one can manipulate arrays

'''an array of integers'''
#int array [10]= {1,2,3,4,5,6,7,8,9,10} 

#Adding items to an array
   print (arrayName)

#Removing Items from an Array
#removes 1st item


#removing item im arr[3]


Enter fullscreen mode Exit fullscreen mode

Linked Lists

Lisked lists are data structures that consist of nodes connected with pointers. These nodes are made up of data and memory location of the next node as shown below:

Image description

The first node is called the head node. If the linked list is empty then the value of the head node is "None".If the list is long, we will have access to the other node sequentially starting from the head node.There are however other linked lisyed that can be traversedd differentlty. The last node doesn't contain any address value but it has a data value(×=0).

Linked lists can be categorised into three groups

   - Singly Linked List

   - Doubly linked lists

   - Circular Linked List.
Enter fullscreen mode Exit fullscreen mode

Singly Linked List

These are linked that can be accessed only in forward direction starting with the first data.

Doubly Linked Lists

In this lists one node has two pointer: one to the next node and the other to the previous node apart from the first and last node.

Circular Linked List.

This isa linked list where the head and the last node are connected such that the address value of the last node belongs to the first node.

The following are ways in which the mentioned linked lists can be implemented.

#Singly Linked Lists

#Node class

Class Node:
   def__init__(self, dataval=None)

Class SLinkedList:
    def PrintList (self)
       While printval is not None:
         print (printval.dataval) #printing the first data value value

          printval=printval.nextval # the first node.

#calling the list


#linkng the nodes



Enter fullscreen mode Exit fullscreen mode


As the word implies, stacks are structures that store data arranged in a Last-in-First out manner.
One can alter a stack from only one end. Adding and removing items from the stack are carried by the push and pop operations.

#creatibg a stack
def stack ():
   stack =[]
  return stack

#Empty stack
def IsEmpty (stack):
    return len (stavk)==0

#adding items
 def push (stack,item):
     stack.append (item)
     Print ("pushed item; "+item)

#removing item
def pop (stack)
    If pop (IsEmpty (stack))
         return "stack is empty"
    return stack.pop ()

Enter fullscreen mode Exit fullscreen mode

Top comments (0)