BASU S Learn Data Structure Array Linked List Stack Queue Using Python in 5 Minutes 2021
BASU S Learn Data Structure Array Linked List Stack Queue Using Python in 5 Minutes 2021
DATA STRUCTURE
LINKED LIST
ARRAY
STACK
QUEUE
USING
PYTHON
IN 5 MINUTES
Copyright © 2021 S Basu
All rights reserved.
Disclaimer:
The information and materials presented here are for educational
purposes only. Every effort has been made to make this book as
complete and as accurate as possible but no warranty or fitness is
implied. The information provided is on an "as is" basis. The ideas and
opinions expressed are of the author's own imagination and the author is
not affiliated to any organization, school or educational discipline and
will not be held accountable or liable for any inadvertent
misrepresentation.
Contents
Chapter 1 : Few Important Python Basics
1.1 : What is a Python module?
1.2 : What is a Python function?
1.3 : What is a Python class?
Chapter 2 : Array
Chapter 3 : Linked List
3.1 : Creating a Linked List & displaying its values
3.2 : Insert a node in the beginning of the Linked List
3.3 : Insert a node in the middle of the Linked List
3.4 : Insert a node at the end of the Linked List
3.5 : Deletion
3.6 : Types of Linked List
3.7 : Difference between a Array and a Linked List
Chapter 4 : Stack
Chapter 5 : Queue
Chapter 1 : Few Important Python
Basics
Before we start learning about data structure related topics like linked list,
array, stack and queue, we first need to get an understanding about a few
python basics like how to create a python module, run a python code and
what is a python function or a python class.
So let us first review these python topics.
Open Notepad++ and create a new file (hello_world ) and save the
file with .py extension
We see that the test module prints out “Hello World ” output from
hello_world module.
Syntax:
def function_name ( ) :
……..
Example:
Code Explanation:
function_name ( 123,”Hello_World” )
● Python object contains the copy of all attributes and methods present
within its class. To access those properties dot operator ( . ) is used.
The syntax for creating a class is:
class class_name :
def __init__(self) :
….
def method1 :
….
The syntax for creating an object of a class is:
class Hello :
def __init__(self) :
………………
…………
h = Hello( ) , where h is the object of class Hello .
Example:
Output
In order to get the value Ford as output, we need to write print ( cars [ 2 ]
)
A value from a List can be accessed from its index value and the
syntax is.
List_name [ index_num ]
Example:
Create a new Python file (variables.py ) and write the following lines of
code:
After running the above piece of code, we get an output of:
Code explanation:
● In the above piece of code, fruits is a List containing 6 elements. The
value at position 0 is “apple ”, at position 1 is “orange ” and so on.
NOTE : To insert a data into an array list, python provides us with insert
method.
To delete an element from the array list, python provide us with remove
method.
The first node is called the Head and the last node is called the Tail.
The last node always points to NULL.
Let's code..
3.1 : Creating a Linked List & displaying its values
Open Notepad++ and create a new python file and name it linked_list.py
and write the following lines of code.
In the above piece of code, we simply created the Node class which
contains two attributes, one is value or data of the linked list node and the
other one is the next pointer which points to the next node of the linked
list.
For now the next pointer points to none.
Now let's create the Linked List class
The Linked_List class contains only one attribute head and for now the
head is empty or none .
The image below shows the order in which we will create our linked list.
Head = n1
n1.next = n2
n2.next = n3
n3 points to none or null
Code explanation:
In line 9, we created the first node of the linked list using Node
class and assigned that node to head .
s.head now act as an object of class Node
In line 12, we linked the second node and the third node using next
attribute of class Node .
From line 14 to line 16, using while loop , we display the values of
each node of the linked list.
Let us suppose we want to assign a node x as the new head of the linked
list as shown in the image below.
In order to do this the following three steps are needed:
Step 1 : Move the existing linked list (suppose linked list z ) and store it in
a temporary variable.
Step 2 : Create a new node and assign that node to the head .
Step 3 : Now link the new head with the existing linked list z with the help
of next pointer.
Time complexity denotes the amount of time taken by the code to execute.
The Big-O notation is the growth rate of a function . It usually provides the upper limit on the
growth rate of an algorithm or function .
Time Complexity : In this case we have a reference node and know the
position in which we are inserting the new node , so the time complexity
is O(1).
If we do not have a reference node and have to iterate through the linked
list to insert a node , in that case the time complexity is O(n) , where n
refers to the input size. For example, in a list , n refers to the number of
elements.
Suppose we want to insert node b at the end of the linked list as shown in
the image below.
Step 2: Now make the last node a of the linked list point to the new node
b
In linked_list.py , add the following function in class Linked_List .
Now let's call the newly created function .
3.5 : Deletion
Suppose we want to remove node b from the linked list as shown in the
image below.
Step 2: Make the b node ’s next attribute point to c node ’s next attribute
which is pointing to node d
While coding, we just need to add one extra attribute prev to the class
Node
Head = n1
n1.next = n2
n2.prev = n1
n2.next points to none or null
n1.prev points to none or null
The highlighted piece of code in the screen shot above shows the double
linked list insert function .
Code explanation:
In line 11, we made the new_node point to head with the help of
next attribute.
➢ If head node is present, then make the head node point to the new
node with the help of prev attribute.
In the code below we will create a circular linked list and will display the
values.
Code explanation:
In the above piece of code we created our general linked list, but we
made the last node (n3 ) point to the head of the linked list in line:
n3.next = s.head
Step 2: Check whether the last node points to the head node . If last node
points to the head node , then it is a circular linked list.
In the highlighted piece of code above, we check whether the last node
points to head or not.
Plate a is the first one to go in and plate d is the last one to enter the cabinet.
When we want to use a plate, we usually take plate d and it is the first plate
to be removed and plate a is the last one to be removed.
This concept is similar to a data structure stack . The first data element
inserted into the stack (plate a in the above example ) is the last one to be
removed and the last element inserted into the stack (plate d of above
example ) is the first one to be removed. Hence stack is called Last In
First Out (LIFO) or First In Last Out (FILO) list. The insertion and
deletion always occurs from one end and that is the top .
NOTE: The data elements are pushed and popped from the top of the
stack which has a special index value of -1. When the stack is empty the
index value of top is -1.
The operation of adding and removing the elements is called PUSH
and POP respectively. Python provide us with append( ) and pop( )
functions to add and remove the data elements.
Now create a new python file and name it stack.py and write the following
lines of code.
Code explanation:
pop function pops or removes the last data element entered from
the top of the stack .
The first car which enters the line gets washed first and they leave. Then the
second car is washed and then the third car. Similarly in queue , the first
data element inserted gets removed first and the last data element inserted
gets removed last. Hence queue is called First in First Out (FIFO) or
Last In Last Out (LILO) list .
When an element is inserted into the queue , the concept is called
EnQueue and when an element is removed from the queue , the concept is
called DeQueue .
Now create a new python file and name it queue.py and write the following
lines of code.
Code explanation: