Mod 333
Mod 333
com/c/EDULINEFORCSE
STUDENTS
MODULE 3
LINKED LIST & MEMORY
MANAGEMENT
• In the above declaration prev & next are the pointer to the
structure of type node. Here prev pointer contains the address of
the previous node and next pointer contains the address of the
next node.
Syntax:
void* malloc(byte-size)
Eg:
int *x;
x = (int*)malloc(50*sizeof(int)); //memory space allocated to
variable x
free(x); //releases the memory allocated to variable x
Syntax:
void *calloc(number of items, element-size)
Eg:
struct employee
{
char *name;
int salary;
};
typedef struct employee emp;
emp *e1;
e1 = (emp*)calloc(30,sizeof(emp));
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 9
LINKED LIST
• A linked list is a linear data structure, in which the elements are not
stored at contiguous memory locations. The elements in a linked
list are linked using pointers as shown in the below image:
y
x
0
f
y
x
A B C D 0
y 0
x
f p p q
d 0
p. link x. link= q
y
x
f p f
A B C D 0
f p s p
s
A B C D 0 E 0
f pq p
d 0
• In double linked list, the first node must be always pointed by head.
• Always the previous field of the first node must be NULL.
• Always the next field of the last node must be NULL.
• We can traverse the list in this way until we find any node
containing null or -1 in its next part.
• Due to the fact that a circular doubly linked list contains three parts
in its structure therefore, it demands more space per node and
more expensive basic operations.
• However, a circular doubly linked list provides easy manipulation of
the pointers and the searching becomes twice as efficient.
• The variable head contains the address of the first element of the
list i.e. 1 hence the starting node of the list contains data A is stored
at address 1.
• Since, each node of the list is supposed to have three parts
therefore, the starting node of the list contains address of the last
node i.e. 8 and the next node i.e. 4.
• The last node of the list that is stored at address 8 and containing
data as 6, contains address of the first node of the list as shown in
the image i.e. 1.
• In circular doubly linked list, the last node is identified by the
address of the first node which is stored in the next part of the last
node therefore the node which contains the address of the first
node, is actually the last node of the list.
Top
y Top
x
Top d
x
Top
f r
y 0
x
f f r
s
x
BEST FIT
212K is put in 300K partition
417K is put in 500K partition
112K is put in 200K partition
426K is put in 600K partition
WORST FIT
212K is put in 600K partition (remaining 600k-212k= 388k)
417K is put in 500K partition
112K is put in 388K partition
426K must wait
In this example, best-fit turns out to be the best.