0% found this document useful (0 votes)
21 views92 pages

L3

Uploaded by

Loshilu Singooi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views92 pages

L3

Uploaded by

Loshilu Singooi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 92

Lists:

The list can be defined as an abstract data type in which the


elements are stored in an ordered manner for easier and
efficient retrieval of the elements.
List Data Structure allows repetition that means a single piece
of data can occur more than once in a list.
In the case of multiple entries of the same data, each entry of
that repeating data is considered as a distinct item or entry.
It is very much similar to the array but the major difference
between the array and the list data structure is that array
stores only homogenous data in them whereas the list can
store heterogeneous data items in its object.
List Data Structure is also known as a sequence.
The list can be called Dynamic size arrays, which means their size
increased as we go on adding data in them and we need not to pre-
define a static size for the list.
For example,
numbers = [ 1, 2, 3, 4, 5]
In this example, 'numbers' is the name of the List Data Structure object
and it has five items stored in it. In the object named numbers, we have
stored all the elements of numeric type. In the list, the indexing starts
from zero, which means if we want to access or retrieve the first
element of this list then we need to use index zero and similarly
whenever we want to access any element from this list named
numbers. In other words, we can say that element 1 is on the index 0
and element 2 is on index 1 and similarly for further all elements.
mixed_data = [205, 'Nirnay', 8.56]
• In this second example, mixed_data is the name of the list object that
stores the data of different types. In the mixed_data list, we have
stored data of three types, first one is the integer type which is id
'205', after the integer data we have stored a string type data having
the value 'Nirnay' stored at index 1 and at last the index value 2, we
have stored a float type data having the value '8.56'.
• To access the elements of the mixed_data list, we need to follow the
same approach as defined in the previous example.
We can add more data to these defined List objects and that will get
appended at the last of the list. For example, if we add another data in
the mixed_data list, it will get appended after the float value object
having value '8.56'. And we can add repeating values to these list-
objects.
Various operations on the List Data Structure:
The various operations that are performed on a List Data Structure or
Sequence are:
• Add or Insert Operation: In the Add or Insert operation, a new item
(of any data type) is added in the List Data Structure or Sequence
object.
• Replace or reassign Operation: In the Replace or reassign operation,
the already existing value in the List object is changed or modified. In
other words, a new value is added at that particular index of the
already existing value.
• Delete or remove Operation: In the Delete or remove operation, the
already present element is deleted or removed from the Dictionary or
associative array object.
• Find or Lookup or Search Operation: In the Find or Lookup operation,
the element stored in that List Data Structure or Sequence object is
Linear list:
A linear list, is defined as a sequence of nodes together with a
set of operations on those nodes.
The term "node" can be thought of as something carrying
some information; it might be a person's health record, a
playing card, or simply an integer.
The essential feature of a linear list is the fact that it is a
sequence of nodes.
If there are n nodes in the list, we can write the sequence as
Xl, X2, ... , Xn where Xl is the first node, X2 is the second, and
so on, with Xn being the last node.
The operations that we might want to perform on a
linear list will vary from one situation to another. They
might include:
• examine the contents of the node at one end of the
list
• insert or delete a node at one end of the list
• insert a new node before or after a node containing
some key
• delete the kth node
• alter the contents of the kth node
• search for a node that contains some value

There are a number of possible implementations of lists.
The most basic is whether to use;
• sequential allocation (meaning storing the elements
sequentially in an array) or
• linked allocation (meaning storing the elements in a linked
list).
With linked allocation there are many other options to be
considered;
Is the list singly linked (each node pointing to its successor in
the list),
doubly linked (each node pointing to both its successor and
predecessor),
circularly linked (with the last node pointing back to the first)
sequential allocation:
A list is allocated memory in a sequential manner.
This means that the elements of the list are stored in
memory in the sequence of their declaration.
So if you want to view the fifth element of the list you
have to first traverse through the first four elements of
the list.
This is called sequential allocation of memory.
Linear list:
A linear list is a type of data structure that represents a
collection of elements, or items, arranged in a sequential
order.
In a linear list, each element is connected to its adjacent
elements in a linear fashion, such that there is only one path
through the list.
Linear lists are commonly used in computer programming for
a variety of applications, including data storage, sorting,
searching, and manipulation.
They can be implemented using various data structures, such
as arrays, linked lists, and stacks.
• Arrays are a type of linear list that store
elements in contiguous memory locations,
which makes accessing elements in the list fast
and efficient. However, arrays have a fixed size,
which can limit their flexibility.

• Linked lists, on the other hand, are dynamic


data structures that can grow or shrink as
needed, but accessing elements in the list can
be slower than with arrays.
• Stacks are a special type of linear list that allow
elements to be added and removed from only one
end, known as the top of the stack.
• Linear lists can be used to represent a variety of data
types, such as numbers, characters, strings, and
objects.
• Liner list are useful in many applications such as
sorting, searching and storing data.
• However they have some limitations such as fixed
size( for fixed sized arrays) and slow access time for
insertion and deleting elements (for linked list).
• They are often used as a building block for more
complex data structures, such as trees and graphs."
General Linear List:
A general linear list is a data structure in computer science
and programming that consists of a sequence of nodes, each
of which contains a data element and a reference to the next
node in the sequence.
The nodes can be located anywhere in memory, and they are
connected by the next reference.
The term "general" indicates that the list can have any
number of nodes, and it is not restricted to a fixed size or
capacity.
The "linear" aspect of the list refers to the fact that the
nodes are arranged in a linear sequence, with each node
having only one predecessor and one successor.
• General linear lists can be implemented using various
data structures, such as arrays or linked lists.
• They are commonly used in many applications,
including data storage, graph algorithms, and file
systems.
Differences between linear list and general linear list:
A linear list and a general linear list are both data
structures used in computer science and programming.
However, there is a fundamental difference between the
two;
A linear list is a data structure in which elements are
arranged in a sequential order, where each element is
connected to its adjacent element by a link or a pointer.
This means that in a linear list, each element has only
one predecessor and one successor, except for the first
and last elements, which have only one predecessor or
successor, respectively.
• On the other hand, a general linear list is a data
structure that allows each element to have any
number of predecessors and successors.
• In other words, in a general linear list, a node can
have multiple references to other nodes, which
enables it to form complex data structures such as
trees, graphs, and networks.
Therefore, the primary difference between a linear list
and a general linear list is that a linear list only allows
for a simple sequence of elements, while a general
linear list allows for more complex and interconnected
structures.
Examples:
• A single element can be added using an append function.
Stacks:
• Stacks is a linear type of data structure that follows
the LIFO (Last-In-First-Out) principle and allows
insertion and deletion operations from one end of
the stack data structure, that is top.
• Implementation of the stack can be done by
contiguous memory which is an array, and non-
contiguous memory which is a linked list.
• Stack plays a vital role in many applications.
• Real-life examples of a stack are a deck of cards, piles
of books, piles of money, and many more.
Some key points related to
stack:
• It is called as stack because it behaves like a real-world
stack, piles of books, etc.
• A Stack is an abstract data type with a pre-defined
capacity, which means that it can store the elements
of a limited size.
• It is a data structure that follows some order to insert
and delete the elements, and that order can be LIFO
(LAST IN FIRST OUT) or FILO (FIRST IN LAST OUT).
Working of Stack:
• Stack works on the LIFO pattern.
• Inserting a new element in the stack is termed a push operation.
• Removing or deleting elements from the stack is termed pop
operation
Standard Stack Operations:
• push (): When we insert an element in a stack then the operation is
known as a push. If the stack is full then the overflow condition
occurs.
• pop (): When we delete an element from the stack, the operation is
known as a pop. If the stack is empty means that no element exists in
the stack, this state is known as an underflow state.
• is Empty (): It determines whether the stack is empty or not.
• Is Full (): It determines whether the stack is full or not.'
• peek (): It returns the element at the given position.
• count (): It returns the total number of elements available in a stack.
• change (): It changes the element at the given position.
• display (): It prints all the elements available in the stack.
PUSH operation:
The steps involved in the PUSH operation is given below:
• Before inserting an element in a stack, we check whether the stack is
full.
• If we try to insert the element in a stack, and the stack is full, then the
overflow condition occurs.
• When we initialize a stack, we set the value of top as -1 to check that
the stack is empty.
• When the new element is pushed in a stack, first, the value of the top
gets incremented, i.e., top=top+1, and the element will be placed at
the new position of the top.
• The elements will be inserted until we reach the max size of the stack.
Push operation includes
various steps:
• Step 1: First, check whether or not the stack
is full
• Step 2: If the stack is complete, then exit
• Step 3: If not, increment the top by one
• Step 4: Insert a new element where the top
is pointing
• Step 5: Success
The algorithm of the push
operation is:
Begin push: stack, item
If the stack is complete(Full), return null
end if
top ->top+1;
stack[top] <- item
end
POP operation:
The steps involved in the POP operation is given below:
• Before deleting the element from the stack, we
check whether the stack is empty.
• If we try to delete the element from the empty
stack, then the underflow condition occurs.
• If the stack is not empty, we first access the
element which is pointed by the top
• Once the pop operation is performed, the top is
decremented by 1, i.e., top=top-1.
Pop operation includes various steps:

• Step 1: First, check whether or not the stack is


empty
• Step 2: If the stack is empty, then exit
• Step 3: If not, access the topmost data element
• Step 4: Decrement the top by one
• Step 5: Success
The algorithm of the pop
operation:
Begin pop: stack
if the stack is empty
return null
end if
item -> stack[top] ;
Top -> top - 1;
Return item;
end
Peek Operation:
The algorithm of a peek operation is:
begin to peek
return stack[top];
End
The implementation of the peek operation is:
int peek()
{
return stack[top];
}
The algorithm of the isEmpty() function:

begin
if
topless than 1
return true
else
return false
else if
end
Types of Stacks:
• Register Stack:
This type of stack is also a memory element present in the
memory unit and can handle a small amount of data only.
The height of the register stack is always limited as the size of
the register stack is very small compared to the memory.
• Memory Stack:
This type of stack can handle a large amount of memory data.
The height of the memory stack is flexible as it occupies a
large amount of memory data.
Implementation of Stack:
There are two ways to implement a stack
• Using array
• Using linked list
Implementing Stack using Arrays:
• In array implementation, the stack is formed using an array. All the
operations are performed using arrays.
Advantages of array implementation:
• Easy to implement.
• Memory is saved as pointers are not involved.
Disadvantages of array implementation:
• It is not dynamic.
• It doesn’t grow and shrink depending on needs
at runtime.
Implementing Stack using Linked List:
• Every new element is inserted as a top element
in the linked list implementation of stacks in data
structures.
• That means every newly inserted element is
pointed to the top.
• Whenever you want to remove an element from
the stack, remove the node indicated by the top,
by moving the top to its previous node in the list.
Advantages of Linked List
implementation:
• The linked list implementation of a stack can grow and shrink
according to the needs at runtime.
• It is used in many virtual machines like JVM.
• Stacks are more secure and reliable as they do not get corrupted
easily.
• Stack cleans up the objects automatically.
Disadvantages of Linked List implementation:
• Requires extra memory due to the involvement of pointers.
• Random accessing is not possible in stack.
• The total size of the stack must be defined before.
• If the stack falls outside the memory it can lead to abnormal
termination.
Application of Stack in Data Structures:
• Expression Evaluation and Conversion
• Backtracking
• Function Call
• Parentheses Checking
• String Reversal
• Syntax Parsing
• Memory Management
Storing two stacks into one Array:
To store two stacks into one array, you can divide the array
into two parts and allocate each part to a separate stack.
One way to do this is to allocate the first half of the array to
one stack and the second half to the other stack.
Here's an example implementation in Python:
class TwoStacks:
def __init__(self, n):
self.n = n
self.array = [None] * n
self.top1 = -1
self.top2 = n
def push1(self, item):
if self.top1 < self.top2 - 1:
self.top1 += 1
self.array[self.top1] = item
else:
print("Stack Overflow")
return
def push2(self, item):
if self.top1 < self.top2 - 1:
self.top2 -= 1
self.array[self.top2] = item
else:
print("Stack Overflow")
def pop1(self):
if self.top1 == -1:
print("Stack Underflow")
return None
else:
item = self.array[self.top1]
self.top1 -= 1
return item
def pop2(self):
if self.top2 == self.n:
print("Stack Underflow")
return None
else:
item = self.array[self.top2]
self.top2 += 1
return item

In the above example, the TwoStacks class initializes


the array with a length n and initializes two pointers
top1 and top2 to track the top of each stack.
The push1 method pushes an item onto the first stack,
push2 pushes an item onto the second stack, pop1
pops an item off the first stack, and pop2 pops an item
off the second stack.
• To push an item onto a stack, the method first checks
if there is enough space in the array.
• If there is, it increments the top pointer and sets the
item at the top of the stack.
• If there is not enough space, it prints an error
message and returns.
• To pop an item off a stack, the method first checks if
the stack is empty.
• If it is, it prints an error message and returns.
• If it is not empty, it retrieves the item at the top of
the stack, decrements the top pointer, and returns
the item.
Queue:
• A Queue is an abstract data structure, defined as a linear data
structure that is open at both ends and the operations are
performed in First In First Out (FIFO) order.
• We define a queue to be a list in which all additions to the list are
made at one end, and all deletions from the list are made at the
other end.
• The element which is first pushed into the order, the operation is
first performed on that.
• A queue is open at both its ends.
• One end is always used to insert data (enqueue) and the other is
used to remove data (dequeue).
• Queue follows First-In-First-Out methodology, i.e., the data item
stored first will be accessed first.
FIFO Principle of Queue:
• A Queue is like a line waiting to purchase tickets,
where the first person in line is the first person
served. (i.e. First come first serve).
• Position of the entry in a queue ready to be served,
that is, the first entry that will be removed from the
queue, is called the front of the queue(sometimes,
head of the queue), similarly, the position of the last
entry in the queue, that is, the one most recently
added, is called the rear (or the tail) of the queue.
Characteristics of Queue:
• Queue can handle multiple data.
• We can access both ends.
• They are fast and flexible.
Types of Queue:
There are four different types of queue as follows;
• Simple Queue or Linear Queue
• Circular Queue
• Priority Queue
• Double Ended Queue (or Deque)
Simple Queue or Linear Queue:
• In Linear Queue, an insertion takes place from one end while
the deletion occurs from another end.
• The end at which the insertion takes place is known as the rear
end, and the end at which the deletion takes place is known as
front end.
• It strictly follows the FIFO rule.
• The major drawback of using a linear Queue is that
insertion is done only from the rear end.
• If the first three elements are deleted from the
Queue, we cannot insert more elements even though
the space is available in a Linear Queue.
• In this case, the linear Queue shows the overflow
condition as the rear is pointing to the last element of
the Queue.
Circular Queue:
• In Circular Queue, all the nodes are represented as circular.
• It is similar to the linear Queue except that the last element of the
queue is connected to the first element.
• It is also known as Ring Buffer, as all the ends are connected to
another end.
• The representation of circular queue is shown in the below image –
• The drawback that occurs in a linear queue is
overcome by using the circular queue.
• If the empty space is available in a circular queue, the
new element can be added in an empty space by
simply incrementing the value of rear.
• The main advantage of using the circular queue is
better memory utilization.
Priority Queue:
• It is a special type of queue in which the elements are arranged based
on the priority.
• It is a special type of queue data structure in which every element has
a priority associated with it.
• Suppose some elements occur with the same priority, they will be
arranged according to the FIFO principle.
• The representation of priority queue is shown in the below image;
• Insertion in priority queue takes place based on the arrival,
while deletion in the priority queue occurs based on the
priority.
• Priority queue is mainly used to implement the CPU
scheduling algorithms.
• There are two types of priority queue that are discussed as
follows;
Ascending priority queue:
• In ascending priority queue, elements can be inserted in
arbitrary order, but only smallest can be deleted first.
• Suppose an array with elements 7, 5, and 3 in the same
order, so, insertion can be done with the same sequence, but
the order of deleting the elements is 3, 5, 7.
Descending priority queue:
• In descending priority queue, elements can be
inserted in arbitrary order, but only the largest
element can be deleted first.
• Suppose an array with elements 7, 3, and 5 in the
same order, so, insertion can be done with the same
sequence, but the order of deleting the elements is 7,
5, 3.
Deque (or, Double Ended Queue):
• In Deque or Double Ended Queue, insertion and deletion can be done
from both ends of the queue either from the front or rear.
• It means that we can insert and delete elements from both front and
rear ends of the queue.
• Deque can be used as a palindrome checker means that if we read the
string from both ends, then the string would be the same.
• Deque can be used both as stack and queue as it allows the insertion
and deletion operations on both ends.
• Deque can be considered as stack because stack follows the LIFO (Last
In First Out) principle in which insertion and deletion both can be
performed only from one end.
• And in deque, it is possible to perform both insertion and deletion
from one end, and Deque does not follow the FIFO principle.
The representation of the deque is shown in the below image;
There are two types of deque as follows;
Input restricted deque:
• As the name implies, in input restricted queue, insertion operation
can be performed at only one end, while deletion can be performed
from both ends.
Output restricted deque:
• As the name implies, in output restricted queue, deletion operation
can be performed at only one end, while insertion can be performed
from both ends.
Operations performed on queue:
The fundamental (Basic) operations that can be performed on
queue are as follows;
Enqueue:
• The Enqueue operation is used to insert the element at the
rear end of the queue.
• It returns void.
Dequeue:
• It performs the deletion from the front-end of the queue.
• It also returns the element which has been removed from
the front-end.
• It returns an integer value.
Peek:
• This is the third operation that returns the element,
which is pointed by the front pointer in the queue
but does not delete it.
Queue overflow (isfull):
• It shows the overflow condition when the queue is
completely full.
Queue underflow (isempty):
• It shows the underflow condition when the Queue is
empty.
• Thus no elements are in the Queue.
Enqueue Operation:
Queues maintain two data pointers, front and rear.
Therefore, its operations are comparatively difficult to implement than
that of stacks.
The following steps should be taken to enqueue (insert) data into a
queue;
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error and exit.
• Step 3 − If the queue is not full, increment rear pointer to point the
next empty space.
• Step 4 − Add data element to the queue location, where the rear is
pointing.
• Step 5 − return success.
Dequeue Operation:
Accessing data from the queue is a process of two tasks,
access the data where front is pointing and remove the data
after access.
The following steps are taken to perform dequeue operation;
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow error and
exit.
• Step 3 − If the queue is not empty, access the data where
front is pointing.
• Step 4 − Increment front pointer to point to the next
available data element.
• Step 5 − Return success.
Ways to implement the queue:
There are two ways of implementing the Queue:
Implementation using array:
The sequential allocation in a Queue can be
implemented using an array.
Implementation using Linked list:
The linked list allocation in a Queue can be
implemented using a linked list.
Array representation of Queue:
We can easily represent queue by using linear arrays.
There are two variables i.e. front and rear, that are
implemented in the case of every queue.
Front and rear variables point to the position from
where insertions and deletions are performed in a
queue.
Initially, the value of front and queue is -1 which
represents an empty queue.
Array representation of a queue containing 5 elements along with the
respective values of front and rear, is shown in the following figure.
The above figure shows the queue of characters
forming the English word "HELLO".
Since, No deletion is performed in the queue till now,
therefore the value of front remains 0.
However, the value of rear increases by one every time
an insertion is performed in the queue.
After inserting an element into the queue shown in the
above figure, the queue will look something like
following.
The value of rear will become 5 while the value of front
remains same.
After deleting an element, the value of front will increase from 0 to 1
however, the queue will look something like following;
Drawback of array implementation:
• Although, the technique of creating a queue is easy,
but there are some drawbacks of using this technique
to implement a queue.
Memory wastage :
• The space of the array, which is used to store queue
elements, can never be reused to store the elements
of that queue because the elements can only be
inserted at front end and the value of front might be
so high so that, all the space before that, can never be
filled.
• The above figure shows how the memory space is wasted in the array
representation of queue.
• In the above figure, a queue of size 10 having 3 elements, is shown.
• The value of the front variable is 5, therefore, we can not reinsert the
values in the place of already deleted element before the position of front.
• That much space of the array is wasted and can not be used in the future
(for this queue).
Deciding the array size:
• On of the most common problem with array implementation
is the size of the array which requires to be declared in
advance.
• Due to the fact that, the queue can be extended at runtime
depending upon the problem, the extension in the array size
is a time taking process and almost impossible to be
performed at runtime since a lot of reallocations take place.
• Due to this reason, we can declare the array large enough so
that we can store queue elements as enough as possible but
the main problem with this declaration is that, most of the
array slots (nearly half) can never be reused.
• It will again lead to memory wastage.
Linked List implementation of Queue:
• Due to the drawbacks discussed in the array
implementation, it can not be used for the large scale
applications where the queues are implemented.
• One of the alternative of array implementation is linked
list implementation of queue.
• The storage requirement of linked representation of a
queue with n elements is o(n) while the time
requirement for operations is o(1).
• In a linked queue, each node of the queue consists of
two parts i.e. data part and the link part. Each element
of the queue points to its immediate next element in
the memory.
• In the linked queue, there are two pointers maintained in the memory
i.e. front pointer and rear pointer.
• The front pointer contains the address of the starting element of the
queue while the rear pointer contains the address of the last element
of the queue.
• Insertion and deletions are performed at rear and front end
respectively.
• If front and rear both are NULL, it indicates that the queue is empty.
• The linked representation of queue is shown in the following figure.
Operation on Linked Queue
• There are two basic operations which can be implemented
on the linked queues.
• The operations are Insertion and Deletion.
Insert operation:
• The insert operation append the queue by adding an
element to the end of the queue.
• The new element will be the last element of the queue.
• Firstly, allocate the memory for the new node ptr by using
the following statement;
• Ptr = (struct node *) malloc (sizeof(struct node));
• There can be the two scenario of inserting this new node ptr into the
linked queue.
• In the first scenario, we insert element into an empty queue.
• In this case, the condition front = NULL becomes true.
• Now, the new element will be added as the only element of the queue
and the next pointer of front and rear pointer both, will point to NULL.
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
In the second case, the queue contains more than one
element.
The condition front = NULL becomes false.
In this scenario, we need to update the end pointer rear so
that the next pointer of rear will point to the new node ptr.
Since, this is a linked queue, hence we also need to make the
rear pointer point to the newly added node ptr.
We also need to make the next pointer of rear point to NULL.
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
In this way, the element is inserted into the queue.
Algorithm:
• Step 1: Allocate the space for the new node PTR
• Step 2: SET PTR -> DATA = VAL
• Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT = NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]
• Step 4: END
Deletion:
• Deletion operation removes the element that is first
inserted among all the queue elements.
• Firstly, we need to check either the list is empty or not.
• The condition front == NULL becomes true if the list is
empty, in this case , we simply write underflow on the
console and make exit.
• Otherwise, we will delete the element that is pointed by the
pointer front.
• For this purpose, copy the node pointed by the front pointer
into the pointer ptr.
• Now, shift the front pointer, point to its next node and free
the node pointed by the node ptr.
• This is done by using the following statements.
ptr = front;
front = front -> next;
free(ptr);
Algorithm:
• Step 1: IF FRONT = NULL
Write " Underflow "
Go to Step 5
[END OF IF]
• Step 2: SET PTR = FRONT
• Step 3: SET FRONT = FRONT -> NEXT
• Step 4: FREE PTR
• Step 5: END
Store an Element in a Queue:
Enqueue an element to a queue.
To store an element in a queue, you need to perform the
following steps:
• Check if the queue is full. If the queue is full, you cannot
store any more elements in it.
• If the queue is not full, then insert the element at the rear
end of the queue.
• This operation is called "enqueue" in the queue data
structure.
• After inserting the element, update the rear pointer of the
queue to point to the newly added element.
Here's a sample code in Python to enqueue an element into a
queue:
def enqueue(queue, element):
if len(queue) == MAX_SIZE:
print("Queue is full.")
else:
queue.append(element)
print("Element enqueued to the queue:", element)
In this code, queue is the list representing the queue and
element is the element to be enqueued.
MAX_SIZE is the maximum size of the queue.
To enqueue an element, you can simply call the
enqueue function passing the queue and the
element to be enqueued:
queue = []
enqueue(queue, 10) # enqueues 10 to the queue
enqueue(queue, 20) # enqueues 20 to the queue
After executing the above code, the queue would
look like: [10, 20] where 10 is at the front of the
queue and 20 is at the rear.

You might also like