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.

Prerequisite

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:

Arrays

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} 
arrayName=[1,2,3,4,5,6,7,8,9,10]

#Adding items to an array
arrayName.append[11]
   print (arrayName)
# 
[1,2,3,4,5,6,7,8,9,10]

#Removing Items from an Array
#removes 1st item
arrayName.remove

#output
#[2,3,4,5,6,7,8,9,10]



#removing item im arr[3]
arrayName.pop[3]

#output
#[1,2,3,5,6,7,8,9,10]

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)
      self.dataval=dataval
      self.nextval=None


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

          printval=printval.nextval # the first node.


#calling the list

MyList=SlinkedList()
MyList.headval=Node("Mon")
N2=Node("Tue")
N3=Node("Wed")
N4=Node("Thur")
N5=Node("Fri")

#linkng the nodes
MyList.headval.nextval=N2
N2.nextval=N4
N4.dataval=N5

MyList.PrintList()

#Output
Mon
Tue
Wed
Thur
Fri

Enter fullscreen mode Exit fullscreen mode

Stacks

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)