Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Experiment No.

– 10
CASE STUDY
Aim – Identify best suited Data structures for solving real world problem.
Hardware/software required – Turbo C

1. INTRODUCTION
A data structure is a storage that is used to store and organize data. It is a way of arranging data on a
computer so that it can be accessed and updated efficiently.
A data structure is not only used for organizing the data. It is also used for processing, retrieving, and
storing data. There are different basic and advanced types of data structures that are used in almost
every program or software system that has been developed. So we must have good knowledge about
data structures.

Linear data structure: Data structure in which data elements are arranged sequentially or linearly,
where each element is attached to its previous and next adjacent elements, is called a linear data
structure.
Examples of linear data structures are array, stack, queue, linked list, etc.
Static data structure: Static data structure has a fixed memory size. It is easier to access the
elements in a static data structure.
An example of this data structure is an array.
Dynamic data structure: In dynamic data structure, the size is not fixed. It can be randomly updated
during the runtime which may be considered efficient concerning the memory (space) complexity of
the code.
Examples of this data structure are queue, stack, linked lists etc.
Non-linear data structure: Data structures where data elements are not placed sequentially or
linearly are called non-linear data structures. In a non-linear data structure, we can’t traverse all the
elements in a single run only.
Examples of non-linear data structures are trees and graphs.
Need Of Data structure:
The structure of the data and the synthesis of the algorithm are relative to each other. Data
presentation must be easy to understand so the developer, as well as the user, can make an efficient
implementation of the operation.
Data structures provide an easy way of organizing, retrieving, managing, and storing data.
Here is a list of the needs for data Structure.
1. Data structure modification is easy.
2. It requires less time.
3. Save storage memory space.
4. Data representation is easy.
5. Easy access to the large database.
LINKED LISTS
Like Arrays, a linked list is a linear data structure in which elements are not stored at contiguous
memory locations. The elements in a linked list are linked using pointers as shown in the below
image:

WHY LINKED LIST?


Arrays can be used to store linear data of similar types, but arrays have the following limitations:
The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage.
Insertion of a new element / Deletion of an existing element in an array of elements is expensive:
The room has to be created for the new elements and to create room existing elements have to be
shifted but in Linked list if we have the head node then we can traverse to any node through it and
insert new node at the required position.

Example:
In a system, if we maintain a sorted list of IDs in an array id[] = [1000, 1010, 1050, 2000, 2040].
If we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the
elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are used. For example, to
delete 1010 in id[], everything after 1010 has to be moved due to this so much work is being done
which affects the efficiency of the code.
ADVANTAGES OF LINKED LISTS OVER ARRAYS:

• Dynamic Array.
• Ease of Insertion/Deletion.
• Insertion at the beginning is a constant time operation and takes O(1) time, as compared to
arrays where inserting an element at the beginning takes O(n) time,where n is the number of
elements in the array.
DRAWBACKS OF LINKED LISTS:
• Random access is not allowed. We have to access elements sequentially starting from the
first node(head node). So we cannot do a binary search with linked lists efficiently with its
default implementation.
• Extra memory space for a pointer is required with each element of the list.
• Not cache-friendly. Since array elements are contiguous locations, there is the locality of
reference which is not there in the case of linked lists.
• It takes a lot of time in traversing and changing the pointers.
• Reverse traversing is not possible in singly linked lists.
• It will be confusing when we work with pointers.
• Direct access to an element is not possible in a linked list as in an array by index.
• Searching for an element is costly and requires O(n) time complexity.
• Sorting of linked lists is very complex and costly.
• Appending an element to a linked list is a costly operation, and takes O(n) time, where n is
the number of elements in the linked list, as compared to arrays that take O(1) time.
• TYPES OF LINKED LISTS:
1. Singly-linked list
2. Doubly linked list
3. Circular linked list
4. Doubly circular linked list

CHARACTERISTICS OF A LINKED LIST:


• A linked list uses extra memory to store links.
• During the initialization of the linked list, there is no need to know the size of the elements.
• Linked lists are used to implement stacks, queues, graphs, etc.
• The first node of the linked list is called the Head.
• The next pointer of the last node always points to NULL.
• In a linked list, insertion and deletion are possible easily.
• Each node of the linked list consists of a pointer/link which is the address of the next node.
• Linked lists can shrink or grow at any point in time easily.

OPERATIONS PERFORMED ON LINKED LIST:


• A linked list is a linear data structure where each node contains a value and a reference to
the next node. Here are some common operations performed on linked lists:
• Initialization: A linked list can be initialized by creating a head node with a reference to the
first node. Each subsequent node contains a value and a reference to the next node.
• Inserting elements: Elements can be inserted at the head, tail, or at a specific position in the
linked list.
• Deleting elements: Elements can be deleted from the linked list by updating the reference of
the previous node to point to the next node, effectively removing the current node from the
list.
• Searching for elements: Linked lists can be searched for a specific element by starting from
the head node and following the references to the next nodes until the desired element is
found.
• Updating elements: Elements in a linked list can be updated by modifying the value of a
specific node.
• Traversing elements: The elements in a linked list can be traversed by starting from the head
node and following the references to the next nodes until the end of the list is reached.
• Reversing a linked list: The linked list can be reversed by updating the references of each
node so that they point to the previous node instead of the next node.
APPLICATIONS OF THE LINKED LIST:
DIFFERENT APPLICATIONS OF LINKED LISTS ARE AS FOLLOWS:
1. Linked lists are used to implement stacks, queues, graphs, etc.
2. Linked lists are used to perform arithmetic operations on long integers.
3. It is used for the representation of sparse matrices.
4. It is used in the linked allocation of files.
5. It helps in memory management.
6. It is used in the representation of Polynomial Manipulation where each polynomial term
represents a node in the linked list.
7. Linked lists are used to display image containers. Users can visit past, current, and next
images.
8. They are used to store the history of the visited page.
9. They are used to perform undo operations.
10. Linked are used in software development where they indicate the correct syntax of a tag.
11. Linked lists are used to display social media feeds.
REAL-LIFE APPLICATIONS OF A LINKED LIST:
1. mage viewer – Previous and next images are linked and can be accessed by the next and
previous buttons.
2. Previous and next page in a web browser – We can access the previous and next URL
searched in a web browser by pressing the back and next buttons since they are linked as a
linked list.
3. Music Player – Songs in the music player are linked to the previous and next songs. So you
can play songs either from starting or ending of the list.
4. GPS navigation systems- Linked lists can be used to store and manage a list of locations and
routes, allowing users to easily navigate to their desired destination.
5. Robotics- Linked lists can be used to implement control systems for robots, allowing them to
navigate and interact with their environment.
6. Task Scheduling- Operating systems use linked lists to manage task scheduling, where each
process waiting to be executed is represented as a node in the list.
7. Image Processing- Linked lists can be used to represent images, where each pixel is
represented as a node in the list.
8. File Systems- File systems use linked lists to represent the hierarchical structure of
directories, where each directory or file is represented as a node in the list.
9. Symbol Table- Compilers use linked lists to build a symbol table, which is a data structure
that stores information about identifiers used in a program.
10. Undo/Redo Functionality- Many software applications implement undo/redo functionality
using linked lists, where each action that can be undone is represented as a node in a doubly
linked list.
11. Speech Recognition- Speech recognition software uses linked lists to represent the possible
phonetic pronunciations of a word, where each possible pronunciation is represented as a
node in the list.
12. Polynomial Representation- Polynomials can be represented using linked lists, where each
term in the polynomial is represented as a node in the list.
13. Simulation of Physical Systems- Linked lists can be used to simulate physical systems, where
each element in the list represents a discrete point in time and the state of the system at
that time.
2. PROBLEM DISCUSSION
Playlists offer several benefits to music lovers. They allow users to organize their favorite songs into
custom collections, which can be listened to in any order, making it easier for them to find and play
their favorite tunes. Playlists are also a great way to discover new music. Music streaming services
use algorithms to analyze users' listening habits and suggest new songs or artists that they might like
based on their preferences.

In addition, playlists can be shared with others, allowing users to share their music preferences with
friends and family. Sharing playlists can be a great way to bond with others over shared music tastes
or introduce friends to new artists or genres. It can also be a fun way to collaborate with others on a
playlist and create something that everyone can enjoy.

Moreover, playlists can be used for different purposes. For example, a workout playlist can help
motivate someone during their fitness routine, while a study playlist can help them focus and
concentrate better. A party playlist can set the mood for a social gathering, while a relaxing playlist
can help someone unwind after a long day.

In conclusion, playlists are a versatile and convenient feature that music applications offer to their
users. They allow users to customize their listening experience, discover new music, share their
musical tastes, and enhance their moods and activities.

TO DESIGN MUSIC PLAYLIST SYSTEM

This Project will help you to understand the basic operations on linked list such as adding, removing
and iterating a linked list forward and backward by using a real-world problem i.e PlayList. The main
motive of this project is to clear the operations of linked list like adding elements,deleting
elements,iterating a linked list,etc
3. ALGORITHM

Algorithm for Doubly Linked List for Playlist

1. Declare a struct Node that has the following elements:


o songTitle: to store the title of the song as a string
o artistName: to store the name of the artist as a string
o previous: to store the pointer to the previous node
o next: to store the pointer to the next node
2. Declare a struct Playlist that has the following elements:
o head: to store the pointer to the first node in the playlist
o tail: to store the pointer to the last node in the playlist
3. Declare a function addSong that takes in the following parameters:
o playlist: a pointer to the struct Playlist
o songTitle: a string that represents the title of the song
o artistName: a string that represents the name of the artist
4. Create a new node with the songTitle and artistName, and set the previous pointer to
the tail of the playlist and the next pointer to NULL.
5. If the head of the playlist is NULL, set the head of the playlist to the new node.
6. Otherwise, set the next pointer of the tail node to the new node.
7. Set the tail of the playlist to the new node.
8. Declare a function removeSong that takes in the following parameters:
o playlist: a pointer to the struct Playlist
o songTitle: a string that represents the title of the song
o artistName: a string that represents the name of the artist
9. If the head of the playlist is NULL, return.
10. Traverse the playlist using a while loop and compare the songTitle and artistName of
each node with the given songTitle and artistName.
11. If the node is found, update the previous and next pointers of the adjacent nodes to
remove the current node.
12. If the current node is the head of the playlist, update the head of the playlist to the
next node.
13. If the current node is the tail of the playlist, update the tail of the playlist to the
previous node.
14. Free the memory allocated to the current node.
15. Declare a function printPlaylist that takes in the following parameter:

• playlist: a pointer to the struct Playlist

16. Traverse the playlist using a while loop and print the songTitle and artistName of each
node.

End of Algorithm
4. CODE
struct Node {
char* songTitle;
char* artistName;
struct Node* next;
};

struct Playlist {
struct Node* head;
};

void addSong(struct Playlist* playlist, char* songTitle, char* artistName) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->songTitle = songTitle;
newNode->artistName = artistName;
newNode->next = NULL;

if (playlist->head == NULL) {
playlist->head = newNode;
} else {
struct Node* current = playlist->head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}

void removeSong(struct Playlist* playlist, char* songTitle, char* artistName) {


if (playlist->head == NULL) {
return;
}
struct Node* current = playlist->head;
struct Node* previous = NULL;

while (current != NULL) {


if (strcmp(current->songTitle, songTitle) == 0 && strcmp(current->artistName, artistName) == 0)
{
if (previous == NULL) {
playlist->head = current->next;
} else {
previous->next = current->next;
}
free(current);
return;
}
previous = current;
current = current->next;
}
}

void printPlaylist(struct Playlist* playlist) {


struct Node* current = playlist->head;
while (current != NULL) {
printf("%s - %s\\n", current->songTitle, current->artistName);
current = current->next;
}
}
5. CONCLUSION
Playlists are a versatile and convenient feature that music applications offer to their users. They allow
users to customize their listening experience, discover new music, share their musical tastes, and
enhance their moods and activities. The implementation of a playlist using a doubly linked list data
structure offers several benefits to music lovers, such as easy insertion and removal of songs and the
ability to traverse the list backwards. Playlists are a great way to enjoy music and enhance the overall
listening experience, whether it's for working out, studying, partying, or relaxing. In conclusion,
playlists are a versatile and convenient feature that music applications offer to their users. They allow
users to customize their listening experience, discover new music, share their musical tastes, and
enhance their moods and activities.

You might also like