Sahil Kumar DSA Ca2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

TECHNO INTERNATIONAL, NEWTOWN

Technical Report Writing for CA#2 Examination

Paper name – DATA STRUCTURE AND ALGORITHM


(PCCCS301)
Topic- Linked list

Name – Sahil Kumar Shaw


University Roll No. - 18700221111
Stream - IT(Sec-B)
Year – 2nd
Semester – 3rd
Date of Submission – 30/08/2022
INTRODUCTION
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.

Classification of Data Structure

Types of Data Structures

Depending on the organization of the elements, data structures are classified into two types:

1)Linear data structures: Elements are accessed in a sequential order but it is not
compulsory to store all elements sequentially.

Examples: Linked Lists, Stacks and Queues.

2) Non-linear data structures: Elements of this data structure are stored/accessed in a non-
linear order.

Examples: Trees and graphs.


Motivation
There are many other data structures that do the same thing as linked lists. Before discussing
linked lists it is important to understand the difference between linked lists and arrays.

➢ The songs in the Music Player are linked to the next and the previous song. We can play
songs either from the starting or the end of the list.

➢ In an Image Viewer, the next and the previous images are linked; hence they can be
accessed by the previous and the next button.

Methodology
The simplest form of linked lists — a singly linked list — is a series of nodes where each
individual node contains both a value and a pointer to the next node in the list.
 Additions (Add) grow the list by adding items to the end of the list.
 Removals (Remove) will always remove from a given position in the list.
 Search (Contains) will search the list for a value.

Example use cases:


 Storing values in a hash table to prevent collisions (more on this in a few posts)

 Remaking the amazing race!

Advantages Of Linked List:


 Dynamic data structure: A linked list is a dynamic arrangement so it can grow and
shrink at runtime by allocating and deallocating memory. So there is no need to give
the initial size of the linked list.
 No memory wastage: In the Linked list, efficient memory utilization can be achieved
since the size of the linked list increase or decrease at run time so there is no memory
wastage and there is no need to pre-allocate the memory.
 Implementation: Linear data structures like stack and queues are often easily
implemented using a linked list.
 Insertion and Deletion Operations: Insertion and deletion operations are quite easier
in the linked list. There is no need to shift elements after the insertion or deletion of an
element only the address present in the next pointer needs to be updated.

Disadvantages Of Linked List:

 Memory usage: More memory is required in the linked list as compared to an array.
Because in a linked list, a pointer is also required to store the address of the next
element and it requires extra memory for itself.
 Traversal: In a Linked list traversal is more time-consuming as compared to an array.
Direct access to an element is not possible in a linked list as in an array by index. For
example, for accessing a node at position n, one has to traverse all the nodes before it.
 Reverse Traversing: In a singly linked list reverse traversing is not possible, but in
the case of a doubly-linked list, it can be possible as it contains a pointer to the
previously connected nodes with each node. For performing this extra memory is
required for the back pointer hence, there is a wastage of memory.
 Random Access: Random access is not possible in a linked list due to its dynamic
memory allocation.

Linked List
A Linked list is a dynamic arrangement that contains a “link” to the structure containing the
subsequent items. It’s a set of structures ordered not by their physical placement in memory
(like an array) but by logical links that are stored as a part of the info within the structure
itself.

A linked list is another way to collect similar data. However, unlike an array, elements during
a linked list aren’t in consecutive memory locations. A linked list consists of nodes that are
connected with one another using pointers. The figure illustrates a linked list.

Linked lists have their own strengths and weaknesses, but they happen to be strong where
arrays are weak. Generally, arrays allocate the memory for all its elements in one block
whereas linked lists use an entirely different strategy. Linked lists allocate memory for each
element separately and only when necessary.

Types Of Linked List:

 Singly Linked List: It is the simplest type of linked list in which every node contains
some data and a pointer to the next node of the same data type. The node contains a
pointer to the next node means that the node stores the address of the next node in the
sequence. A single linked list allows the traversal of data only in one way.

 Doubly or Two Way Linked List: A doubly linked list or a two-way linked list is a
more complex type of linked list that contains a pointer to the next as well as the
previous node in sequence, Therefore, it contains three parts are data, a pointer to the
next node, and a pointer to the previous node. This would enable us to traverse the list
in the backward direction as well.
 Circular Linked List: A circular linked list is that in which the last node contains the
pointer to the first node of the list. While traversing a circular linked list, one can
begin at any node and traverse the list in any direction forward and backward until
reaching the same node where started. Thus, a circular linked list has no beginning
and no end.

 Circular Doubly Linked List: A Doubly Circular linked list or a circular two-way
linked list is a more complex type of linked-list that contains a pointer to the next as
well as the previous node in the sequence. The difference between the doubly linked
and circular doubly list is the same as that between a singly linked list and a circular
linked list. The circular doubly linked list does not contain null in the previous field of
the first node.

Analysis
Pseudocode:

Traversal: struct
node * temp = start;
printf(“\n list empty are-”);
while (temp!= NULL)
{
printf(‘%d “, temp -> data)
temp=temp -> next;
}

Insertion: at begining
struct node *NewNode;
NewNode=malloc(sizeof(struct node));
NewNode -> data = 40;
NewNode -> next= start;
start= NewNode;

Insertion: at the end


struct node *NewNode;
NewNode=malloc(sizeof(struct node));
NewNode-> data = 40;
NewNode->next = NULL;
struct node *temp = start;
while( temp->next ! = NULL)
{
temp=temp -> next;
}
temp -> next = NewNode;

Deletion: struct node * temp = start;


while(temp -> next -> next!= NULL)
{
temp=temp -> next;
}
temp -> next = NULL;

Search: int searchNode(struct node *head, int item)


{
struct node *temp = head;
while(temp != NULL)
{
if(temp->data == item)
return 1;
temp = temp->next;
}
}

Conclusion
Through this Report I learnt a lot about Data Structures and Linked Lists,
the usage, its types and characteristics, mechanism that how the concept
works, its advantages and disadvantages and its usage to the real world.
Due to this help we can solve more problems in better and efficient way
and could build upcoming projects using it.

You might also like