SlideShare a Scribd company logo
1 of 70
Download to read offline
LINK LIST
BY
MUKESH SAKLE
ASSISTANT PROFESSOR
DEPARTMENT OF INFORMATION TECHNOLOGY
Raghav Birla
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.
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
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
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]
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
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
Graphical Representation
10
2000
2000
3000
3000
15 NULL
20
1000
Singly Linked List
Start=1000
STRUCTURE OF SINGLE LINKED LIST NODE
Struct node
{
int info;
Struct node * Next
} *start=NULL;
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;
}
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
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
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
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;
}
}
TO BE CALLED FROM MAIN() AS:-
void main()
{
node* ptr;
int data;
scanf(“%d”,&data);
ptr=create(data);
}
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
INSERTION AT THE TOP
Steps:
 Create a Node
 Set the node data Values
 Connect the pointers
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
Functions to create new node of
linked list then inserting that
node in the front of linked list
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
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
10
1000
1000
2000
2000
15 NULL
20
4000
Operations on Singly Linked List
Insert from the front
Start = 1000
temp
temp->link=start,
10
1000
1000
2000
2000
15 NULL
20
4000
Operations on Singly Linked List
Insert from the front
Start=4000
start=temp
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
INSERTION AT THE END
Steps:
 Create a Node
 Set the node data Values
 Connect the pointers
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
Functions to create new node of linked
list then inserting that node in the
last of linked list
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
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
10
1000
null
2000
2000
15 NULL
20
4000
Operations on Singly Linked List
Insert at the end
Start = 1000
temp
q=start
While(q  link != null)
q=q  link
After calling
Create-node(10)
15
2000
2000
4000
4000
20 NULL
10
1000
Operations on Singly Linked List
Insert at the end
Start=1000
q link = temp
q
temp
void insert_at_end()
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
if(new_node == NULL)
printf("nFailed to Allocate Memory");
printf("nEnter the data : ");
scanf("%d",&new_node>data);
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
temp = start;
while(temp->next!=NULL)
{
temp = temp->next;
}
Temp->next = new_node;
}
}
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
INSERTION AFTER A PARTICULAR NODE
Steps:
 Create a Node
 Set the node data Values
 Break pointer connection
 Re-connect the pointers
INSERTION DESCRIPTION
Follow the previous steps and we get
15 10 20 //
Start
Step 1 Step 2
Step 4
1000
2000
4000
2000
10
15 20
start //
4000
1000
4000
15 20 //
Start
Step 3
//
1000
2000
Functions to create new node of linked
list then inserting that node after a
particular node in the linked list
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
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
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
Insert after a particular node
15
4000
4000
3000
3000
10 NULL
20
2000
Start=1000
2. q  link=temp;
15
4000
3000
3000
3000
10 NULL
20
2000
Start=1000
1. temp  link=q->link;
q temp
1000
2000
5
1000
2000
5
q temp
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
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
DELETING FROM THE TOP
Steps
 Break the pointer connection
 Re-connect the nodes
 Delete the node
DELETION DESCRIPTION
15 20
start
10
15 20
start
10
15 20
start 1
Functions to delete the front node from
the linked list
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
10
1000
1000
2000
2000
15 NUL
L
20
4000
Operations on Singly Linked List
Delete from the front
Temp=Start = 4000
temp=start
10
1000
1000
2000
2000
15 NULL
20
4000
Operations on Singly Linked List
Delete from the front
Start=1000
temp
temp=start, start=temp->link
1000 2000
2000
15 NULL
20
Operations on Singly Linked List
Delete from the front
start
Delete the node temp
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
DELETING FROM THE END
Steps
 Break the pointer connection
 Set previous node pointer to NULL
 Delete the node
DELETION DESCRIPTION
15 20
start
10
15
start
20
10
15
10
start
Functions to delete the last node from
the linked list
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
10
1000
1000
2000
2000
15 NULL
20
4000
Operations on Singly Linked List
Delete from the last
Temp=Start = 4000
struct node* q=start;
while(q  link  link!=NULL)
q=q  link;
10
1000
1000
2000
NULL
15 NUL
L
20
4000
Operations on Singly Linked List
Delete from the last
Start=4000
temp
struct node* temp;
temp = q  link;
q  link=NULL;
q
Operations on Singly Linked List
Delete from the last
4000 1000
1000
10 NULL
15
start
Delete the node temp
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
DELETING NODE AFTER THE PARTICULAR
NODE
Steps
 Set previous Node pointer to next node
 Break Node pointer connection
 Delete the node
DELETION DESCRIPTION
10 15 20
head
10 17
head
20
10
head
20
Functions to delete the node after the
particular node from the linked list
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
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
}
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
Operations on Singly Linked List
Delete after a particular node
4000 2000
2000
10 NULL
20
start
Delete the node temp
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
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;
}
}
REVERSING A LINKED LIST
• We can reverse a linked list by reversing the direction
of the links between 2 nodes
 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
CODE
{
node*temp,*prev,*curr;
if(start==NULL)
{
print "List is empty";
return;
}
prev=null;
curr=null
temp=start;
while(temp!=NULL)
{
prev=curr
curr=temp
temp=templink
curr->link=prev;
}
start=curr;
return start;
}
Struct node* reverse(struct node * start)

More Related Content

Similar to Singly Linked List

Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
Unit 1 LINEAR DATA STRUCTURES
Unit 1  LINEAR DATA STRUCTURESUnit 1  LINEAR DATA STRUCTURES
Unit 1 LINEAR DATA STRUCTURESUsha Mahalingam
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptxSajalFayyaz
 
Finalpresentationfordatastructure 161207052319
Finalpresentationfordatastructure 161207052319Finalpresentationfordatastructure 161207052319
Finalpresentationfordatastructure 161207052319Shaid Bin MD. Sifat
 
Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)shah alom
 
Final presentation for data structure
Final presentation for data structureFinal presentation for data structure
Final presentation for data structureshah alom
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdffortmdu
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queueskalyanineve
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Balwant Gorad
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked listAmit Vats
 

Similar to Singly Linked List (20)

Linked list
Linked list Linked list
Linked list
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Unit 1 LINEAR DATA STRUCTURES
Unit 1  LINEAR DATA STRUCTURESUnit 1  LINEAR DATA STRUCTURES
Unit 1 LINEAR DATA STRUCTURES
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Linkedlist1
Linkedlist1Linkedlist1
Linkedlist1
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
 
Linked list
Linked listLinked list
Linked list
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Finalpresentationfordatastructure 161207052319
Finalpresentationfordatastructure 161207052319Finalpresentationfordatastructure 161207052319
Finalpresentationfordatastructure 161207052319
 
Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)
 
Final presentation for data structure
Final presentation for data structureFinal presentation for data structure
Final presentation for data structure
 
Link List
Link ListLink List
Link List
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 
Unit 2 linked list and queues
Unit 2   linked list and queuesUnit 2   linked list and queues
Unit 2 linked list and queues
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
Linked list
Linked listLinked list
Linked list
 

Recently uploaded

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 

Recently uploaded (20)

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 

Singly Linked List

  • 1. LINK LIST BY MUKESH SAKLE ASSISTANT PROFESSOR DEPARTMENT OF INFORMATION TECHNOLOGY Raghav Birla
  • 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
  • 22. 10 1000 1000 2000 2000 15 NULL 20 4000 Operations on Singly Linked List Insert from the front Start = 1000 temp temp->link=start,
  • 23. 10 1000 1000 2000 2000 15 NULL 20 4000 Operations on Singly Linked List Insert from the front Start=4000 start=temp
  • 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
  • 30. 10 1000 null 2000 2000 15 NULL 20 4000 Operations on Singly Linked List Insert at the end Start = 1000 temp q=start While(q  link != null) q=q  link After calling Create-node(10)
  • 31. 15 2000 2000 4000 4000 20 NULL 10 1000 Operations on Singly Linked List Insert at the end Start=1000 q link = temp q temp
  • 32. void insert_at_end() { struct node *new_node,*current; new_node=(struct node *)malloc(sizeof(struct node)); if(new_node == NULL) printf("nFailed to Allocate Memory"); printf("nEnter the data : "); scanf("%d",&new_node>data); new_node->next=NULL; if(start==NULL) { start=new_node; current=new_node; } else { temp = start; while(temp->next!=NULL) { temp = temp->next; } Temp->next = new_node; } }
  • 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
  • 35. INSERTION DESCRIPTION Follow the previous steps and we get 15 10 20 // Start Step 1 Step 2 Step 4 1000 2000 4000 2000 10 15 20 start // 4000 1000 4000 15 20 // Start Step 3 // 1000 2000
  • 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
  • 40. Insert after a particular node 15 4000 4000 3000 3000 10 NULL 20 2000 Start=1000 2. q  link=temp; 15 4000 3000 3000 3000 10 NULL 20 2000 Start=1000 1. temp  link=q->link; q temp 1000 2000 5 1000 2000 5 q temp
  • 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
  • 44. DELETION DESCRIPTION 15 20 start 10 15 20 start 10 15 20 start 1
  • 45. Functions to delete the front node from the linked list
  • 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
  • 47. 10 1000 1000 2000 2000 15 NUL L 20 4000 Operations on Singly Linked List Delete from the front Temp=Start = 4000 temp=start
  • 48. 10 1000 1000 2000 2000 15 NULL 20 4000 Operations on Singly Linked List Delete from the front Start=1000 temp temp=start, start=temp->link
  • 49. 1000 2000 2000 15 NULL 20 Operations on Singly Linked List Delete from the front start Delete the node temp
  • 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
  • 53. Functions to delete the last node from the linked list
  • 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
  • 55. 10 1000 1000 2000 2000 15 NULL 20 4000 Operations on Singly Linked List Delete from the last Temp=Start = 4000 struct node* q=start; while(q  link  link!=NULL) q=q  link;
  • 56. 10 1000 1000 2000 NULL 15 NUL L 20 4000 Operations on Singly Linked List Delete from the last Start=4000 temp struct node* temp; temp = q  link; q  link=NULL; q
  • 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
  • 60. DELETION DESCRIPTION 10 15 20 head 10 17 head 20 10 head 20
  • 61. Functions to delete the node after the particular node from the linked list
  • 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
  • 70. CODE { node*temp,*prev,*curr; if(start==NULL) { print "List is empty"; return; } prev=null; curr=null temp=start; while(temp!=NULL) { prev=curr curr=temp temp=templink curr->link=prev; } start=curr; return start; } Struct node* reverse(struct node * start)