Activity 1: Department of Artificial Intelligence
Activity 1: Department of Artificial Intelligence
Session 2024-2025
Semester – III
Activity 1
Topic name :- Performing operation on stack, queue & link list in virtual lab
Submitted by :-
Roshni Gupta
Roll No. :19
___________________
Prof. A.R. Deokate
1
Stacks and Queues
Aim :
To learn and implement the basic operations of stack and queue, showcasing their functionality and
efficiency through simple coding exercises.
Stack
Arrays
● Arrays are a kind of data structure that can store a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but it is often
more useful to think of an array as a collection of variables of the same type.
● Instead of declaring individual variables, such as value0, value1, ..., and value99, you
declare one array variable such as value and use value[0], value[1], and ..., value[99] to
represent individual variables. A specific element in an array is accessed by an index.
● All arrays consist of contiguous memory locations. The lowest address corresponds to the
first element and the highest address to the last element.
Arrays Visualization
Time
and
Space
Complexity
● Time complexity of an algorithm gives the measure of time taken by it to run as a function
of the length of the input. Similarly, Space complexity of an algorithm quantifies the
amount of space or memory taken by an algorithm to run as a function of the length of the
input.
2
● Recall that suppose our input is an array of N elements, and our algorithm iterates through
the array once, time complexity will be O(N). If I run two embedded loops to traverse the
array N times, time complexity will be O(N2).
Linked Lists
● A disadvantage of array is that they are static hence they are not easily extended or
reduced. So we will look into another similar data structure called Linked Lists. A linked
list is a linear dynamic data structure where each element is a separate object. Each
element (referred as node) of a list comprises two items - the data and a reference to the
next node.
● The entry point of the linked list is referred by the HEAD and the last node is referred to as
null. If the list is empty then head itself refers to null. Number of nodes are not fixed
which can increase or decrease based on the demand.
● Disadvantage of linked list against array is that it doesn't allow direct access to individual
elements. That is, access time of a particular node is O(n) instead of O(1). And another
disadvantage is memory. Linked list uses extra 4 bytes to store reference to the next node.
Time Complexity
● A linked list is typically accessed from the HEAD node. We reach the desired node by
travelling through the linked list from head node and if we assume there are N nodes in the
linked list then time taken to access/search a node is O(N).
● Inserting into a linked list requires re-pointing the previous node (the node before the
insertion node) to the inserted node, and pointing the newly-inserted node to the next
node. Thus insertion is O(1).
● Deleting from a linked list requires re-pointing the previous node (the node before the
deleted node) to the next node (the node after the deleted node). Thus deletion is O(1).
Question: A set of elements are given in Stack A. With the help of Stack B, POP the elements in
ASCENDING order.
5
Queue
What are Queues?
● Imagine a line of people waiting at a ticket counter. People are served in the order they
come, that is, people who come first are served first whereas people who come later are
served after that.
● Let the action of someone joining a queue be called enqueue and someone being serve and
getting out of the queue be called dequeue.
● A type of structure, similar to the example of the line of people, can be represented as a
data structure. Such a data structure is known as a queue.
Types of Queues
● SIMPLE QUEUE : A simple queue is a type of queue where insertion is at the end of the
queue and removal is at the front.
● CIRCULAR QUEUE : A circular queue is a type of queue where the last element is
connected to the first. An element is added to the end of the queue and removed from the
front.
● PRIORITY QUEUE : A priority queue is a type of queue where the elements are arranged
in some priority order. An element with the highest priority is removed first and insertion
occurs according to the priority.
● DOUBLE ENDED QUEUE : A double ended queue is a type of queue where insertion
and deletion can happen at both ends of the queue.
Queue Operations and Applications
● Just like how we saw in the example of the queue of people, a queue data structure has two
types of operations : enqueue and dequeue. As we can see, a queue is an example of a first
in, first out data structure (FIFO). That is, an element that is enqueued first is the first to be
dequeued.Enqueue operation happens at "REAR" pointer whereas dequeue operation
happens at "FRONT" pointer.
● Queues have a variety of applications. Lets explore a few of them. -> Processing requests
in a website : When a website is visited by a large number of users, it puts the users in a
queue - users that vists the website first are processed earlier. -> Priority Queue in
Operating Systems : An operating system has many tasks to perform and tasks are ordered
according to the most important ones.
6
Enqueue Operation
Dequeue Operation
7
Linked List Practice in Queue
Question: A set of elements are given in Queue A. With the help of queue B, deque the elements
in ASCENDING order.
8
Stacks : Example
Queues : Example
Applications of Stacks
● Expression Evaluation Stack is used to evaluate prefix, postfix and infix expressions.
● Expression Conversion An expression can be represented in prefix, postfix or infix
notation. Stack can be used to convert one form of expression to another.
Applications of Queues
● Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
● In real life scenario, Call Center phone systems uses Queues to hold people calling them in
an order, until a service representative is free. Handling of interrupts in real-time systems.
The interrupts are handled in the same order as they arrive i.e First come first served.
Result :
Hence, successfully to learn and implement the basic operations of stack and queue, showcasing their
functionality and efficiency through simple coding exercises has been completed.
Linked List
9
Aim :
To learn and implement the basic operations of stack and queue, showcasing their functionality and
efficiency through simple coding exercises.
2. Faster Processing: To delete an element from an array we need to shift the elements of the
array. In the worst case, suppose we have an array of size n. Now we want to delete the first
element of the array that means we need to shift the (n-1) elements of the array to the left which
have time complexity of O(n). Also we waste the memory that was occupied by the rightmost
element. On the other hand, deleting an element from Linked List is quite simple, we just need to
change the address hold by the two pointers which have O(1) time complexity.
10
Single Linked List
Each Node of the singly list consists of two parts:
● First part of the singly linked list Node contains the information to be stored.
● Second part of the singly linked list Node contains the address of either its next Node or it
previous Node and is a pointer.
● Head is a pointer that store the address of the first Node of the list. Tail is also a pointer
that holds the address of the last Node of the list.
● Each Node of the singly linked list store the address of its next Node and in that way all
the data of the singly linked list gets linked to each other.
11
Linked List vs Array
12
Insertion of elements into Singly Linked List
Two cases may arise when we want to insert data at the front.
Case – 1 : When the list is empty i.e. head = NULL
Here we can simply do head = tail = newNode(pointer)
Case – 2 : When list contains some entries i.e. head != NULL
● Insertion at Front
● Insertion at End
● Insertion after a Node
13
Insertion of node in Linked List (Case 2b)
14
Question: Convert to 91, 47, 7, 76, 56, 99
15
Deletion of node from Linked List (Case 3a)
16
Deletion of tail node from Linked List
Practice
17
Insertion of elements into Doubly Linked List
Insertion in doubly linked list is similar to that of insertion in singly linked list, unlike there need
to be a change in data type we use, we need to add an extra element to the data type, i.e; Node
*parent, to store the address of the parent of a given node. The same has to be done while insetion,
after creating a new element store the address of it's parent in the Node *parent variable.
Practice
18
Circular linked list
Pictorial Representation of Singly Linked List vs Circular Linked List
Practice
19
Question: Convert to 72, 27, 68, 75, 42
Result :
Hence, successfully to learn and implement the basic operations of linked list, showcasing their
functionality and efficiency through simple coding exercises has been completed.
20