0% found this document useful (0 votes)
10 views

Unit IV

Uploaded by

mukul.jagtap
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Unit IV

Uploaded by

mukul.jagtap
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Memory Allocation

There are two types of memory allocations:

1. Compile Time or Static Memory Allocation


2. Run-time or Dynamic Memory Allocation
Static Memory is allocated for declared variables by the
compiler. The address can be found using the address of
operator and can be assigned to a pointer. The memory is
allocated during compile time.
Dynamic Memory allocation done at the time of execution(run
time) is known as dynamic memory allocation. Functions
calloc() and malloc() support allocating dynamic memory. In the
Dynamic allocation of memory space is allocated by using these
functions when the value is returned by functions and assigned
to pointer variables.
S.No Static Memory Allocation Dynamic Memory Allocation

In the static memory allocation, In the Dynamics memory allocation,


1 variables get allocated variables get allocated only if your program
permanently. unit gets active.

Static Memory Allocation is Dynamics Memory Allocation is done during


2
done before program execution. program execution.

It uses stack for managing the It uses heap for managing the dynamic
3
static allocation of memory allocation of memory

4 It is less efficient It is more efficient


S.No Static Memory Allocation Dynamic Memory Allocation

In Dynamics Memory Allocation, there is


In Static Memory Allocation,
5 memory re-usability and memory can be
there is no memory re-usability
freed when not required
In static memory allocation,
In dynamic memory allocation, when
once the memory is allocated,
6 memory is allocated the memory size can be
the memory size can not
changed.
change.
This allows reusing the memory. The user
In this memory allocation
can allocate more memory when required.
7 scheme, we cannot reuse the
Also, the user can release the memory when
unused memory.
the user needs it.
In Dynamics Memory Allocation, there is
In Static Memory Allocation,
8 memory re-usability and memory can be
there is no memory re-usability
freed when not required
S.No Static Memory Allocation Dynamic Memory Allocation

In this memory allocation scheme, In this memory allocation scheme,


8 execution is faster than dynamic execution is slower than static memory
memory allocation. allocation.
In this memory is allocated at
9 In this memory is allocated at run time.
compile time.

In this allocated memory remains In this allocated memory can be released


10
from start to end of the program. at any time during the program.

Example: This static memory


Example: This dynamic memory allocation
11 allocation is generally used for
is generally used for linked list.
array.
Like arrays, Linked List is a linear data structure. Unlike arrays,
linked list elements are not stored at a contiguous location; the
elements are linked using pointers.
Arrays can be used to store linear data of similar types, but arrays have the
following limitations.

1) The size of the arrays is fixed: So we must know the upper limit on the
number of elements in advance. Also, generally, the allocated memory is
equal to the upper limit irrespective of the usage.

2) Inserting a new element in an array of elements is expensive because the


room has to be created for the new elements and to create room existing
elements have to be shifted.
For example, in a system, if we maintain a sorted list of IDs in an array
id[].

id[] = [1000, 1010, 1050, 2000, 2040].

And if we want to insert a new ID 1005, then to maintain the sorted


order, we have to move all the elements after 1000 (excluding 1000).

Deletion is also expensive with arrays until unless some special


techniques are used. For example, to delete 1010 in id[], everything after
1010 has to be moved.
1) Dynamic size
2) Ease of insertion/deletion
1) Random access is not allowed. We have to access elements sequentially
starting from the first node. So we cannot do binary search with linked
lists efficiently with its default implementation.

2) Extra memory space for a pointer is required with each element of the
list.

3) Not cache friendly. Since array elements are contiguous locations,


there is locality of reference which is not there in case of linked lists.
Applications of Linked Lists :

1.Linked lists are used to implement stacks, queues, graphs,


etc.

2. Linked lists let you insert elements at the beginning and end
of the list.

3. In Linked Lists we don't need to know the size in advance.


1. Node an item in a linked list. Each node contains a piece of
list data and the location of the next node (item).
2. Link (of a node) the location of the next node.
3. head node first node in a linked list
4. head pointer points to the head node.
5. Null pointer there is no need in it (linked list is empty)
Basic Operations :

Following are the basic operations supported by a list.

1. Insertion − Adds an element at the beginning of the list.


2. Deletion − Deletes an element at the beginning of the list.
3. Display − Displays the complete list.
4. Search − Searches an element using the given key.
5. Delete − Deletes an element using the given key.
Realization stands for Representation of Linked List

A linked list can be represented in two ways :

1. Dynamic representation of linked list


2. Static representation of linked list
Dynamic Representation of linked list :
Static Representation of linked list :
“Linked List is an Abstract Data Type (ADT) that holds a
collection of Nodes, the nodes can be accessed in a sequential
way. Linked List doesn’t provide a random access to a Node.”

Usually, those Nodes are connected to the next node and/or


with the previous one, this gives the linked effect. When the
Nodes are connected with only the next pointer the list is
called Singly Linked List and when it’s connected by the next
and previous the list is called Doubly Linked List.
ADT — Interface

The Linked List interface can be implemented in different ways, is


important to have operations to insert a new node and to remove a
Node:

ADT Operation :

Create – Creation of linked list


Traversal – it can be traversed
Search – Data can be compare with data to be search.
Destroy – Free the memory
There are 3 different implementations of Linked List available,
they are:

1. Linear/Singly Linked List


2. Doubly Linked List
3. Circular Linked List
“ A linked list in which every node has one link field, to provide
information about where the next node of list is, is called as
singly linked list ”
Singly linked list is a basic linked list type. Singly linked list is a
collection of nodes linked together in a sequential way where
each node of singly linked list contains a data field and an
address field which contains the reference of the next node.
Singly linked list can contain multiple data fields but should
contain at least single address field pointing to its connected
next node.
1. Singly linked list is probably the most easiest data structure
to implement.
2.Insertion and deletion of element can be done easily.
3.Insertion and deletion of elements doesn't requires movement
of all elements when compared to an array.
4.Requires less memory when compared to doubly, circular or
doubly circular linked list.
5.Can allocate or deallocate memory easily when required
during its execution.
6.It is one of most efficient data structure to implement when
traversing in one direction is required.
1. It uses more memory when compared to an array.

2. Since elements are not stored sequentially hence requires more


time to access each elements of list.

3. Traversing in reverse is not possible in case of Singly linked list


when compared to Doubly linked list.

4. Requires O(n) time on appending a new node to end. Which is


relatively very high when compared to array or other linked list.
1.Creation
2.Insertion
3.Deletion
4.Searching
5.Display
In doubly linked list, each node has two link fields to store
information about who is the next and also about who is ahead
of the node

• Hence each node has knowledge of its successor and also its
predecessor.

• In doubly linked list, from every node the list can be


traversed in both the directions.
Doubly Linked List is a variation of Linked list in which navigation is
possible in both ways, either forward and backward easily as compared to
Single Linked List. Following are the important terms to understand the
concept of doubly linked list.

Link − Each link of a linked list can store a data called an element.

Next − Each link of a linked list contains a link to the next link called Next.

Prev − Each link of a linked list contains a link to the previous link called
Prev.

Linked List − A Linked List contains the connection link to the first link
called First and to the last link called Last.
1. A DLL can be traversed in both forward and backward
direction.

2. The delete operation in DLL is more efficient if pointer to the


node to be deleted is given.

3. We can quickly insert a new node before a given node. In singly


linked list, to delete a node, pointer to the previous node is
needed. To get this previous node, sometimes the list is
traversed. In DLL, we can get the previous node using previous
pointer.
1) Every node of DLL Require extra space for an previous
pointer. It is possible to implement DLL with single pointer
though

2) All operations require an extra pointer previous to be


maintained. For example, in insertion, we need to modify
previous pointers together with next pointers. For example in
following functions for insertions at different positions, we need
1 or 2 extra steps to set previous pointer.
Following are the basic operations supported by a list.

Insertion − Adds an element at the beginning of the list.


Deletion − Deletes an element at the beginning of the list.
Insert Last − Adds an element at the end of the list.
Delete Last − Deletes an element from the end of the list.
Insert After − Adds an element after an item of the list.
Delete − Deletes an element from the list using the key.
Display forward − Displays the complete list in a forward manner.
Display backward − Displays the complete list in a backward manner.
Circular linked list are mostly used in task maintenance in
operating systems. There are many examples where circular
linked list are being used in computer science including
browser surfing where a record of pages visited in the past by
the user, is maintained in the form of circular linked lists and
can be accessed again on clicking the previous button.
“Circular Linked List is a variation of Linked list in which the
first element points to the last element and the last element
points to the first element. Both Singly Linked List and Doubly
Linked List can be made into a circular linked list”.
1.Some problems are circular and a circular data structure
would be more natural when used to represent it.

2.The entire list can be traversed starting from any node


(traverse means visit every node just once)

3.fewer special cases when coding(all nodes have a node before


and after it)
1.Circular list are complex as compared to singly linked lists.

2.Reversing of circular list is a complex as compared to singly


or doubly lists.

3.If not traversed carefully, then we could end up in an infinite


loop.
Following are the important operations supported by a circular
list.

insert − Inserts an element at the start of the list.

delete − Deletes an element from the start of the list.

display − Displays the list.


“Circular Doubly Linked List has properties of both doubly
linked list and circular linked list in which two consecutive
elements are linked or connected by previous and next pointer
and the last node points to first node by next pointer and also
the first node points to last node by previous pointer”.
1.List can be traversed from both the directions i.e. from head
to tail or from tail to head.

2.Jumping from head to tail or from tail to head is done in


constant time O(1).

3.Circular Doubly Linked Lists are used for implementation of


advanced data structures like Fibonacci Heap.
1.It takes slightly extra memory in each node to accommodate
previous pointer.

2.Lots of pointers involved while implementing or doing


operations on a list. So, pointers should be handled carefully
otherwise data of the list may get lost.
Applications of Circular doubly linked list

1. Managing songs playlist in media player applications.


2. Managing shopping cart in online shopping

You might also like