Vous êtes sur la page 1sur 18

PRESENTATION

SUBMITTED TO :BY :-

SUBMITTED

Mrs. Monica Saluja


Puri
(Lect. DSPM)
CSE06

Amit

DYNAMIC
MEMORY
ALLOCATION

DYANMIC MEMORY
ALLOCATION

Before , when we dont know


about dynamic memory allocation,
we used to allocate memory at
compile time and sometimes face
a problem when we need more
memory while program is running.
What should we do then? ? ? ? ? ?
DYNAMIC
MEMORY
ALLOCATION

PREDEFINED FUNCTIONS FOR


DYNAMIC MEMORY ALLOCATION
NEW
we can initialize memory using NEW
operator as:pointer variable= new data-type
(value);
DELETE
we can use it as :delete pointer-variable;

THE new OPERATOR


This operator obtains memory from the operating system and
returns a pointer to its starting point.
E.g. :void main()
{ char *str = IDLE HANDS ARE THE DEVILS WORKSHOP;
Returns a pointer that points to
int len = strlen(str);
the memory just large enough to
char *ptr;
hold the string str , whose length
ptr= new char[ len + 1];
we have found .
strcpy( ptr , str );
cout<<end<<ptr=<<ptr;
}
Then we use strcpy to copy string str
the output will be :in the newly created memory pointed
ptr= IDLE HANDS ARE THE DEVILS WORKSHOP
to by ptr.

THE delete OPERATOR


THINK.
If our program reserves all the memory
available in the system using new , then the
system will crash. To ensure safe and
efficient use of memory new operator is
matched by a corresponding delete operator
that returns memory to operating system.

=>In the same E.g. :void main()


{ char *str = IDLE HANDS ARE THE DEVILS
WORKSHOP;
int len = strlen(str);
Here delete ptr;
char *ptr;
Returns to the system
ptr= new char[ len + 1];
whatever memory was
strcpy( ptr , str );
cout<<end<<ptr=<<ptr; pointed to by ptr.
delete ptr;
}

COMPARISON
BETWEEN SINGLE
AND DOUBLE
LINKEDLIST


In a standard linked list,
we can only traverse the list in one
direction. It may be useful however,
to be able to go both forwards and
backwards when traversing a linked
list. In order to allow this, we need
each Node to contain not one, but
two references to Nodes - one to the
next Node in the list and one to the
previous Node in the list.

A double linked list can be implemented by


editing code for a singly linked list.
For singly linked list :main()
{
header=NULL;
ptr= new node;
header= ptr;
ptr->next=NULL;

THIS CODE WILL


CREATE A SINGLY
LINKED LIST WITH
ONE NODE

For doubly linked list :main()


{

THIS CODE WILL


CREATE A DOUBLY
LINKED LIST WITH
ONE NODE

header=NULL;
ptr=new node;
ptr->next=NULL;
ptr->prev=NULL;
header=ptr;
} where prev is reference for address of previous node.

HEADE
R
As clear from the fig. that in a singly linked list, each
node contains the address of next node.

HEADE
R

Where as in this fig. that in doubly linked list each node


contains the address of both next and previous node.
But this doesnt mean for first node, in doubly linked
list first node will not contain the address of header
infect it will contain NULL in its previous address part.

Different operations on two types of


linked lists : The operations on doubly linked lists are same as
of singly linked lists. Little change is in the coding
of operations.
Following are some operations which i will consider
to compare two different types of linked lists :
Insertion
1. At BEGINNING
AND SOME
OTHER
2. At END
OPERATION
3. After any element
S

Deletion
ALSO..
1. At BEGINNING
2. At END
3. After any element

INSERTION AT BEGINNING
IN SINGLY LINKED LIST
In this insertion the new node is given the address of
first node taking it from header and header is
pointed
to the new node.
HEADE

R
New
Nod
e

HEADE
R

IN DOUBLY LINKED LIST


In this insertion we have to repeat the same as in
singly linked list, but we also have to store NULL in
the previous part of the new node and address of
HEADE
new node in the previous part of second node.

Ne
w
No

INSERTION AT END
IN SINGLY LINKED LIST
In this insertion, the new node is given NULL in its next
part and address of the new node is stored in the last
node before insertion. We have to reach the
Ne last node
by traversing.
w

HEADE
R

No
de

IN DOUBLY LINKED LIST


In this insertion, we have to again repeat the same
Nehave to
procedure as in singly linked list, but with it we
w
point previous part of the new node to it previous
node.
HEADE

No
de

INSERTION AFTER GIVEN NODE


IN SINGLY LINKED LIST
In this insertion we have to reach at the given
node by traversing till the inputted item, Then we
have to point address part of new node to the next
node whose address we can take from inputted
node . And the address part of inputted node is
pointed to the new node.

HEADE
R

Inputted
item

New
node

IN DOUBLY LINKED LIST


In this insertion similarly we have to traverse to
inputted node, then we have to create a new node
then we have to point its next part to address of
next node which we can take from inputted node and
similarly in its previous part we have to put the
address of the inputted node, which we can take from
the next node. Then we
have to point the next part of
Inputted
inputted node and previous
Nodepart of next node to the
HEADE
new node.

New
Nod
e

Thus on comparing the


insertion operation between two
different types of linked lists, we
suffer few changes for the given
operation. Only difference is that
doubly linked list contains the
address of previous node in
addition to the next node.

SOME IMPORTANT POINTS ABOUT DOUBLY


LINKED LISTS :1. In DOUBLY linked lists if we have to go back in the
list then we dont have to traverse again from the
starting, as we have the address of previous node
also we can directly go back to any node.
2. In doubly linked lists, if in an operation when we
need nodes of both the sides of a given node like in
DELETION AFTER A GIVE ELEMENT then we dont
need to declare extra pointer variables as we can
access nodes of both the sides directly, where as in
singly linked lists we cannot use the previous
node so
SO SAVES
we have to declare extra variable for it,
in these
MEMORY
types of operations.
ALSO

THANKS

Vous aimerez peut-être aussi