SlideShare a Scribd company logo
UNIT IV : Introduction to Data Structures
and Linked List
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
Syllabus – Unit Wise
6/30/2021 4.1 _ Introduction to Data Structures 2
List of Exercises
6/30/2021 4.1 _ Introduction to Data Structures 3
Text Book and Reference Book
6/30/2021 4.1 _ Introduction to Data Structures 4
Unit IV : Contents
1. Introduction to Data Structures
2. Classification
3. Introduction to Linked List
4. Linked lists vs Arrays
5. Singly linked list
6. Creating a list
7. Traversing a list
8. Adding a node
9. Deleting a node
10. Searching a list
11. Sorting a list
12. Printing linked list in reverse order
13. Copy a singly linked list
14. Destroying a list
6/30/2021 5
4.1 _ Introduction to Data Structures
Introduction to Data Structures
• A data structure is a specialized format for
organizing and storing data in a computer so that
it can be used effectively.
• More precisely, a data structure is a
– collection of data values,
– the relationships among them, and
– the functions or operations that can be applied to the
data.
• Different types of data structures are available to
suit different applications.
6/30/2021 4.1 _ Introduction to Data Structures 6
Classification of Data Structures
6/30/2021 4.1 _ Introduction to Data Structures 7
Primitive Data Structures
• Primitive data structures are basic data types
which are supported by a programming language.
• For example,
– integer
– character
– string
• Programmers can use these data types when
creating variables in their programs.
• The term "data type" and "primitive data type"
are often used interchangeably.
6/30/2021 4.1 _ Introduction to Data Structures 8
Non - Primitive Data Structures
• Non-primitive data structures are created by the
programmer using primitive data structures.
• Few examples,
– linked lists
– Stacks
– Queues
– trees etc.
• These non-primitive data structures are further
classified into linear and non-linear data
structures.
6/30/2021 4.1 _ Introduction to Data Structures 9
Linear Data Structures
• In linear data structure, the elements are accessed in a
sequential order.
• But it is not compulsory to store all elements sequentially
(Say, Linked list).
• For Examples,
– Array
– Queue
– Stack
– linked list
• They can be implemented in memory using two ways.
• The first method is by having a linear relationship between
elements by means of sequential memory locations.
• The second method is by having a linear relationship by
using links.
6/30/2021 4.1 _ Introduction to Data Structures 10
Non-Linear Data Structures
• The non-linear data structure does not
organize the data in a sequential manner.
• When the data elements are organised in
some arbitrary fashion without any
sequence, such data structures are called non-
linear data structures.
• For Example,
– Tree
– Graph
6/30/2021 4.1 _ Introduction to Data Structures 11
Linear and Non-Linear
6/30/2021 4.1 _ Introduction to Data Structures 12
Linear and Non-Linear
6/30/2021 4.1 _ Introduction to Data Structures 13
Thank you
6/30/2021 4.1 _ Introduction to Data Structures 14
UNIT IV : Introduction to Data Structures
and Linked List
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
Unit IV : Contents
1. Introduction to Data Structures
2. Classification
3. Introduction to Linked List
4. Linked lists vs Arrays
5. Singly linked list
6. Creating a list
7. Traversing a list
8. Adding a node
9. Deleting a node
10. Searching a list
11. Sorting a list
12. Printing linked list in reverse order
13. Copy a singly linked list
14. Destroying a list
6/30/2021 16
4.1 _ Introduction to Linked List
Introduction to Linked List
• A linked list is a linear data structure, in which
the
– elements are not stored at contiguous memory
locations and
– elements are linked using pointers.
• Each element (we will call it as a node) of a list
is comprising of two items :
– data and
– reference to the next node.
6/30/2021 4.1 _ Introduction to Linked List 17
Introduction to Linked List
• The last node has a reference to null.
• The entry point into a linked list is called the
Head of the list.
• It should be noted that Head is not a separate
node, but the reference to the first node.
• If the list is empty then the Head is a null
reference.
6/30/2021 4.1 _ Introduction to Linked List 18
Introduction to Linked List
• A linked list is a dynamic data structure.
• The number of nodes in a list is not fixed and can grow
and shrink on demand.
• Any application which has to deal with an unknown
number of objects will need to use a linked list.
• A node in linked list contains two types of fields
namely Data and Next.
• The left part of the node which contains data may
include a simple data type, an array, or a structure.
• The right part of the node contains a pointer to the
next node.
• Linked list is also called a self-referential data type,
since every node contains a pointer to another node
which is of same type.
6/30/2021 4.1 _ Introduction to Linked List 19
Introduction to Linked List
• A linked list contains a pointer variable, Head, which
stores the address of the first node in the list.
• The entire list can be traversed using this Head
pointer.
• The address of the first node is available in the Head
pointer.
• The address of its succeeding node will be stored in
Next part of the node.
• Using this method, the individual nodes of the list form
a chain of nodes.
• If Head = NULL, means the linked list is empty and
contains no nodes.
6/30/2021 4.1 _ Introduction to Linked List 20
Linked List Vs Arrays
• Arrays can be used to store linear data of similar types, but arrays have the
following limitations.
• 1) 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.
• 2) Inserting a new element in an array of elements is expensive because
the room has to be created for the new elements and to create room
existing elements have to be shifted.
• For example, in a system, if we maintain a sorted list of IDs in an array id[].
id[] = [1000, 1010, 1050, 2000, 2040].
• And 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.
6/30/2021 4.1 _ Introduction to Linked List 21
Linked List Vs Arrays
• Advantages over arrays
– 1) Dynamic size
– 2) Ease of insertion/deletion
• Drawbacks:
– 1) Random access is not allowed. We have to access
elements sequentially starting from the first node. So we
cannot do binary search with linked lists efficiently with
its default implementation.
– 2) Extra memory space for a pointer is required with each
element of the list.
– 3) Not cache friendly. Since array elements are contiguous
locations, there is locality of reference which is not there
in case of linked lists.
6/30/2021 4.1 _ Introduction to Linked List 22
Linked List Vs Arrays
• Both arrays and linked lists are a linear collection of data elements.
• But unlike an array, a linked list does not store its nodes in
consecutive memory locations.
• Another difference between an array and a linked list is that a
linked list does not allow random access of data. Nodes in a linked
list can be accessed only in a sequential manner.
• Another advantage of a linked list over an array is that we can add
any number of elements in the list. This is not possible in case of
an array.
• For example, if we declare an integer array as int sno[10], then the
array can store a maximum of 10 data elements and not even one
more than that. There is no such restriction in the case of a linked
list.
• Thus, linked lists provide an efficient way of storing related data and
perform basic operations such as insertion, deletion and updation
of information at the cost of the extra space required for storing the
address of the next node.
6/30/2021 4.1 _ Introduction to Linked List 23
Linked List Vs Arrays
6/30/2021 4.1 _ Introduction to Linked List 24
Linked List Vs Arrays
6/30/2021 4.1 _ Introduction to Linked List 25
Linked List Vs Arrays
• Below Figures gives a pictorial representation showing
how consecutive memory locations are allocated for
array, while in case of linked list random memory
locations are assigned to nodes, but each node is
connected to its next node using pointer.
6/30/2021 4.1 _ Introduction to Linked List 26
Memory Allocation and Deallocation
for a linked list
• In linked lists, data is stored in the form of
nodes and at runtime memory is allocated for
creating nodes using malloc() function.
6/30/2021 4.1 _ Introduction to Linked List 27
Memory Allocation and Deallocation
for a linked list
6/30/2021 4.1 _ Introduction to Linked List 28
6/30/2021 4.1 _ Introduction to Linked List 29
6/30/2021 4.1 _ Introduction to Linked List 30
6/30/2021 4.1 _ Introduction to Linked List 31
6/30/2021 4.1 _ Introduction to Linked List 32
6/30/2021 4.1 _ Introduction to Linked List 33
Creating 3 nodes in a linked list and Printing
6/30/2021 4.1 _ Introduction to Linked List 34
Thank you
6/30/2021 4.1 _ Introduction to Linked List 35
UNIT IV : Introduction to Data Structures
and Linked List
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
Unit IV : Contents
1. Introduction to Data Structures
2. Classification
3. Introduction to Linked List
4. Linked lists vs Arrays
5. Singly linked list
6. Creating a list
7. Traversing a list
8. Adding a node
9. Deleting a node
10. Searching a list
11. Sorting a list
12. Printing linked list in reverse order
13. Copy a singly linked list
14. Destroying a list
6/30/2021 37
4.3 _ Singly Linked List Creation and
Traversing
Different types of linked lists
• There are different types of linked lists
available. They are
– Singly linked list
– Doubly linked list
– Circular linked list
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
38
Singly linked list
• Singly Linked Lists are a type of data structure in which
each node in the list stores the contents and a pointer
or reference to the next node in the list.
• It does not store any pointer or reference to the
previous node.
• To store a singly linked list, only the reference or
pointer to the first node in that list must be stored.
• The last node in a single linked list points to nothing.
• A singly linked list can be traversed in only one
direction from Head to the last node
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
39
Doubly linked list
• A doubly linked list is a complex type of linked list in which a node
contains a pointer to the previous as well as the next node in the
sequence.
• A doubly linked list consists of data, a pointer to the next node and a
pointer to the previous node.
• The First and last node of a linked list contains a terminator generally a
NULL value, that determines the start and end of the list.
• Doubly linked list is sometimes also referred as bi-directional linked list
since it allows traversal of nodes in both direction.
• Doubly linked list can be used in navigation systems where both front and
back navigation is required. It is used by browsers to implement backward
and forward navigation of visited web pages i.e. back and forward button.
• It is one of the most efficient data structure to implement when
traversing in both direction is required.
• Doubly linked list uses extra memory when compared to array and singly
linked list.
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
40
Circular linked list
• In a circular linked list, the last node contains a pointer to
the first node of the list.
• It is basically a linear linked list that may be singly or
doubly.
• In the list every node points to the next node and last
node points to the first node, thus forming a circle.
• Since it forms a circle, it is called as a circular linked list.
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
41
Operations in Linked List
• Creating a List
• Traversing a List
• Adding a Node
• Deleting a Node
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
42
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
43
Creating 3 nodes in a linked list and Printing
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
44
Creating n nodes in a linked list and Traversing
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address
}*head;
void createList(int n);
void traverseList();
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
45
int main()
{
int n;
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("nData in the list n");
traverseList();
return 0;
}
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
46
/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int info, i;
head = (struct node *)malloc(sizeof(struct node));
printf("Enter the data of node 1: "); // Input data of node from the user
scanf("%d", &info);
head->data = info; // Link data field with info
head->next = NULL; // Link address field to NULL
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
47
// Create n - 1 nodes and add to list
temp = head; // to mention temp is the last node in the list
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
printf("Enter the data of node %d: ", i);
scanf("%d", &info);
newNode->data = info; // Link data field of newNode
newNode->next = NULL; // Make sure new node points to NULL
temp->next = newNode; // Link previous node with newNode
temp = temp->next; // Make current node as previous node
}
}
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
48
/*
* Display entire list
*/
void traverseList()
{
struct node *temp;
temp = head;
while(temp != NULL)
{
printf("Data = %dn", temp->data); // Print data of current node
temp = temp->next; // Move to next node
}
}
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
49
Output
Enter the total number of nodes: 3
Enter the data of node 1: 23
Enter the data of node 2: 56
Enter the data of node 3: 89
Data in the list
Data = 23
Data = 56
Data = 89
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
50
Thank you
6/30/2021
4.3 _ Singly Linked List Creation and
Traversing
51
UNIT IV : Introduction to Data Structures
and Linked List
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
Unit IV : Contents
1. Introduction to Data Structures
2. Classification
3. Introduction to Linked List
4. Linked lists vs Arrays
5. Singly linked list
6. Creating a list
7. Traversing a list
8. Adding a node
9. Deleting a node
10. Searching a list
11. Sorting a list
12. Printing linked list in reverse order
13. Copy a singly linked list
14. Destroying a list
6/30/2021 53
4.4 _ Adding a Node
Add a node in linked list
• If we want to add a node to an already
existing linked list, we will first find free space
in the memory and then use it to store the
information.
6/30/2021 4.4 _ Adding a Node 54
Example 1
• For example consider the linked list contains the Access
number of the book, title of the book, and a NEXT field
which stores the address of the next node in sequence.
• Now, if a new book is added to the library, then the
details of the new book should be entered in the linked
list. For adding this data, free space should be
identified and store the information.
• In Figure, the shaded portions show available free
spaces, and any one of them can be used for future
uses.
• The operating system takes care of the free memory
space without any intervention from the user or the
programmer.
6/30/2021 4.4 _ Adding a Node 55
Example 1 - Memory representation of linked list
6/30/2021 4.4 _ Adding a Node 56
Finding Free Space using AVIAL pointer
• The computer maintains a list of all free memory cells.
• The list of available space is called free pool.
• Every linked list has a pointer variable Head which
stores the address of the first node of the list.
• Likewise, for the free pool, we have a pointer variable
AVAIL which stores the address of the first free space.
• Now, when a new book is to be added, the memory
address pointed by AVAIL is taken and used to store
the desired information.
6/30/2021 4.4 _ Adding a Node 57
Example 1 – Finding Free Space using AVAIL Pointer
6/30/2021 4.4 _ Adding a Node 58
Example 1 - Memory representation of linked list
after adding a book
6/30/2021 4.4 _ Adding a Node 59
Adding a Node in Linked List
• A new node can be added to the already
existing linked list.
• A node can be added in three different ways.
– At the front of the given linked list
– At the middle of the given linked list
– At the end of the given linked list
6/30/2021 4.4 _ Adding a Node 60
Insert at the front of the given linked list
• Consider the linked list shown in the Figure.
Suppose we want to add a new node 5 at the
beginning of the list. The following steps will
be done in the linked list to add the node.
6/30/2021 4.4 _ Adding a Node 61
Step 1
• Step 1: Allocate memory for the new data.
6/30/2021 4.4 _ Adding a Node 62
Step 2
• Step 2: initialize its data value to 5.
6/30/2021 4.4 _ Adding a Node 63
Step 3
• Step 3: Add the newly created node as the
first node of the list. Now the Next part of the
new node will contain the address of Head.
6/30/2021 4.4 _ Adding a Node 64
Step 4
• Step 4: Now assign Head to point to the new
node. Now Head will have the starting
address of the linked list.
6/30/2021 4.4 _ Adding a Node 65
6/30/2021 4.4 _ Adding a Node 66
Insert at the middle of the given linked list
• It involves insertion after the specified node of the
linked list. We need to skip the desired number of
nodes in order to reach the node after which the new
node will be inserted. Consider the linked list shown in
Figure. Suppose we want to add a new node 25 after
the node 20 of the given list. The following steps will
be done in the linked list to add the node.
6/30/2021 4.4 _ Adding a Node 67
Step 1
• Step 1: Allocate memory for the new data.
6/30/2021 4.4 _ Adding a Node 68
Step 2
• Step 2: initialize its data value to 25.
6/30/2021 4.4 _ Adding a Node 69
Step 3
• Step 3: Read the element after which you ant
to insert.
6/30/2021 4.4 _ Adding a Node 70
Step 4
• Step 4: Assign variable ptr to point to Head node.
Move ptr to the next node until the Data part of the
ptr points to 20.
6/30/2021 4.4 _ Adding a Node 71
Step 5
• Step 5: Assign the NEXT part of the new node
to NEXT part of the ptr.
6/30/2021 4.4 _ Adding a Node 72
Step 6
• Step 6: Assign the NEXT part of the ptr to new
node.
6/30/2021 4.4 _ Adding a Node 73
6/30/2021 4.4 _ Adding a Node 74
6/30/2021 4.4 _ Adding a Node 75
Insert at the last of the given linked list
• It involves insertion at the last of the linked list. The
new node can be inserted as the only node in the list
or it can be inserted as the last one. Consider the
linked list shown in Figure. Suppose we want to add a
new node 45 at the last of the given list. The following
steps will be done in the linked list to add the node.
6/30/2021 4.4 _ Adding a Node 76
Step 1
• Step 1: Allocate memory for the new data.
6/30/2021 4.4 _ Adding a Node 77
Step 2
• Step 2: initialize its data value to 45.
6/30/2021 4.4 _ Adding a Node 78
Step 3
• Step 3: Assign variable ptr to point to Head node.
Move ptr to the next node until the Next part of the
ptr points to NULL.
6/30/2021 4.4 _ Adding a Node 79
Step 4
• Step 4: Assign the NEXT part of the new node
to NULL.
6/30/2021 4.4 _ Adding a Node 80
Step 5
• Step 5: Assign the NEXT part of the ptr to new
node.
6/30/2021 4.4 _ Adding a Node 81
6/30/2021 4.4 _ Adding a Node 82
6/30/2021 4.4 _ Adding a Node 83
Time Complexity
• Time complexity of insert_begin() is O(1) as it
does a constant amount of work.
• Time complexity of insert_middle() is O(1) as it
does a constant amount of work.
• Time complexity of insert_last() is O(n) where n
is the number of nodes in linked list. Since there
is a loop from head to end, the function does
O(n) work.
– This method can also be optimized to work in O(1) by
keeping an extra pointer to the tail of linked list
6/30/2021 4.4 _ Adding a Node 84
Thank you
6/30/2021 4.4 _ Adding a Node 85
UNIT IV : Introduction to Data Structures
and Linked List
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
Unit IV : Contents
1. Introduction to Data Structures
2. Classification
3. Introduction to Linked List
4. Linked lists vs Arrays
5. Singly linked list
6. Creating a list
7. Traversing a list
8. Adding a node
9. Deleting a node
10. Searching a list
11. Sorting a list
12. Printing linked list in reverse order
13. Copy a singly linked list
14. Destroying a list
6/30/2021 87
4.5 _ Deleting a Node
Deallocating using free()
• After the insertion, the address of the next
available free space is stored in AVAIL.
• Similarly when we delete a particular node from
an existing linked list or delete the entire linked
list, the space occupied by it must be given back
to the free pool so that the memory can be
reused by some other program that needs
memory space.
• The operating system does this task of adding
the freed memory to the free pool.
• The node can deallocated using free() function.
6/30/2021 4.5 _ Deleting a Node 88
Deleting a Node in Linked List
• A new node can be deleted to the already
existing linked list.
• A node can be deleted in three different ways.
– At the front of the given linked list
– At the middle of the given linked list
– At the end of the given linked list
6/30/2021 4.5 _ Deleting a Node 89
6/30/2021 4.5 _ Deleting a Node 90
Deletion at the front of the given linked list
Deletion at the front of the given linked list
• It involves deletion of a node from the
beginning of the list. Consider the linked list
shown in Figure. Suppose we want to delete
the node at the beginning of the list. The
following steps will be done in the linked list
to delete the node.
6/30/2021 4.5 _ Deleting a Node 91
Step 1
• Step 1: Assign variable ptr to Head node.
6/30/2021 4.5 _ Deleting a Node 92
Step 2
• Step 2: Assign Head to point ptr next.
6/30/2021 4.5 _ Deleting a Node 93
Step 3
• Step 3: Deallocate ptr .
6/30/2021 4.5 _ Deleting a Node 94
6/30/2021 4.5 _ Deleting a Node 95
6/30/2021 4.5 _ Deleting a Node 96
Deletion at the middle of the given linked list
Deletion at the middle of the given linked list
• It involves deletion of a node from the middle
of the list. Consider the linked list shown in
Figure. Suppose we want to delete the node
20 from the given list. The following steps will
be done in the linked list to delete the node.
6/30/2021 4.5 _ Deleting a Node 97
6/30/2021 4.5 _ Deleting a Node 98
6/30/2021 4.5 _ Deleting a Node 99
Deletion at the end of the given linked list
• It involves deletion of the last node from the
list. Consider the linked list shown in Figure.
Suppose we want to delete the last node
from the given list. The following steps will be
done in the linked list to delete the node.
6/30/2021 4.5 _ Deleting a Node 100
6/30/2021 4.5 _ Deleting a Node 101
6/30/2021 4.5 _ Deleting a Node 102
Time Complexity
• Time complexity of delete_begin() is O(1) as it
does a constant amount of work.
• Time complexity of delete_middle() is O(n) as
it does a constant amount of work.
• Time complexity of delete_last() is O(n) where
n is the number of nodes in linked list. Since
there is a loop from head to end, the function
does O(n) work.
6/30/2021 4.5 _ Deleting a Node 103
Thank you
6/30/2021 4.5 _ Deleting a Node 104
UNIT IV : Introduction to Data Structures
and Linked List
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
Unit IV : Contents
1. Introduction to Data Structures
2. Classification
3. Introduction to Linked List
4. Linked lists vs Arrays
5. Singly linked list
6. Creating a list
7. Traversing a list
8. Adding a node
9. Deleting a node
10. Searching a list
11. Sorting a list
12. Printing linked list in reverse order
13. Copy a singly linked list
14. Destroying a list
6/30/2021 106
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
Searching a List
• Searching is performed in order to find the
location of a particular element in the list.
• Searching any element in the list needs
traversing through the list and make the
comparison of every element of the list with
the specified element.
• If the element is matched with any of the list
element then the location of the element is
returned from the function.
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
107
Types of Search
• Linear Search
• Binary Search
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
108
Linear Search
• Linear search is the simplest search algorithm
and often called sequential search.
• In this type of searching, we simply traverse the
list completely and match each element of the
list with the item whose location is to be found.
• If the match found then location of the item is
returned otherwise the algorithm return NULL.
• Linear search is mostly used to search an
unordered list in which the items are not sorted.
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
109
Linear Search
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
110
void search()
{
struct node *temp1;
int value,flag,i=0;
temp1=head;
printf("Enter the value you want to search:");
scanf("%d",&value);
while(temp1 !=NULL)
{
if(temp1->data==value)
{
printf("Yes %d is present at location %d",value,i+1);
flag=0;
}
else
{
flag=1;
}
i++;
temp1=temp1->next;
}
if(flag!=0)
{
printf("No %d is not present",value);
}
}
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
111
Search an element
in a linked list
Binary Search
• Binary search is the search technique which works
efficiently on the sorted lists.
• Hence, in order to search an element into some list by
using binary search technique, we must ensure that
the list is sorted.
• Binary search follows divide and conquer approach in
which, the list is divided into two halves and the item
is compared with the middle element of the list.
• If the match is found then, the location of middle
element is returned otherwise, we search into either of
the halves depending upon the result produced
through the match.
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
112
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
113
Binary Search
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
114
Sorting a List
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
115
Sorting a List
• Many sorting algorithms available:
– Bubble sort
– Insertion sort
– Merge sort
– Quick sort
– Etc..
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
116
Sorting a List – using bubble sort
• To accomplish this task, we maintain two pointers: ptr1 and
ptr2.
• Initially,
– ptr1 point to head node and
– ptr2 will point to node next to ptr1.
• Traverse through the list till ptr1 points to null, by
comparing ptr1's data with ptr2's data.
• If ptr1's data is greater than the ptr2's data, then swap
data between them.
• In the above example,
– ptr1 will initially point to 9 and ptr2 will point to 7.
– Since, 9 is greater than 7, swap the data.
• Continue this process until the entire list is sorted in
ascending order
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
117
void bubblesort()
{
struct node *ptr1,*ptr2;
int temp;
ptr1 = head;
while (ptr1!=NULL)
{
ptr2=ptr1->next;
while(ptr2!= NULL)
{
if (ptr1->data > ptr2->data)
{
temp = ptr1->data;
ptr1->data=ptr2->data;
ptr2->data=temp;
}
ptr2 = ptr2->next;
}
ptr1 = ptr1->next;
}
}
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
118
Sorting a List
– using
bubble sort
Reversing a list
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
119
Reversing a list
• Using array method
• Using recursive method
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
120
Using array method
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
121
Using array method
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
122
Using array method
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
123
Using array method
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
124
Recursive Function
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
125
Structure of recursive function
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
126
Structure of recursive function
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
127
Execution of Factorial Function
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
128
Execution of Factorial Function
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
129
Execution of Factorial Function
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
130
Recursive Function Examples
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
131
Using Recursive Method
void reverse(struct node *ptr1); // Function Prototype
int main()
{
createList(n);
printf("nData in the list n");
traverseList();
reverse(head); // Function call or initial recursive call
return 0;
}
void reverse(struct node *ptr1) // Function definition
{
if(ptr1==NULL) // Base case
{
return;
}
else
{
reverse(ptr1->next); // Progressive case
printf("%dn",ptr1->data);
}
}
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
132
Copying a linked list
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
133
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
134
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
135
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
136
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
137
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
138
Destroying a linked list
• Use free() function.
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
139
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
140
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
141
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
142
Thank you
6/30/2021
4.5 _ Searching, Sorting, Reversing, Copying
and Detroying
143

More Related Content

PDF
Binary search tree operations
PPTX
Data Structures - Lecture 8 [Sorting Algorithms]
PPTX
Searching and sorting
PDF
PSP LAB MANUAL.pdf
PPTX
Binary tree traversal ppt
PPT
Data Structure and Algorithms Linked List
PPT
Quick Sort
PPTX
Linked List
Binary search tree operations
Data Structures - Lecture 8 [Sorting Algorithms]
Searching and sorting
PSP LAB MANUAL.pdf
Binary tree traversal ppt
Data Structure and Algorithms Linked List
Quick Sort
Linked List

What's hot (20)

PPTX
Hash table
PPTX
Tree Traversal Algorithm in Data Structure
PPT
Binary search tree in data structures
PPT
Chapter 1( intro &amp; overview)
PPTX
Linked list
PDF
GE3171-PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PPTX
linear probing
PDF
Linked list implementation of Queue
PPTX
Queue and its operations
PPT
Linked list
PPT
Queue implementation
DOCX
Selection sort lab mannual
PPTX
Searching, Sorting and Hashing Techniques
PPTX
AVL Tree Data Structure
PPT
Chapter 8: tree data structure
PPTX
Bubble sort
PDF
Data preprocessing using Machine Learning
PPTX
Ppt bubble sort
PPTX
single linked list
Hash table
Tree Traversal Algorithm in Data Structure
Binary search tree in data structures
Chapter 1( intro &amp; overview)
Linked list
GE3171-PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
linear probing
Linked list implementation of Queue
Queue and its operations
Linked list
Queue implementation
Selection sort lab mannual
Searching, Sorting and Hashing Techniques
AVL Tree Data Structure
Chapter 8: tree data structure
Bubble sort
Data preprocessing using Machine Learning
Ppt bubble sort
single linked list
Ad

Similar to Introduction to Data Structures and Linked List (20)

PPTX
Linear Data Structures - List, Stack and Queue
PPTX
Data Structures and Algorithms - Lec 05.pptx
PPTX
Linked lists linked lists vs Arrays.pptx
PPTX
LINKED LIST.pptx
PPTX
Data Structures-UNIT Four_Linked_List.pptx
PPTX
Lecture 2 - Linear Data Structures & Implementation.pptx
PPTX
Linked list in Data Structure and Algorithm
PPTX
unit 1.pptx
PPT
Unit 1 linked list
PPTX
DATA STRUCTURES AND LINKED LISTS IN C.pptx
PPTX
DATA STRUCTURES AND LINKED LISTS IN C.pptx
PPTX
link list.pptx complete notes detailed ans
PPTX
Data Structure and Algorithms by Sabeen Memon03.pptx
PPTX
DSA-Linked-List-.. learning process.pptx
PPTX
Linked list
PPTX
Linked list
PPTX
Data Structures_Linear data structures Linked Lists.pptx
PDF
Data Structures and Algorithms-DSA_Linkedlist_class 1.pdf
PDF
CSE_D:11:_Somnath Mallick_3rd semister.pdf
PPTX
linked list in data structure
Linear Data Structures - List, Stack and Queue
Data Structures and Algorithms - Lec 05.pptx
Linked lists linked lists vs Arrays.pptx
LINKED LIST.pptx
Data Structures-UNIT Four_Linked_List.pptx
Lecture 2 - Linear Data Structures & Implementation.pptx
Linked list in Data Structure and Algorithm
unit 1.pptx
Unit 1 linked list
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
link list.pptx complete notes detailed ans
Data Structure and Algorithms by Sabeen Memon03.pptx
DSA-Linked-List-.. learning process.pptx
Linked list
Linked list
Data Structures_Linear data structures Linked Lists.pptx
Data Structures and Algorithms-DSA_Linkedlist_class 1.pdf
CSE_D:11:_Somnath Mallick_3rd semister.pdf
linked list in data structure
Ad

More from Selvaraj Seerangan (20)

PDF
Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...
PDF
Unit 5 _ Fog Computing .pdf
PDF
CAT III Answer Key.pdf
PPTX
END SEM _ Design Thinking _ 16 Templates.pptx
PPTX
Design Thinking _ Complete Templates.pptx
PPTX
CAT 3 _ List of Templates.pptx
PPTX
[PPT] _ Unit 5 _ Evolve.pptx
PPTX
[PPT] _ Unit 4 _ Engage.pptx
PPTX
[PPT] _ Unit 3 _ Experiment.pptx
PPTX
CAT 2 _ List of Templates.pptx
PPTX
Design Thinking - Empathize Phase
PDF
CAT-II Answer Key.pdf
PDF
18CSL51 - Network Lab Manual.pdf
PDF
DS LAB MANUAL.pdf
PPTX
CAT 1 _ List of Templates.pptx
PPTX
[PPT] _ UNIT 1 _ COMPLETE.pptx
DOC
CAT-1 Answer Key.doc
PPTX
Unit 3 Complete.pptx
PDF
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf
PPTX
[PPT] _ Unit 2 _ Complete PPT.pptx
Unit 2,3,4 _ Internet of Things A Hands-On Approach (Arshdeep Bahga, Vijay Ma...
Unit 5 _ Fog Computing .pdf
CAT III Answer Key.pdf
END SEM _ Design Thinking _ 16 Templates.pptx
Design Thinking _ Complete Templates.pptx
CAT 3 _ List of Templates.pptx
[PPT] _ Unit 5 _ Evolve.pptx
[PPT] _ Unit 4 _ Engage.pptx
[PPT] _ Unit 3 _ Experiment.pptx
CAT 2 _ List of Templates.pptx
Design Thinking - Empathize Phase
CAT-II Answer Key.pdf
18CSL51 - Network Lab Manual.pdf
DS LAB MANUAL.pdf
CAT 1 _ List of Templates.pptx
[PPT] _ UNIT 1 _ COMPLETE.pptx
CAT-1 Answer Key.doc
Unit 3 Complete.pptx
[PPT] _ Unit 2 _ 9.0 _ Domain Specific IoT _Home Automation.pdf
[PPT] _ Unit 2 _ Complete PPT.pptx

Recently uploaded (20)

PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT
Project quality management in manufacturing
PPTX
UNIT 4 Total Quality Management .pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
web development for engineering and engineering
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Well-logging-methods_new................
PPTX
CH1 Production IntroductoryConcepts.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
OOP with Java - Java Introduction (Basics)
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Project quality management in manufacturing
UNIT 4 Total Quality Management .pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Operating System & Kernel Study Guide-1 - converted.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
web development for engineering and engineering
CYBER-CRIMES AND SECURITY A guide to understanding
Well-logging-methods_new................
CH1 Production IntroductoryConcepts.pptx

Introduction to Data Structures and Linked List

  • 1. UNIT IV : Introduction to Data Structures and Linked List By Mr.S.Selvaraj Asst. Professor (SRG) / CSE Kongu Engineering College Perundurai, Erode, Tamilnadu, India Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018. 20CST21 – Programming and Linear Data Structures
  • 2. Syllabus – Unit Wise 6/30/2021 4.1 _ Introduction to Data Structures 2
  • 3. List of Exercises 6/30/2021 4.1 _ Introduction to Data Structures 3
  • 4. Text Book and Reference Book 6/30/2021 4.1 _ Introduction to Data Structures 4
  • 5. Unit IV : Contents 1. Introduction to Data Structures 2. Classification 3. Introduction to Linked List 4. Linked lists vs Arrays 5. Singly linked list 6. Creating a list 7. Traversing a list 8. Adding a node 9. Deleting a node 10. Searching a list 11. Sorting a list 12. Printing linked list in reverse order 13. Copy a singly linked list 14. Destroying a list 6/30/2021 5 4.1 _ Introduction to Data Structures
  • 6. Introduction to Data Structures • A data structure is a specialized format for organizing and storing data in a computer so that it can be used effectively. • More precisely, a data structure is a – collection of data values, – the relationships among them, and – the functions or operations that can be applied to the data. • Different types of data structures are available to suit different applications. 6/30/2021 4.1 _ Introduction to Data Structures 6
  • 7. Classification of Data Structures 6/30/2021 4.1 _ Introduction to Data Structures 7
  • 8. Primitive Data Structures • Primitive data structures are basic data types which are supported by a programming language. • For example, – integer – character – string • Programmers can use these data types when creating variables in their programs. • The term "data type" and "primitive data type" are often used interchangeably. 6/30/2021 4.1 _ Introduction to Data Structures 8
  • 9. Non - Primitive Data Structures • Non-primitive data structures are created by the programmer using primitive data structures. • Few examples, – linked lists – Stacks – Queues – trees etc. • These non-primitive data structures are further classified into linear and non-linear data structures. 6/30/2021 4.1 _ Introduction to Data Structures 9
  • 10. Linear Data Structures • In linear data structure, the elements are accessed in a sequential order. • But it is not compulsory to store all elements sequentially (Say, Linked list). • For Examples, – Array – Queue – Stack – linked list • They can be implemented in memory using two ways. • The first method is by having a linear relationship between elements by means of sequential memory locations. • The second method is by having a linear relationship by using links. 6/30/2021 4.1 _ Introduction to Data Structures 10
  • 11. Non-Linear Data Structures • The non-linear data structure does not organize the data in a sequential manner. • When the data elements are organised in some arbitrary fashion without any sequence, such data structures are called non- linear data structures. • For Example, – Tree – Graph 6/30/2021 4.1 _ Introduction to Data Structures 11
  • 12. Linear and Non-Linear 6/30/2021 4.1 _ Introduction to Data Structures 12
  • 13. Linear and Non-Linear 6/30/2021 4.1 _ Introduction to Data Structures 13
  • 14. Thank you 6/30/2021 4.1 _ Introduction to Data Structures 14
  • 15. UNIT IV : Introduction to Data Structures and Linked List By Mr.S.Selvaraj Asst. Professor (SRG) / CSE Kongu Engineering College Perundurai, Erode, Tamilnadu, India Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018. 20CST21 – Programming and Linear Data Structures
  • 16. Unit IV : Contents 1. Introduction to Data Structures 2. Classification 3. Introduction to Linked List 4. Linked lists vs Arrays 5. Singly linked list 6. Creating a list 7. Traversing a list 8. Adding a node 9. Deleting a node 10. Searching a list 11. Sorting a list 12. Printing linked list in reverse order 13. Copy a singly linked list 14. Destroying a list 6/30/2021 16 4.1 _ Introduction to Linked List
  • 17. Introduction to Linked List • A linked list is a linear data structure, in which the – elements are not stored at contiguous memory locations and – elements are linked using pointers. • Each element (we will call it as a node) of a list is comprising of two items : – data and – reference to the next node. 6/30/2021 4.1 _ Introduction to Linked List 17
  • 18. Introduction to Linked List • The last node has a reference to null. • The entry point into a linked list is called the Head of the list. • It should be noted that Head is not a separate node, but the reference to the first node. • If the list is empty then the Head is a null reference. 6/30/2021 4.1 _ Introduction to Linked List 18
  • 19. Introduction to Linked List • A linked list is a dynamic data structure. • The number of nodes in a list is not fixed and can grow and shrink on demand. • Any application which has to deal with an unknown number of objects will need to use a linked list. • A node in linked list contains two types of fields namely Data and Next. • The left part of the node which contains data may include a simple data type, an array, or a structure. • The right part of the node contains a pointer to the next node. • Linked list is also called a self-referential data type, since every node contains a pointer to another node which is of same type. 6/30/2021 4.1 _ Introduction to Linked List 19
  • 20. Introduction to Linked List • A linked list contains a pointer variable, Head, which stores the address of the first node in the list. • The entire list can be traversed using this Head pointer. • The address of the first node is available in the Head pointer. • The address of its succeeding node will be stored in Next part of the node. • Using this method, the individual nodes of the list form a chain of nodes. • If Head = NULL, means the linked list is empty and contains no nodes. 6/30/2021 4.1 _ Introduction to Linked List 20
  • 21. Linked List Vs Arrays • Arrays can be used to store linear data of similar types, but arrays have the following limitations. • 1) 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. • 2) Inserting a new element in an array of elements is expensive because the room has to be created for the new elements and to create room existing elements have to be shifted. • For example, in a system, if we maintain a sorted list of IDs in an array id[]. id[] = [1000, 1010, 1050, 2000, 2040]. • And 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. 6/30/2021 4.1 _ Introduction to Linked List 21
  • 22. Linked List Vs Arrays • Advantages over arrays – 1) Dynamic size – 2) Ease of insertion/deletion • Drawbacks: – 1) Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists efficiently with its default implementation. – 2) Extra memory space for a pointer is required with each element of the list. – 3) Not cache friendly. Since array elements are contiguous locations, there is locality of reference which is not there in case of linked lists. 6/30/2021 4.1 _ Introduction to Linked List 22
  • 23. Linked List Vs Arrays • Both arrays and linked lists are a linear collection of data elements. • But unlike an array, a linked list does not store its nodes in consecutive memory locations. • Another difference between an array and a linked list is that a linked list does not allow random access of data. Nodes in a linked list can be accessed only in a sequential manner. • Another advantage of a linked list over an array is that we can add any number of elements in the list. This is not possible in case of an array. • For example, if we declare an integer array as int sno[10], then the array can store a maximum of 10 data elements and not even one more than that. There is no such restriction in the case of a linked list. • Thus, linked lists provide an efficient way of storing related data and perform basic operations such as insertion, deletion and updation of information at the cost of the extra space required for storing the address of the next node. 6/30/2021 4.1 _ Introduction to Linked List 23
  • 24. Linked List Vs Arrays 6/30/2021 4.1 _ Introduction to Linked List 24
  • 25. Linked List Vs Arrays 6/30/2021 4.1 _ Introduction to Linked List 25
  • 26. Linked List Vs Arrays • Below Figures gives a pictorial representation showing how consecutive memory locations are allocated for array, while in case of linked list random memory locations are assigned to nodes, but each node is connected to its next node using pointer. 6/30/2021 4.1 _ Introduction to Linked List 26
  • 27. Memory Allocation and Deallocation for a linked list • In linked lists, data is stored in the form of nodes and at runtime memory is allocated for creating nodes using malloc() function. 6/30/2021 4.1 _ Introduction to Linked List 27
  • 28. Memory Allocation and Deallocation for a linked list 6/30/2021 4.1 _ Introduction to Linked List 28
  • 29. 6/30/2021 4.1 _ Introduction to Linked List 29
  • 30. 6/30/2021 4.1 _ Introduction to Linked List 30
  • 31. 6/30/2021 4.1 _ Introduction to Linked List 31
  • 32. 6/30/2021 4.1 _ Introduction to Linked List 32
  • 33. 6/30/2021 4.1 _ Introduction to Linked List 33 Creating 3 nodes in a linked list and Printing
  • 34. 6/30/2021 4.1 _ Introduction to Linked List 34
  • 35. Thank you 6/30/2021 4.1 _ Introduction to Linked List 35
  • 36. UNIT IV : Introduction to Data Structures and Linked List By Mr.S.Selvaraj Asst. Professor (SRG) / CSE Kongu Engineering College Perundurai, Erode, Tamilnadu, India Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018. 20CST21 – Programming and Linear Data Structures
  • 37. Unit IV : Contents 1. Introduction to Data Structures 2. Classification 3. Introduction to Linked List 4. Linked lists vs Arrays 5. Singly linked list 6. Creating a list 7. Traversing a list 8. Adding a node 9. Deleting a node 10. Searching a list 11. Sorting a list 12. Printing linked list in reverse order 13. Copy a singly linked list 14. Destroying a list 6/30/2021 37 4.3 _ Singly Linked List Creation and Traversing
  • 38. Different types of linked lists • There are different types of linked lists available. They are – Singly linked list – Doubly linked list – Circular linked list 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 38
  • 39. Singly linked list • Singly Linked Lists are a type of data structure in which each node in the list stores the contents and a pointer or reference to the next node in the list. • It does not store any pointer or reference to the previous node. • To store a singly linked list, only the reference or pointer to the first node in that list must be stored. • The last node in a single linked list points to nothing. • A singly linked list can be traversed in only one direction from Head to the last node 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 39
  • 40. Doubly linked list • A doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the sequence. • A doubly linked list consists of data, a pointer to the next node and a pointer to the previous node. • The First and last node of a linked list contains a terminator generally a NULL value, that determines the start and end of the list. • Doubly linked list is sometimes also referred as bi-directional linked list since it allows traversal of nodes in both direction. • Doubly linked list can be used in navigation systems where both front and back navigation is required. It is used by browsers to implement backward and forward navigation of visited web pages i.e. back and forward button. • It is one of the most efficient data structure to implement when traversing in both direction is required. • Doubly linked list uses extra memory when compared to array and singly linked list. 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 40
  • 41. Circular linked list • In a circular linked list, the last node contains a pointer to the first node of the list. • It is basically a linear linked list that may be singly or doubly. • In the list every node points to the next node and last node points to the first node, thus forming a circle. • Since it forms a circle, it is called as a circular linked list. 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 41
  • 42. Operations in Linked List • Creating a List • Traversing a List • Adding a Node • Deleting a Node 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 42
  • 43. 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 43 Creating 3 nodes in a linked list and Printing
  • 44. 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 44
  • 45. Creating n nodes in a linked list and Traversing #include <stdio.h> #include <stdlib.h> /* Structure of a node */ struct node { int data; // Data struct node *next; // Address }*head; void createList(int n); void traverseList(); 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 45
  • 46. int main() { int n; printf("Enter the total number of nodes: "); scanf("%d", &n); createList(n); printf("nData in the list n"); traverseList(); return 0; } 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 46
  • 47. /* * Create a list of n nodes */ void createList(int n) { struct node *newNode, *temp; int info, i; head = (struct node *)malloc(sizeof(struct node)); printf("Enter the data of node 1: "); // Input data of node from the user scanf("%d", &info); head->data = info; // Link data field with info head->next = NULL; // Link address field to NULL 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 47
  • 48. // Create n - 1 nodes and add to list temp = head; // to mention temp is the last node in the list for(i=2; i<=n; i++) { newNode = (struct node *)malloc(sizeof(struct node)); printf("Enter the data of node %d: ", i); scanf("%d", &info); newNode->data = info; // Link data field of newNode newNode->next = NULL; // Make sure new node points to NULL temp->next = newNode; // Link previous node with newNode temp = temp->next; // Make current node as previous node } } 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 48
  • 49. /* * Display entire list */ void traverseList() { struct node *temp; temp = head; while(temp != NULL) { printf("Data = %dn", temp->data); // Print data of current node temp = temp->next; // Move to next node } } 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 49
  • 50. Output Enter the total number of nodes: 3 Enter the data of node 1: 23 Enter the data of node 2: 56 Enter the data of node 3: 89 Data in the list Data = 23 Data = 56 Data = 89 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 50
  • 51. Thank you 6/30/2021 4.3 _ Singly Linked List Creation and Traversing 51
  • 52. UNIT IV : Introduction to Data Structures and Linked List By Mr.S.Selvaraj Asst. Professor (SRG) / CSE Kongu Engineering College Perundurai, Erode, Tamilnadu, India Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018. 20CST21 – Programming and Linear Data Structures
  • 53. Unit IV : Contents 1. Introduction to Data Structures 2. Classification 3. Introduction to Linked List 4. Linked lists vs Arrays 5. Singly linked list 6. Creating a list 7. Traversing a list 8. Adding a node 9. Deleting a node 10. Searching a list 11. Sorting a list 12. Printing linked list in reverse order 13. Copy a singly linked list 14. Destroying a list 6/30/2021 53 4.4 _ Adding a Node
  • 54. Add a node in linked list • If we want to add a node to an already existing linked list, we will first find free space in the memory and then use it to store the information. 6/30/2021 4.4 _ Adding a Node 54
  • 55. Example 1 • For example consider the linked list contains the Access number of the book, title of the book, and a NEXT field which stores the address of the next node in sequence. • Now, if a new book is added to the library, then the details of the new book should be entered in the linked list. For adding this data, free space should be identified and store the information. • In Figure, the shaded portions show available free spaces, and any one of them can be used for future uses. • The operating system takes care of the free memory space without any intervention from the user or the programmer. 6/30/2021 4.4 _ Adding a Node 55
  • 56. Example 1 - Memory representation of linked list 6/30/2021 4.4 _ Adding a Node 56
  • 57. Finding Free Space using AVIAL pointer • The computer maintains a list of all free memory cells. • The list of available space is called free pool. • Every linked list has a pointer variable Head which stores the address of the first node of the list. • Likewise, for the free pool, we have a pointer variable AVAIL which stores the address of the first free space. • Now, when a new book is to be added, the memory address pointed by AVAIL is taken and used to store the desired information. 6/30/2021 4.4 _ Adding a Node 57
  • 58. Example 1 – Finding Free Space using AVAIL Pointer 6/30/2021 4.4 _ Adding a Node 58
  • 59. Example 1 - Memory representation of linked list after adding a book 6/30/2021 4.4 _ Adding a Node 59
  • 60. Adding a Node in Linked List • A new node can be added to the already existing linked list. • A node can be added in three different ways. – At the front of the given linked list – At the middle of the given linked list – At the end of the given linked list 6/30/2021 4.4 _ Adding a Node 60
  • 61. Insert at the front of the given linked list • Consider the linked list shown in the Figure. Suppose we want to add a new node 5 at the beginning of the list. The following steps will be done in the linked list to add the node. 6/30/2021 4.4 _ Adding a Node 61
  • 62. Step 1 • Step 1: Allocate memory for the new data. 6/30/2021 4.4 _ Adding a Node 62
  • 63. Step 2 • Step 2: initialize its data value to 5. 6/30/2021 4.4 _ Adding a Node 63
  • 64. Step 3 • Step 3: Add the newly created node as the first node of the list. Now the Next part of the new node will contain the address of Head. 6/30/2021 4.4 _ Adding a Node 64
  • 65. Step 4 • Step 4: Now assign Head to point to the new node. Now Head will have the starting address of the linked list. 6/30/2021 4.4 _ Adding a Node 65
  • 66. 6/30/2021 4.4 _ Adding a Node 66
  • 67. Insert at the middle of the given linked list • It involves insertion after the specified node of the linked list. We need to skip the desired number of nodes in order to reach the node after which the new node will be inserted. Consider the linked list shown in Figure. Suppose we want to add a new node 25 after the node 20 of the given list. The following steps will be done in the linked list to add the node. 6/30/2021 4.4 _ Adding a Node 67
  • 68. Step 1 • Step 1: Allocate memory for the new data. 6/30/2021 4.4 _ Adding a Node 68
  • 69. Step 2 • Step 2: initialize its data value to 25. 6/30/2021 4.4 _ Adding a Node 69
  • 70. Step 3 • Step 3: Read the element after which you ant to insert. 6/30/2021 4.4 _ Adding a Node 70
  • 71. Step 4 • Step 4: Assign variable ptr to point to Head node. Move ptr to the next node until the Data part of the ptr points to 20. 6/30/2021 4.4 _ Adding a Node 71
  • 72. Step 5 • Step 5: Assign the NEXT part of the new node to NEXT part of the ptr. 6/30/2021 4.4 _ Adding a Node 72
  • 73. Step 6 • Step 6: Assign the NEXT part of the ptr to new node. 6/30/2021 4.4 _ Adding a Node 73
  • 74. 6/30/2021 4.4 _ Adding a Node 74
  • 75. 6/30/2021 4.4 _ Adding a Node 75
  • 76. Insert at the last of the given linked list • It involves insertion at the last of the linked list. The new node can be inserted as the only node in the list or it can be inserted as the last one. Consider the linked list shown in Figure. Suppose we want to add a new node 45 at the last of the given list. The following steps will be done in the linked list to add the node. 6/30/2021 4.4 _ Adding a Node 76
  • 77. Step 1 • Step 1: Allocate memory for the new data. 6/30/2021 4.4 _ Adding a Node 77
  • 78. Step 2 • Step 2: initialize its data value to 45. 6/30/2021 4.4 _ Adding a Node 78
  • 79. Step 3 • Step 3: Assign variable ptr to point to Head node. Move ptr to the next node until the Next part of the ptr points to NULL. 6/30/2021 4.4 _ Adding a Node 79
  • 80. Step 4 • Step 4: Assign the NEXT part of the new node to NULL. 6/30/2021 4.4 _ Adding a Node 80
  • 81. Step 5 • Step 5: Assign the NEXT part of the ptr to new node. 6/30/2021 4.4 _ Adding a Node 81
  • 82. 6/30/2021 4.4 _ Adding a Node 82
  • 83. 6/30/2021 4.4 _ Adding a Node 83
  • 84. Time Complexity • Time complexity of insert_begin() is O(1) as it does a constant amount of work. • Time complexity of insert_middle() is O(1) as it does a constant amount of work. • Time complexity of insert_last() is O(n) where n is the number of nodes in linked list. Since there is a loop from head to end, the function does O(n) work. – This method can also be optimized to work in O(1) by keeping an extra pointer to the tail of linked list 6/30/2021 4.4 _ Adding a Node 84
  • 85. Thank you 6/30/2021 4.4 _ Adding a Node 85
  • 86. UNIT IV : Introduction to Data Structures and Linked List By Mr.S.Selvaraj Asst. Professor (SRG) / CSE Kongu Engineering College Perundurai, Erode, Tamilnadu, India Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018. 20CST21 – Programming and Linear Data Structures
  • 87. Unit IV : Contents 1. Introduction to Data Structures 2. Classification 3. Introduction to Linked List 4. Linked lists vs Arrays 5. Singly linked list 6. Creating a list 7. Traversing a list 8. Adding a node 9. Deleting a node 10. Searching a list 11. Sorting a list 12. Printing linked list in reverse order 13. Copy a singly linked list 14. Destroying a list 6/30/2021 87 4.5 _ Deleting a Node
  • 88. Deallocating using free() • After the insertion, the address of the next available free space is stored in AVAIL. • Similarly when we delete a particular node from an existing linked list or delete the entire linked list, the space occupied by it must be given back to the free pool so that the memory can be reused by some other program that needs memory space. • The operating system does this task of adding the freed memory to the free pool. • The node can deallocated using free() function. 6/30/2021 4.5 _ Deleting a Node 88
  • 89. Deleting a Node in Linked List • A new node can be deleted to the already existing linked list. • A node can be deleted in three different ways. – At the front of the given linked list – At the middle of the given linked list – At the end of the given linked list 6/30/2021 4.5 _ Deleting a Node 89
  • 90. 6/30/2021 4.5 _ Deleting a Node 90 Deletion at the front of the given linked list
  • 91. Deletion at the front of the given linked list • It involves deletion of a node from the beginning of the list. Consider the linked list shown in Figure. Suppose we want to delete the node at the beginning of the list. The following steps will be done in the linked list to delete the node. 6/30/2021 4.5 _ Deleting a Node 91
  • 92. Step 1 • Step 1: Assign variable ptr to Head node. 6/30/2021 4.5 _ Deleting a Node 92
  • 93. Step 2 • Step 2: Assign Head to point ptr next. 6/30/2021 4.5 _ Deleting a Node 93
  • 94. Step 3 • Step 3: Deallocate ptr . 6/30/2021 4.5 _ Deleting a Node 94
  • 95. 6/30/2021 4.5 _ Deleting a Node 95
  • 96. 6/30/2021 4.5 _ Deleting a Node 96 Deletion at the middle of the given linked list
  • 97. Deletion at the middle of the given linked list • It involves deletion of a node from the middle of the list. Consider the linked list shown in Figure. Suppose we want to delete the node 20 from the given list. The following steps will be done in the linked list to delete the node. 6/30/2021 4.5 _ Deleting a Node 97
  • 98. 6/30/2021 4.5 _ Deleting a Node 98
  • 99. 6/30/2021 4.5 _ Deleting a Node 99
  • 100. Deletion at the end of the given linked list • It involves deletion of the last node from the list. Consider the linked list shown in Figure. Suppose we want to delete the last node from the given list. The following steps will be done in the linked list to delete the node. 6/30/2021 4.5 _ Deleting a Node 100
  • 101. 6/30/2021 4.5 _ Deleting a Node 101
  • 102. 6/30/2021 4.5 _ Deleting a Node 102
  • 103. Time Complexity • Time complexity of delete_begin() is O(1) as it does a constant amount of work. • Time complexity of delete_middle() is O(n) as it does a constant amount of work. • Time complexity of delete_last() is O(n) where n is the number of nodes in linked list. Since there is a loop from head to end, the function does O(n) work. 6/30/2021 4.5 _ Deleting a Node 103
  • 104. Thank you 6/30/2021 4.5 _ Deleting a Node 104
  • 105. UNIT IV : Introduction to Data Structures and Linked List By Mr.S.Selvaraj Asst. Professor (SRG) / CSE Kongu Engineering College Perundurai, Erode, Tamilnadu, India Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018. 20CST21 – Programming and Linear Data Structures
  • 106. Unit IV : Contents 1. Introduction to Data Structures 2. Classification 3. Introduction to Linked List 4. Linked lists vs Arrays 5. Singly linked list 6. Creating a list 7. Traversing a list 8. Adding a node 9. Deleting a node 10. Searching a list 11. Sorting a list 12. Printing linked list in reverse order 13. Copy a singly linked list 14. Destroying a list 6/30/2021 106 4.5 _ Searching, Sorting, Reversing, Copying and Detroying
  • 107. Searching a List • Searching is performed in order to find the location of a particular element in the list. • Searching any element in the list needs traversing through the list and make the comparison of every element of the list with the specified element. • If the element is matched with any of the list element then the location of the element is returned from the function. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 107
  • 108. Types of Search • Linear Search • Binary Search 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 108
  • 109. Linear Search • Linear search is the simplest search algorithm and often called sequential search. • In this type of searching, we simply traverse the list completely and match each element of the list with the item whose location is to be found. • If the match found then location of the item is returned otherwise the algorithm return NULL. • Linear search is mostly used to search an unordered list in which the items are not sorted. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 109
  • 110. Linear Search 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 110
  • 111. void search() { struct node *temp1; int value,flag,i=0; temp1=head; printf("Enter the value you want to search:"); scanf("%d",&value); while(temp1 !=NULL) { if(temp1->data==value) { printf("Yes %d is present at location %d",value,i+1); flag=0; } else { flag=1; } i++; temp1=temp1->next; } if(flag!=0) { printf("No %d is not present",value); } } 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 111 Search an element in a linked list
  • 112. Binary Search • Binary search is the search technique which works efficiently on the sorted lists. • Hence, in order to search an element into some list by using binary search technique, we must ensure that the list is sorted. • Binary search follows divide and conquer approach in which, the list is divided into two halves and the item is compared with the middle element of the list. • If the match is found then, the location of middle element is returned otherwise, we search into either of the halves depending upon the result produced through the match. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 112
  • 113. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 113
  • 114. Binary Search 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 114
  • 115. Sorting a List 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 115
  • 116. Sorting a List • Many sorting algorithms available: – Bubble sort – Insertion sort – Merge sort – Quick sort – Etc.. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 116
  • 117. Sorting a List – using bubble sort • To accomplish this task, we maintain two pointers: ptr1 and ptr2. • Initially, – ptr1 point to head node and – ptr2 will point to node next to ptr1. • Traverse through the list till ptr1 points to null, by comparing ptr1's data with ptr2's data. • If ptr1's data is greater than the ptr2's data, then swap data between them. • In the above example, – ptr1 will initially point to 9 and ptr2 will point to 7. – Since, 9 is greater than 7, swap the data. • Continue this process until the entire list is sorted in ascending order 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 117
  • 118. void bubblesort() { struct node *ptr1,*ptr2; int temp; ptr1 = head; while (ptr1!=NULL) { ptr2=ptr1->next; while(ptr2!= NULL) { if (ptr1->data > ptr2->data) { temp = ptr1->data; ptr1->data=ptr2->data; ptr2->data=temp; } ptr2 = ptr2->next; } ptr1 = ptr1->next; } } 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 118 Sorting a List – using bubble sort
  • 119. Reversing a list 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 119
  • 120. Reversing a list • Using array method • Using recursive method 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 120
  • 121. Using array method 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 121
  • 122. Using array method 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 122
  • 123. Using array method 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 123
  • 124. Using array method 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 124
  • 125. Recursive Function 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 125
  • 126. Structure of recursive function 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 126
  • 127. Structure of recursive function 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 127
  • 128. Execution of Factorial Function 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 128
  • 129. Execution of Factorial Function 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 129
  • 130. Execution of Factorial Function 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 130
  • 131. Recursive Function Examples 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 131
  • 132. Using Recursive Method void reverse(struct node *ptr1); // Function Prototype int main() { createList(n); printf("nData in the list n"); traverseList(); reverse(head); // Function call or initial recursive call return 0; } void reverse(struct node *ptr1) // Function definition { if(ptr1==NULL) // Base case { return; } else { reverse(ptr1->next); // Progressive case printf("%dn",ptr1->data); } } 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 132
  • 133. Copying a linked list 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 133
  • 134. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 134
  • 135. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 135
  • 136. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 136
  • 137. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 137
  • 138. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 138
  • 139. Destroying a linked list • Use free() function. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 139
  • 140. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 140
  • 141. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 141
  • 142. 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 142
  • 143. Thank you 6/30/2021 4.5 _ Searching, Sorting, Reversing, Copying and Detroying 143