2. LINKED LIST
A linked list is a linear data structure.
Nodes make up linked lists.
Nodes are structures made up of data and a
pointer to another node.
3. What is Linked List?
A linked list is a collection of nodes with various
fields
It contains data field and Address field or Link
field
Info field
Link Field/
Address
Field
Pointer to the
first node
4. Why Linked List?
What are the problems with Arrays
- Size is fixed
-Array Items are stored contiguously
-Insertions and deletion at particular position is
complex
Why Linked list ?
-Size is not fixed
-Data can be stored at any place
-Insertions and deletions are simple and faster
5. ARRAYS VS LINKED LISTS
Arrays Linked list
Fixed size: Resizing is expensive Dynamic size
Insertions and Deletions are inefficient:
Elements are usually shifted
Insertions and Deletions are efficient: No
shifting
Random access i.e., efficient indexing No random access
Not suitable for operations requiring
accessing elements by index such as sorting
No memory waste if the array is full or almost
full; otherwise may result in much memory
waste.
Since memory is allocated dynamically(acc. to
our need) there is no waste of memory.
Sequential access is faster [Reason: Elements in
contiguous memory locations]
Sequential access is slow [Reason: Elements not
in contiguous memory locations]
6. SINGLY LINKED LIST
Each node has only one link part
Each link part contains the address of the next
node in the list
Link part of the last node contains NULL value
which signifies the end of the node
7. SCHEMATIC REPRESENTATION
Here is a singly-linked list (SLL):
a b c d
Start
• Each node contains a value(data) and a
pointer to the next node in the list
• Start is the header pointer which points at
the first node in the list
9. STRUCTURE OF SINGLE LINKED LIST NODE
Struct node
{
int info;
Struct node * Next
} *start=NULL;
10. ACCESSING THE STRUCTURE MEMBERS
USING POINTER
struct Book
{
char name[10];
int price;
} ;
int main()
{
struct Book b;
struct Book* ptr = &b;
ptr->name = “MS";
ptr->price = 500;
}
11. Allocation of memory to new_node
New_node =( Struct node * ) malloc (sizeof(struct node));
It Returns address of the location with piece
of memory of the size struct. That is for
information field and address field
12. Operations of single linked list
Creation of node of link list
Insertion in a single linked list
Deletion from a single linked list
Searching in a single linked list
Reverse the single linked list
13. CREATING A NODE
struct node{
int data; // A simple node of a linked list
struct node*next;
}*start; //start points at the first node
start=NULL ; initialised to NULL at beginning
14. node* create( int num) //say num=1 is passed from main
{
struct node*ptr;
ptr= new node; //memory allocated dynamically
if(ptr==NULL)
„OVERFLOW‟ // no memory available
exit(1);
else
{
ptr->data=num;
ptr->next=NULL;
return ptr;
}
}
15. TO BE CALLED FROM MAIN() AS:-
void main()
{
node* ptr;
int data;
scanf(“%d”,&data);
ptr=create(data);
}
16. INSERTION DESCRIPTION
Insertion at the front of the single list
Insertion at the end of the single list
Insertion after the particular node of the single list
17. INSERTION AT THE TOP
Steps:
Create a Node
Set the node data Values
Connect the pointers
18. INSERTION DESCRIPTION
Follow the previous steps and we get
15 20 //
Start 10
Step 1 Step 2
Step 3
1000 2000
4000 4000
10
15 20
start //
2000
1000
4000
19. Functions to create new node of
linked list then inserting that
node in the front of linked list
20. Struct node* create-node( int num)
{
struct node* new-node;
new-node=(struct node* ) malloc ( sizeof (struct node));
if(newnode ==NULL)
{
„OVERFLOW‟ // no memory available
exit(1);
}
else
{
new-node->info=num;
new-node->link=NULL;
return new-node;
}
}
Node creation
Set the values of node
21. C Function to Insert an Element from the
front
Insertfront(struct node* start, int item)
{
struct node* temp = create-node(item);
temp->link=start;
start=temp;
return(start);
}
Create the node and
assign the values to
newnode in create-
node() function
Connect the pointers
24. INSERTION DESCRIPTION
Insertion at the top of the single list
Insertion at the end of the single list
Insertion after the particular node of the single list
25. INSERTION AT THE END
Steps:
Create a Node
Set the node data Values
Connect the pointers
26. INSERTION DESCRIPTION
Follow the previous steps and we get
15 20 10 //
Start
Step 1 Step 2
Step 3
1000 2000
4000
4000
10
15 20
start //
2000
1000
4000
27. Functions to create new node of linked
list then inserting that node in the
last of linked list
28. Struct node* create-node( int num)
{
struct node* new-node;
new-node=(struct node* ) malloc(sizeof (struct node));
if(newnode ==NULL)
{
„OVERFLOW‟ // no memory available
exit(1);
}
else
{
new-node->info=num;
new-node->link=NULL;
return new-node;
}
}
Node creation
Set the values of node
29. C Function to Insert an Element at the end
Void Insert-End( struct node* start, int item)
{
struct node* temp = create-node(item);
if(start==NULL)
{
start=temp;
print”n Node inserted successfully at the end…!!!n”;
}
else {
q = start;
while(q->link!=NULL)
{ q=q->link; }
q->link=temp;
}
}
Connect the
pointers
Create the node and
assign the values to new-
node in create-node()
function
33. INSERTION DESCRIPTION
Insertion at the top of the single list
Insertion at the end of the single list
Insertion after a particular node of the single list
34. INSERTION AFTER A PARTICULAR NODE
Steps:
Create a Node
Set the node data Values
Break pointer connection
Re-connect the pointers
36. Functions to create new node of linked
list then inserting that node after a
particular node in the linked list
37. Struct node* create-node( int num)
{
struct node* new-node;
new-node=(struct node* ) malloc ( sizeof (struct node));
if(newnode ==NULL)
{
„OVERFLOW‟ // no memory available
exit(1);
}
else
{
new-node->info=num;
new-node->link=NULL;
return new-node;
}
}
Node creation
Set the values of node
38. C Function to Insert an Element with data value item after the i
node
Insert-after(struct node* start, int item, int i)
{
struct node* temp = create-node(item);
Struct node* q;
q=start;
for( loc=1;loc<i;loc++)
{
q=q link;
if(q==NULL)
print ”Less than „i” nodes in the list…!!!”;
}
temp link=q->link;
q link=temp;
Connect the
pointers
Create the node and
assign the values to new-
node in create-node()
function
39. 10
1000
null 2000
5
4000
Operations on Singly Linked List
Insert after a particular node
Start = 1000
temp
If i=2 then
q=start
for( loc=1;loc<i;loc++)
q=q link;
After calling
Create-node(10)
2000
3000
15
3000
NULL
20
41. DELETION DESCRIPTION
Deleting from the top of the single list
Deleting from the end of the single list
Deleting after the particular node of the single list
42. DELETION DESCRIPTION
Deleting from the top of the single list
Deleting from the end of the single list
Deleting after the particular node of the single list
43. DELETING FROM THE TOP
Steps
Break the pointer connection
Re-connect the nodes
Delete the node
46. Struct node* del-first(struct node* start)
{
if(start == NULL)
print ”Error……List is empty”;
else
{
struct node* temp;
temp=start;
start=temp->link;
delete temp;
} return start;
}
temp is the node to be
deleted
Otherwise update start
with new start
Delete the node
If (start link = null)
Start=null and return start;
Check if list has single element
50. DELETION DESCRIPTION
Deleting from the top of the single list
Deleting from the end of the single list
Deleting after the particular node of the single list
51. DELETING FROM THE END
Steps
Break the pointer connection
Set previous node pointer to NULL
Delete the node
54. Struct node* del-last(struct node* start)
{
if(start==NULL)
print ”Error….List is empty”;
else
{
struct node* q=start;
while(q link link!=NULL)
q=q link;
struct node* temp = q link;
q link=NULL;
delete temp;
}
} return start
Store the node to be
deleted in temp and Set
link part of 2nd
Node to NULL
Delete the node
Finding the location
of 2nd last node
If (start link = null)
Start=null and return start;
Check if list has single element
57. Operations on Singly Linked List
Delete from the last
4000 1000
1000
10 NULL
15
start
Delete the node temp
58. DELETION DESCRIPTION
Deleting from the top of the single list
Deleting from the end of the single list
Deleting node after the particular node of the single list
59. DELETING NODE AFTER THE PARTICULAR
NODE
Steps
Set previous Node pointer to next node
Break Node pointer connection
Delete the node
62. Struct node* del-after( struct node* start, int data)
{
Struct node* q = start, *temp;
while( q info ! = data || q link != null)
{
q = q link; // q is the node with info as data
}
if (q info = data && q link != null)
{
temp = q link // temp is the node to be deleted
q link = temp link; // rearrange the pointers
delete( temp );
}
Else
Print “no such node exist”
Function to delete a node after the node with node-info value as
data
63. 10
1000
1000
2000
2000
15 NULL
20
4000
Operations on Singly Linked List
Delete after a particular node
Temp/Start
= 4000
suppose data = 10
while( q info ! = data || q link != null)
{
q = q link; // q is the node with info as data
}
64. 10
1000
2000
2000
2000
15 NULL
20
4000
Operations on Singly Linked List
Delete after a particular node
Start=4000
temp
if (q info = data && q link != null)
{
temp = q link // p is the node to be deleted
q link = temp link; // rearrange the pointers
delete( temp );
}
Else
Print “no such node exist”
q
65. Operations on Singly Linked List
Delete after a particular node
4000 2000
2000
10 NULL
20
start
Delete the node temp
66. SEARCHING A SINGLE LINKED LIST(SLL)
Searching involves finding the required element in the list
We can use various techniques of searching like linear search or
binary search where binary search is more efficient in case of
Arrays
But in case of linked list since random access is not available it
would become complex to do binary search in it
We can perform simple linear search traversal
67. In linear search each node is traversed till the data in
the node matches with the required value
void search(struct node* start, int x)
{
struct node* temp = start;
while(temp!=NULL)
{
if( temp data==x)
{
print “FOUND ”<< temp->data;
break;
}
temp=temp->next;
}
}
68. REVERSING A LINKED LIST
• We can reverse a linked list by reversing the direction
of the links between 2 nodes
69. We make use of 3 structure pointers say p,q,r
At any instant q will point to the node next to p and r will point to
the node next to q
NULL
Head P q r
NULL
• For next iteration p=q and q=r
• At the end we will change head to the last node
p q