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

linkedlist.pptx

  • 1.
  • 2.
    Heap memory isa 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 ifheap 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 LinkedLists ⚫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 LinkedLists 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 ⚫SinglyLinked list ⚫Doubly linked list ⚫Circular linked list ⚫Circular double linked list
  • 7.
    Singly Linked List ⚫Eachnode 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 bc 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 ona list • Creating a List • Inserting an element in a list • Deleting an element from a list • Searching a list
  • 10.
    Creating a node structnode{ 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 nodein a SLL There are 2 cases here:-  Insertion at the beginning  Insertion at the end
  • 13.
    Insertion at thebeginning 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;
  • 15.
    InsertANodeAtTheBeginning void insertbeg() { if(first==NULL) { create(); first=temp; last=first; } else { create(); temp->next=first; first=temp; } } void create() { temp=(structnode *)malloc(sizeof(struct node)); printf(“Enter the information to be stored”); scanf(“%d”,&temp->info); temp->next=NULL; }
  • 16.
    Inserting at theend 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=(structnode *)malloc(sizeof(struct node)); printf(“Enter the information to be stored”); scanf(“%d”,&temp->info); temp->next=NULL; }
  • 18.
    Display Elements void display() { structnode *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 nodein SLL Here also we have 2 cases:-  Deleting the first node Deleting the last node
  • 20.
    Deleting the firstnode 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 lastnode 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 Adoubly 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 toSLL’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 //holdstheaddress 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 nodein 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; }
  • 29.
  • 30.
    void insertbeg() { if(first==NULL) { create(); first=temp; last=first; } else { create(); temp->next=first; first->prev=temp; first=temp; } } Inserting atbeginning 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; }
  • 31.
  • 32.
    void insertEnd() { if(first==NULL) { create(); first=temp; last=first; } else { last->next=temp; temp->prev=last; last=temp; } } Inserting atthe 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() { structnode *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 LINKEDLIST 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 23 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 whyarraysaren’tgood to represent polynomials: • p3(x) = 16x21 - 3x5 + 2x + 6 6 2 0 0 -3 0 ………… 0 16 WASTE OF SPACE!
  • 40.
    • Advantages ofusing 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 ofusing 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 1xam 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 usinga 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 28 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 814 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 28 1 0 a 8 14 -3 10 10 6 b 11 14 -3 10 d a->expon > b->expon 2 8
  • 49.