0% found this document useful (0 votes)
19 views20 pages

Activity 1: Department of Artificial Intelligence

HxHdaitso

Uploaded by

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

Activity 1: Department of Artificial Intelligence

HxHdaitso

Uploaded by

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

DEPARTMENT OF ARTIFICIAL INTELLIGENCE

Session 2024-2025
Semester – III

Activity 1

Subject :- Data structure

Topic name :- Performing operation on stack, queue & link list in virtual lab

Submitted by :-

Roshni Gupta
Roll No. :19

Under The Guidance of :-


Prof. A.R. Deokate

___________________
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.

Learning Objectives of this Module :


In this module, we will be learn about:
● Gain the concept of stacks
● Understand the basic operations of stacks
● Practice the operations of stacks
● Test your conceptual understanding with a short quiz

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.

Linked List Visualization

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).

What are Stacks?


3
● Imagine a pile of books, with books stacked one over the other. From this pile of books,
you can either put another book on top or remove a book from the top.
● The book which is at the bottom of the pile is the last one to be taken out, while the books
at the top are removed first. Books can only be added to the top of the pile.
● Let the action of putting a book on the top be called as push and let the action of removing
a book be called pop. A type of structure, similar to the example of the pile of books, can
be represented as a data structure. Such a data structure is known as a stack.
Stack Operations and Applications
Just like how we saw in the example of a stack of books, a stack data structure has two types
of operations : push and pop. As we can see, a stack is an example of a last in, first out data
structure (LIFO). Stacks have many applications Reversing a word : Think about how you
would reverse a word using a stack. First all the letters are pushed into the stack and then
popped out one by one to get the reversed word. This would take linear time O(n).
Popping from a Stack

Pushing into a Stack

Array Practice in Stack


4
Time Complexity
Insertion : What would be the time for inserting an element into a stack? Since you're inserting an
element to the top of the stack, time is constant O(1). Deletion : Similar to insertion, deletion takes
contant time O(1) too. Search : How much time would it take to search for a particular element
inside a stack? Searching for a particular element would mean traversing the entire stack in the
worst case, so search operation takes linear time O(n).

Linked List Practice in Stack

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

Array Practice in Queue

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.

Key Differences Between Stack and Queue


● Stack follows LIFO mechanism whereas Queue follows FIFO mechanism to add and
remove elements.
● In a stack, the same end is used to insert and delete the elements. On the contrary, two
different ends are used in the queue to insert(Rear) and delete(Front) the elements.
● As Stacks have only one open end, that is the reason for using only one pointer to refer to
the top of the stack. But queues use two pointers to refer to the front and the rear ends of
the queue.
● Stack performs two operations known as push and pop while in Queue its known as
enqueue and dequeue.

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.

Learning Objectives of the Experiment


In this experiment on Linked Lists, you will learn following topics:
● Introduction
o What is linked list?
o How is linked list stored in memory?
● Types of Linked Lists(Operations and time complexity analysis of each type)
o Singly linked List
o Doubly Linked List
o Circular Linked List

Definition of Linked List


Linked list is a linear data structure where each data is a separate object (of same data type).
Linked list objects do not occupy the contiguous memory location as compared to the array which
is also a linear data structure where elements have contiguous memory allocation, instead linked
lists are linked using pointers. Elements of the linked lists are known as Nodes.
There are three types of linked lists:
● Singly Linked List
● Doubly Linked List
● Circular Linked List

Need for linked list


1. Memory Management: When you declare an array, the size of the array is fixed. You
cannot change the size of the array. And there might be a case where you are not able to consume
all the space that you have reserved as an array that means you are wasting a part of your memory
or there might be a case where you have consumed all of the space that was reserved as an array
and still you are not able to store your complete data.

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.

Representation of Linked List Experiment

Data Type used in Singly Linked List


To implement a singly linked list we first need a data type.Suppose we want to store the
information of students where each student have a name, roll number as Singly Linked List.Let us
declare a structure named Node for this. Our structure will contain the fields name, roll number,
which will be the first part of the Node. Second part of the Node contains a pointer that holds the
address of its next Node

What is Linked List?

11
Linked List vs Array

Types of Linked List

Singly Linked List


Implementation of Singly Linked List
Now we have data type in our hand that our singly linked list is going to store.
Next we need two pointers that mark the start and end of our singly linked list. Let us declare
them:
struct Node *head = NULL; struct Node *tail = NULL;
We have initialized these pointers with null to indicate that they do not hold the address of any
data yet i.e. no data has been stored in the list yet.
Now we are ready to insert the data in our singly linked list. First we allocate memory to store
data as:
struct Node newNode = (Node)malloc(sizeof(struct Node));

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

Insertion of node in Linked List (Case 1)

Insertion of node in Linked List (Case 2a)

13
Insertion of node in Linked List (Case 2b)

Insertion of node in Linked List (Case 2c)

Pictorial Representation of Linked List Experiment

14
Question: Convert to 91, 47, 7, 76, 56, 99

Deletion in singly linked list


We can check whether the list is empty or not using something like this:
int is Underflow(){
return start == NULL ? 1 : 0;
}
This function returns 1 if the list is empty, or returns 0 if the list is not empty.

Deletion of elements from Singly Linked List


Three cases may arie when we want to insert data at the front.
Case – 1 : When the list is empty i.e. head = NULL
Here, we don’t have to do anything because there is nothing that is to be deleted.
Case – 2 : When list contains single entry i.e. head != NULL and head = tail.
Case – 3 : When list contains two or more entries i.e. head != tail
Deletion at Front b. Deletion of a Node (not at front)

Deletion of node from Linked List (Case 2)

15
Deletion of node from Linked List (Case 3a)

Deletion of node from Linked List (Case 3b)

16
Deletion of tail node from Linked List

Practice

Doubly Linked List


Pictorial Representation of Singly Linked List vs Doubly Linked List

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.

Deletion of elements from Doubly Linked List


There is no change in implementation of deletion of a node, it is similar to that of in singly linked
list unlike parent of the child of deleted node must be added.

Practice

Question: Convert to 9, 39, 81, 75, 81, 52

18
Circular linked list
Pictorial Representation of Singly Linked List vs Circular Linked List

Insertion of elements into Circular Linked List


Insertion in circular linked list is similar to that of insertion in singly linked list, unlike next of the
last element is pointed to head.

Deletion of elements from Circular Linked List


There is no change in implementation of deletion of a node, it is similar to that of in singly 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

You might also like