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;
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
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
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;
}
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
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.
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.
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