Data structures and algorithms with python: Linear Data structures.
Tags: python, programming, beginners, codenewbie
Published: 2022-11-05 07:36:35

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nb4v7xxk7p4qk4wyff3.jpg)
_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8cu8gix2jy3gasbibiz.png)


**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xj9z006ayfcyry0n00or.png)

_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5m8zye7ekvtmuivfsi2.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9py1worefi5eu5dzjsol.png)
- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/63udu5j8fya2cr06ptf9.png)
- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9t4cu3u6ify138kxj3uo.png)

_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rrv05741pgm8x5si1z4e.png)