DEV Community 👩‍💻👨‍💻

Cover image for Data structures and algorithms with python: Linear Data structures.
EzeanaMichael
EzeanaMichael

Posted on

Data structures and algorithms with python: Linear Data structures.

What are data structures?

Data structures are organized formats or methods of storing data for effective search, retrieval, and use of data.

Operations that can be performed on data structures

  • Insertion
  • Deletion
  • Transversion

Types of data structures.
There are 2 types of data structures

  1. - Linear Data structure
  2. - Non-linear Data structure

Linear Data structure
In this type of data structure, data is stored in an ordered or sequential manner.
There are 3 types of linear data structures:

  • Stack
  • Queue
  • LinkedList

1. Stack
Stack is a linear data structure that follows a last in first out(LIFO) method. A real-world example is a stack or deck of cards.

Image description
Image gotten from tutorialspoint

There are 2 main operations in a stack:

  • Push: The function of the push operation is to put a new data item or element into the top of the stack.
  • Pop: The function of the pop operation is to delete or remove the last element to enter the stack.

A stack function in python that demonstrates how it works.

Image description

2. Queue
This is a type of linear data structure that stores data in a sequential manner and follows a First in First out (FIFO) method. A real-world example of this is cars queuing in traffic.

Image description

Image gotten from tutorialspoint

There are 2 main operations in a queue:

  • Enqueue: This is an operation that adds a data item or element to the back of the queue.
  • Dequeue: This is an operation that removes a data item or element at the front of the queue.

Implementation of queue data structure in python:

Image description

3. Linked list
A LinkedList is an ordered data structure which consists of a connection of nodes which contains data items and a connecting link to the next node.

There are 3 different types of linked list:

  • Single linked list: In this type of linked list, there is one head and a tail/exit of the linked list with each node pointing to the next.

Image description

  • Double linked list:In this type of linked list, there is one head and two tails/exits of the linked list with each node pointing to the next and previous node.

Image description

  • Circular linked list:In this type of linked list, there is one head. The next of the last node points to the first node and the previous of the first node points to the last node.

Image description

Images gotten from geekforgeeks
Main operations of Linked list

  • Next - Moves from one node to the next or the exit
  • Prev -Moves from one node to a previous node
  • Insertion- A new node can be added in front or behind a node.
  • Deletion- A node can be removed from a linked list .

A python implementation of linked list

Image description

Top comments (0)

Create an Account!

👀 Just want to lurk?

That's fine, you can still create an account and turn on features like 🌚 dark mode.