SlideShare a Scribd company logo
1 of 49
Heap Memory
Heap memory is a common pool of free memory used for
dynamic memory allocations within an application.
heap memory is a part of RAM (Random Access Memory).
On the other hand, heap memory is a type of dynamic memory
allocation used for storing objects and data structures that require
a longer lifespan than stack memory.
What happens if heap memory is full?
When the heap becomes full, garbage is collected. During the
garbage collection objects that are no longer used are cleared,
thus making space for new objects.
What are Linked Lists
⚫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.
⚫Usually the pointer is called next.
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]
Types of lists
⚫Singly Linked list
⚫Doubly linked list
⚫Circular linked list
⚫Circular double linked list
Singly Linked List
⚫Each node has onlyone link part
⚫Each link partcontains theaddressof the next node in
the list
⚫Link partof the last nodecontains NULL valuewhich
signifies theend of the node
Schematic representation
a b c d
⚫Here is a singly-linked list (SLL):
myList
•Each nodecontains avalue(data) and a pointer
to the next node in the list
• myList is the headerpointer which pointsat
the first node in the list
Basic Operations on a list
• Creating a List
• Inserting an element in a list
• Deleting an element from a list
• Searching a list
Creating a node
struct node{
int info;
struct node *next;
}*first=NULL, *temp=NULL, *last=NULL;
void create()
{
temp=(struct node *)malloc(sizeof(struct node));
printf(“Enter the information to be stored”);
scanf(“%d”,&temp->info);
temp->next=NULL;
}
Inserting the node in a SLL
There are 2 cases here:-
 Insertion at the beginning
 Insertion at the end
Insertion at the beginning
Thereare twosteps to be followed:-
a) Make the next pointer of the node point towards the first
node of the list
b) Make the start pointer point towards this new node
 If the list is empty simply make the start pointer
point towards the new node;
InsertANodeAtTheBeginning
void insertbeg()
{
if(first==NULL)
{
create();
first=temp;
last=first;
}
else
{
create();
temp->next=first;
first=temp;
}
}
void create()
{
temp=(struct node *)malloc(sizeof(struct
node));
printf(“Enter the information to be
stored”);
scanf(“%d”,&temp->info);
temp->next=NULL;
}
Inserting at the end
Here we simply need to make the next pointer of
the last node point to the new node
InsertANodeAtTheEnd
void insertEnd()
{
if(first==NULL)
{
create();
first=temp;
last=first;
}
else
{
create();
last->next=temp;
last=temp;
}
}
void create()
{
temp=(struct node *)malloc(sizeof(struct
node));
printf(“Enter the information to be
stored”);
scanf(“%d”,&temp->info);
temp->next=NULL;
}
Display Elements
void display()
{
struct node *temp1;
temp1=first;
if(temp1==NULL)
{
printf(“List is Empty”);
}
printf(“The elements of the linked list are:”);
while(temp1!=NULL)
{
printf(“%d”,temp1->info);
temp1=temp1->next;
}
}
Deleting a node in SLL
Here also we have 2 cases:-
 Deleting the first node
Deleting the last node
Deleting the first node
three
two
one
Here we apply 2 steps:-
 Making the start pointer point towards the 2nd
node
 Deleting the first node using delete keyword
start
void deleteBeg()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
printf(“The item deleted=%d”,temp->info);
free(temp);
first=last=NULL;
}
else
{
first=temp->next;
printf(“The item deleted=%d”,temp->info);
free(temp);
}
}
Deleting the last node
node3
node2
node1
Here we apply 2 steps:-
 Making the second last node’s next pointer point to NULL
 Deleting the last node via delete keyword
start
void deleteEnd()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
printf(“The item deleted=%d”,temp->info);
free(temp);
first=last=NULL;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
printf(“The item deleted=%d”,last->info);
free(last);
temp->next=NULL;
last=temp;
}
}
Doubly Linked List
1. Doubly linked list is a linked data structure that consists of a set of sequentially
linked records called nodes.
2 . Each node contains three fields ::
-: one is data part which contain data only.
-:two other field is links part that are point or
references to the previous or to the next node
in the sequence of nodes.
3. The beginning and ending nodes' previous and next
links, respectively, point to some kind of terminator,
typically a sentinel node or null to facilitate traversal of
the list.
NODE
A B C
A doubly linked listcontain three fields: an integervalue, the
link to the next node, and the link to the previous node.
previous data next
NULL 11 786
786
200 400
200 656 400 786 777 NULL
DLL’s compared to SLL’s
⚫Advantages:
⚫Can be traversed in either
direction (may be essential
for some programs)
⚫Some operations, such as
deletion and inserting
before a node, become
easier
⚫Disadvantages:
⚫Requires more space
⚫List manipulations are
slower (because more
links must be changed)
⚫Greater chance of having
bugs (because more links
must be manipulated)
Structure of DLL
//holds theaddress of previous node
struct node
{
int info;
struct node*next;
struct node*prev;
} *first=NULL, *last=NULL, *temp=NULL, ;
.Data .next
previous.
inf
Creating a node in DLL
void create()
{
temp=(struct node *)malloc(sizeof(struct node));
printf(“Enter the information to be stored”);
scanf(“%d”,&temp->info);
temp->next=NULL;
temp->prev=NULL;
}
Inserting at beginning
void insertbeg()
{
if(first==NULL)
{
create();
first=temp;
last=first;
}
else
{
create();
temp->next=first;
first->prev=temp;
first=temp;
}
}
Inserting at beginning
void create()
{
temp=(struct node *)malloc(sizeof(struct node));
printf(“Enter the information to be stored”);
scanf(“%d”,&temp->info);
temp->next=NULL;
temp->prev=NULL;
}
Inserting at the end
void insertEnd()
{
if(first==NULL)
{
create();
first=temp;
last=first;
}
else
{
last->next=temp;
temp->prev=last;
last=temp;
}
}
Inserting at the end
void create()
{
temp=(struct node *)malloc(sizeof(struct node));
printf(“Enter the information to be stored”);
scanf(“%d”,&temp->info);
temp->next=NULL;
temp->prev=NULL;
}
Deleting a node
• Nodedeletion from a DLL involves changing two links
• In thisexample,we will delete node b
myDLL
a b c
• Wedon’t have todo anything about the links in node b
• Garbagecollection will takecareof deleted nodes
• Deletion of the first nodeor the last node isa special
case
void deleteBeg()
{
struct node *temp;
temp=first;
if(first==NULL)
{printf(“List is empty”);}
if(temp->next==NULL)
{
printf(“The item deleted=%d”,temp->info);
free(temp);
first=last=NULL;
}
else
{
first=first->next;
printf(“The item deleted=%d”,temp->info);
free(temp);
}
}
void deleteEnd()
{
struct node *temp,*temp2;
temp=first;
if(first==NULL)
{printf(“List is empty”);}
if(temp->next==NULL)
{
printf(“The item deleted=%d”,temp->info);
free(temp);
first=last=NULL;
}
else
{
temp2=last->prev;
temp2->next=NULL;
printf(“The item deleted=%d”,last->info);
free(last);
last=temp2;
}
}
Display Elements
void display()
{
struct node *temp1;
temp1=first;
if(temp1==NULL)
{
printf(“List is Empty”);
}
printf(“The elements of the linked list are:”);
while(temp1!=NULL)
{
printf(“%d”,temp1->info);
temp1=temp1->next;
}
}
APPLICATIONS OF LINKED LIST
1.Applications that have an MRU list (a linked list of file
names)
2.The cache in your browser that allows you to hit the BACK
button (a linked list of URLs)
3.Undo functionality in Photoshop or Word (a linked list of
state)
4.A stack, hash table, and binary tree can be implemented
using a doubly linked list.
• Polynomials
6 2 3 8
0 2
Index
represents
exponents
-3 18 0 0 23
0 4
2
•Array Implementation:
• p1(x) = 8x3 + 3x2 + 2x + 6
• p2(x) = 23x4 + 18x - 3
p1(x) p2(x)
•This is whyarrays aren’tgood to represent
polynomials:
• p3(x) = 16x21 - 3x5 + 2x + 6
6 2 0 0 -3 0 ………… 0 16
WASTE OF SPACE!
• Advantages of using an Array:
• onlygood for non-sparse polynomials.
• ease of storageand retrieval.
• Disadvantagesof using an Array:
• have toallocatearray size ahead of time.
•huge array size required for sparse
polynomials. Waste of spaceand runtime.
• Polynomial Representation
• Linked list Implementation:
• p1(x) = 23x9 + 18x7 + 41x6 + 163x4 + 3
• p2(x) = 4x6 + 10x4 + 12x + 8
23 9 18 7 41 6 18 7 3 0
4 6 10 4 12 1 8 0
P1
P2
NODE (contains coefficient & exponent)
TAIL (contains pointer)
• Advantages of using a Linked list:
•savespace (don’t have toworryabout sparse
polynomials) and easy to maintain
•don’t need toallocate list size and can
declare nodes (terms) onlyas needed
• Disadvantagesof using a Linked list :
• can’tgo backwards through the list
•can’t jump to the beginning of the list from
theend.
Polynomials
A(x)
em 1
am 1x am
em 2 e0
2 x ... a0 x
Representation
struct polynode {
int coef;
int exp;
struct polynode * next;
};
typedef struct polynode *polyptr;
coef exp next
•Adding polynomials using a Linked list
representation: (storing the result in p3)
Todo this, we have to break the process down to
cases:
• Case 1: exponent of p1 > exponent of p2
• Copy node of p1 toend of p3.
[go to next node]
• Case 2: exponent of p1 < exponent of p2
• Copy node of p2 toend of p3.
[go to next node]
• Case 3: exponentof p1 = exponent of p2
•Create a new node in p3 with thesame
exponent and with the sum of the
coefficientsof p1 and p2.
Example
3 14 2 8
a
8 14 -3 10
b
b
8x14
3x10
10x 6
1 0 null
10 6 null
a 3x14
2x8
1
Adding Polynomials
3 14
a
8 14
2 8
-3 10
1 0
10 6
b
11 14
d
a->expon == b->expon
3 14 1 0
a
2 8
-3 10 10 6
b
8 14
11 14 -3 10 a->expon < b->expon
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10
d
a->expon > b->expon
2 8
THANK YOU

More Related Content

Similar to linkedlist.pptx

DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxVeerannaKotagi1
 
Computer notes - Hashing
Computer notes - HashingComputer notes - Hashing
Computer notes - Hashingecomputernotes
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
L1 - Recap.pdf
L1 - Recap.pdfL1 - Recap.pdf
L1 - Recap.pdfIfat Nix
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptOliverKane3
 
Stacks and Queues with Linked List.pdf
Stacks and Queues with Linked List.pdfStacks and Queues with Linked List.pdf
Stacks and Queues with Linked List.pdfGKalyani4
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35ecomputernotes
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seoJinTaek Seo
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rathSANTOSH RATH
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxMeghaKulkarni27
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfJAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfamrishinda
 
Describe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdfDescribe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdfarihantstoneart
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxtienlivick
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfankit11134
 

Similar to linkedlist.pptx (20)

DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docx
 
Linked list
Linked listLinked list
Linked list
 
Computer notes - Hashing
Computer notes - HashingComputer notes - Hashing
Computer notes - Hashing
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
L1 - Recap.pdf
L1 - Recap.pdfL1 - Recap.pdf
L1 - Recap.pdf
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
 
Stacks and Queues with Linked List.pdf
Stacks and Queues with Linked List.pdfStacks and Queues with Linked List.pdf
Stacks and Queues with Linked List.pdf
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
 
Data structure
 Data structure Data structure
Data structure
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
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
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfJAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
 
Describe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdfDescribe a data structure that supports both removeMin() and rem.pdf
Describe a data structure that supports both removeMin() and rem.pdf
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
DSModule2.pptx
DSModule2.pptxDSModule2.pptx
DSModule2.pptx
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 

More from MeghaKulkarni27

Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////MeghaKulkarni27
 
data structures and applications power p
data structures and applications power pdata structures and applications power p
data structures and applications power pMeghaKulkarni27
 
Different string operations....................
Different string operations....................Different string operations....................
Different string operations....................MeghaKulkarni27
 
virtual reality...............................
virtual reality...............................virtual reality...............................
virtual reality...............................MeghaKulkarni27
 
circularlinklist-190205164051.pptx
circularlinklist-190205164051.pptxcircularlinklist-190205164051.pptx
circularlinklist-190205164051.pptxMeghaKulkarni27
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxMeghaKulkarni27
 

More from MeghaKulkarni27 (11)

Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////
 
data structures and applications power p
data structures and applications power pdata structures and applications power p
data structures and applications power p
 
Different string operations....................
Different string operations....................Different string operations....................
Different string operations....................
 
virtual reality...............................
virtual reality...............................virtual reality...............................
virtual reality...............................
 
positive.pptx
positive.pptxpositive.pptx
positive.pptx
 
circularlinklist-190205164051.pptx
circularlinklist-190205164051.pptxcircularlinklist-190205164051.pptx
circularlinklist-190205164051.pptx
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
 
DS_PPT.pptx
DS_PPT.pptxDS_PPT.pptx
DS_PPT.pptx
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 
DS_PPT.ppt
DS_PPT.pptDS_PPT.ppt
DS_PPT.ppt
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
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
 

Recently uploaded (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
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🔝
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
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 🔝✔️✔️
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
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
 
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
 

linkedlist.pptx

  • 2. Heap memory is a common pool of free memory used for dynamic memory allocations within an application. heap memory is a part of RAM (Random Access Memory). On the other hand, heap memory is a type of dynamic memory allocation used for storing objects and data structures that require a longer lifespan than stack memory.
  • 3. What happens if heap memory is full? When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.
  • 4. What are Linked Lists ⚫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. ⚫Usually the pointer is called next.
  • 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. Types of lists ⚫Singly Linked list ⚫Doubly linked list ⚫Circular linked list ⚫Circular double linked list
  • 7. Singly Linked List ⚫Each node has onlyone link part ⚫Each link partcontains theaddressof the next node in the list ⚫Link partof the last nodecontains NULL valuewhich signifies theend of the node
  • 8. Schematic representation a b c d ⚫Here is a singly-linked list (SLL): myList •Each nodecontains avalue(data) and a pointer to the next node in the list • myList is the headerpointer which pointsat the first node in the list
  • 9. Basic Operations on a list • Creating a List • Inserting an element in a list • Deleting an element from a list • Searching a list
  • 10. Creating a node struct node{ int info; struct node *next; }*first=NULL, *temp=NULL, *last=NULL;
  • 11. void create() { temp=(struct node *)malloc(sizeof(struct node)); printf(“Enter the information to be stored”); scanf(“%d”,&temp->info); temp->next=NULL; }
  • 12. Inserting the node in a SLL There are 2 cases here:-  Insertion at the beginning  Insertion at the end
  • 13. Insertion at the beginning Thereare twosteps to be followed:- a) Make the next pointer of the node point towards the first node of the list b) Make the start pointer point towards this new node  If the list is empty simply make the start pointer point towards the new node;
  • 14.
  • 15. InsertANodeAtTheBeginning void insertbeg() { if(first==NULL) { create(); first=temp; last=first; } else { create(); temp->next=first; first=temp; } } void create() { temp=(struct node *)malloc(sizeof(struct node)); printf(“Enter the information to be stored”); scanf(“%d”,&temp->info); temp->next=NULL; }
  • 16. Inserting at the end Here we simply need to make the next pointer of the last node point to the new node
  • 17. InsertANodeAtTheEnd void insertEnd() { if(first==NULL) { create(); first=temp; last=first; } else { create(); last->next=temp; last=temp; } } void create() { temp=(struct node *)malloc(sizeof(struct node)); printf(“Enter the information to be stored”); scanf(“%d”,&temp->info); temp->next=NULL; }
  • 18. Display Elements void display() { struct node *temp1; temp1=first; if(temp1==NULL) { printf(“List is Empty”); } printf(“The elements of the linked list are:”); while(temp1!=NULL) { printf(“%d”,temp1->info); temp1=temp1->next; } }
  • 19. Deleting a node in SLL Here also we have 2 cases:-  Deleting the first node Deleting the last node
  • 20. Deleting the first node three two one Here we apply 2 steps:-  Making the start pointer point towards the 2nd node  Deleting the first node using delete keyword start
  • 21. void deleteBeg() { struct node *temp; temp=first; if(temp->next==NULL) { printf(“The item deleted=%d”,temp->info); free(temp); first=last=NULL; } else { first=temp->next; printf(“The item deleted=%d”,temp->info); free(temp); } }
  • 22. Deleting the last node node3 node2 node1 Here we apply 2 steps:-  Making the second last node’s next pointer point to NULL  Deleting the last node via delete keyword start
  • 23. void deleteEnd() { struct node *temp; temp=first; if(temp->next==NULL) { printf(“The item deleted=%d”,temp->info); free(temp); first=last=NULL; } else { while(temp->next!=NULL) temp=temp->next; printf(“The item deleted=%d”,last->info); free(last); temp->next=NULL; last=temp; } }
  • 24. Doubly Linked List 1. Doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. 2 . Each node contains three fields :: -: one is data part which contain data only. -:two other field is links part that are point or references to the previous or to the next node in the sequence of nodes. 3. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null to facilitate traversal of the list.
  • 25. NODE A B C A doubly linked listcontain three fields: an integervalue, the link to the next node, and the link to the previous node. previous data next NULL 11 786 786 200 400 200 656 400 786 777 NULL
  • 26. DLL’s compared to SLL’s ⚫Advantages: ⚫Can be traversed in either direction (may be essential for some programs) ⚫Some operations, such as deletion and inserting before a node, become easier ⚫Disadvantages: ⚫Requires more space ⚫List manipulations are slower (because more links must be changed) ⚫Greater chance of having bugs (because more links must be manipulated)
  • 27. Structure of DLL //holds theaddress of previous node struct node { int info; struct node*next; struct node*prev; } *first=NULL, *last=NULL, *temp=NULL, ; .Data .next previous. inf
  • 28. Creating a node in DLL void create() { temp=(struct node *)malloc(sizeof(struct node)); printf(“Enter the information to be stored”); scanf(“%d”,&temp->info); temp->next=NULL; temp->prev=NULL; }
  • 30. void insertbeg() { if(first==NULL) { create(); first=temp; last=first; } else { create(); temp->next=first; first->prev=temp; first=temp; } } Inserting at beginning void create() { temp=(struct node *)malloc(sizeof(struct node)); printf(“Enter the information to be stored”); scanf(“%d”,&temp->info); temp->next=NULL; temp->prev=NULL; }
  • 32. void insertEnd() { if(first==NULL) { create(); first=temp; last=first; } else { last->next=temp; temp->prev=last; last=temp; } } Inserting at the end void create() { temp=(struct node *)malloc(sizeof(struct node)); printf(“Enter the information to be stored”); scanf(“%d”,&temp->info); temp->next=NULL; temp->prev=NULL; }
  • 33. Deleting a node • Nodedeletion from a DLL involves changing two links • In thisexample,we will delete node b myDLL a b c • Wedon’t have todo anything about the links in node b • Garbagecollection will takecareof deleted nodes • Deletion of the first nodeor the last node isa special case
  • 34. void deleteBeg() { struct node *temp; temp=first; if(first==NULL) {printf(“List is empty”);} if(temp->next==NULL) { printf(“The item deleted=%d”,temp->info); free(temp); first=last=NULL; } else { first=first->next; printf(“The item deleted=%d”,temp->info); free(temp); } }
  • 35. void deleteEnd() { struct node *temp,*temp2; temp=first; if(first==NULL) {printf(“List is empty”);} if(temp->next==NULL) { printf(“The item deleted=%d”,temp->info); free(temp); first=last=NULL; } else { temp2=last->prev; temp2->next=NULL; printf(“The item deleted=%d”,last->info); free(last); last=temp2; } }
  • 36. Display Elements void display() { struct node *temp1; temp1=first; if(temp1==NULL) { printf(“List is Empty”); } printf(“The elements of the linked list are:”); while(temp1!=NULL) { printf(“%d”,temp1->info); temp1=temp1->next; } }
  • 37. APPLICATIONS OF LINKED LIST 1.Applications that have an MRU list (a linked list of file names) 2.The cache in your browser that allows you to hit the BACK button (a linked list of URLs) 3.Undo functionality in Photoshop or Word (a linked list of state) 4.A stack, hash table, and binary tree can be implemented using a doubly linked list.
  • 38. • Polynomials 6 2 3 8 0 2 Index represents exponents -3 18 0 0 23 0 4 2 •Array Implementation: • p1(x) = 8x3 + 3x2 + 2x + 6 • p2(x) = 23x4 + 18x - 3 p1(x) p2(x)
  • 39. •This is whyarrays aren’tgood to represent polynomials: • p3(x) = 16x21 - 3x5 + 2x + 6 6 2 0 0 -3 0 ………… 0 16 WASTE OF SPACE!
  • 40. • Advantages of using an Array: • onlygood for non-sparse polynomials. • ease of storageand retrieval. • Disadvantagesof using an Array: • have toallocatearray size ahead of time. •huge array size required for sparse polynomials. Waste of spaceand runtime.
  • 41. • Polynomial Representation • Linked list Implementation: • p1(x) = 23x9 + 18x7 + 41x6 + 163x4 + 3 • p2(x) = 4x6 + 10x4 + 12x + 8 23 9 18 7 41 6 18 7 3 0 4 6 10 4 12 1 8 0 P1 P2 NODE (contains coefficient & exponent) TAIL (contains pointer)
  • 42. • Advantages of using a Linked list: •savespace (don’t have toworryabout sparse polynomials) and easy to maintain •don’t need toallocate list size and can declare nodes (terms) onlyas needed • Disadvantagesof using a Linked list : • can’tgo backwards through the list •can’t jump to the beginning of the list from theend.
  • 43. Polynomials A(x) em 1 am 1x am em 2 e0 2 x ... a0 x Representation struct polynode { int coef; int exp; struct polynode * next; }; typedef struct polynode *polyptr; coef exp next
  • 44. •Adding polynomials using a Linked list representation: (storing the result in p3) Todo this, we have to break the process down to cases: • Case 1: exponent of p1 > exponent of p2 • Copy node of p1 toend of p3. [go to next node] • Case 2: exponent of p1 < exponent of p2 • Copy node of p2 toend of p3. [go to next node]
  • 45. • Case 3: exponentof p1 = exponent of p2 •Create a new node in p3 with thesame exponent and with the sum of the coefficientsof p1 and p2.
  • 46. Example 3 14 2 8 a 8 14 -3 10 b b 8x14 3x10 10x 6 1 0 null 10 6 null a 3x14 2x8 1
  • 47. Adding Polynomials 3 14 a 8 14 2 8 -3 10 1 0 10 6 b 11 14 d a->expon == b->expon 3 14 1 0 a 2 8 -3 10 10 6 b 8 14 11 14 -3 10 a->expon < b->expon
  • 48. 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 -3 10 d a->expon > b->expon 2 8