0% found this document useful (0 votes)
36 views21 pages

Mod 333

The document discusses self-referential data structures and linked lists. It explains that in a self-referential structure, pointers refer to structures of the same type, allowing for linked lists and other data structures. A linked list is presented as a linear data structure where each node contains a data field and a pointer to the next node. Dynamic memory allocation using functions like malloc(), calloc(), free(), and realloc() allow memory to be allocated and freed at runtime.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views21 pages

Mod 333

The document discusses self-referential data structures and linked lists. It explains that in a self-referential structure, pointers refer to structures of the same type, allowing for linked lists and other data structures. A linked list is presented as a linear data structure where each node contains a data field and a pointer to the next node. Dynamic memory allocation using functions like malloc(), calloc(), free(), and realloc() allow memory to be allocated and freed at runtime.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

DATA STRUCTURE http://www.youtube.

com/c/EDULINEFORCSE
STUDENTS

MODULE 3
LINKED LIST & MEMORY
MANAGEMENT

Prepared By Mr. EBIN PM, AP, IESCE 1

SELF REFERENTIAL STRUCTURE


• Self Referential Structure is the Data Structure in which the pointer
refers (points) to the structure of the same type.
• In other words, structures pointing to the same type of structures
are self-referential in nature.
• A self referential structure is used to create data structures like
linked lists, tree, graph etc.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 2

Prepared By Mr. EBIN PM, AP, IESCE 1


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

Eg: In singly linked list

• In the above declaration next is the pointer to the structure of type


node. Here next is the pointer which will contains the address of
the structure of the same type (i.e address of next node) and data
will contain the actual data.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 3

Eg: In Doubly linked list

• 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.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 4

Prepared By Mr. EBIN PM, AP, IESCE 2


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

DYNAMIC MEMORY ALLOCATION


Memory Allocation Process
• Global variables, static variables and program instructions get their
memory in permanent storage area whereas local variables are
stored in a memory area called Stack.
• The memory space between these two region is known as Heap
area. This region is used for dynamic memory allocation during
execution of the program. The size of heap keep changing.
• The process of allocating memory at runtime is known as dynamic
memory allocation. Library routines known as memory
management functions are used for allocating and freeing memory
during execution of a program.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 5

Program memory layout

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 6

Prepared By Mr. EBIN PM, AP, IESCE 3


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

• There are 4 library functions provided by C defined under


<stdlib.h> header file to facilitate dynamic memory allocation in C
programming. They are:
malloc()
calloc()
free()
realloc()
malloc() function is used for allocating block of memory at
runtime. This function reserves a block of memory of the given size
and returns a pointer of type void. This means that we can assign it
to any type of pointer using typecasting.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 7

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

calloc() is another memory allocation function that is used for


allocating memory at runtime. calloc function is normally used for
allocating memory to derived data types such as arrays and
structures.
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 8

Prepared By Mr. EBIN PM, AP, IESCE 4


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

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

realloc() changes memory size that is already allocated


dynamically to a variable.
Syntax:
void* realloc(pointer, new-size)
Eg:
int *x;
x = (int*)malloc(50 * sizeof(int));
x = (int*)realloc(x,100); //allocated a new memory to variable x
The free( ) function is used to de-allocate the previously allocated
memory using malloc( ) or calloc( ) functions.
Syntax : free (ptr_var);
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 10

Prepared By Mr. EBIN PM, AP, IESCE 5


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

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:

• In simple words, a linked list consists of nodes where each node


contains a data field and a reference(link) to the next node in the
list.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 11

Comparison Between Array & Linked List


No ARRAY LINKED LIST
1 Difficult to perform insertion & deletion Easy to perform insertion & deletion
operation operation
2 It is easy to access an element from an array To access an element from a list, we must
start from beginning of the list and then take
address of next element from current node
3 Array element access is random access and In linked list , access is sequential and it is
it is fast slow
4 Array need space to store only the data In linked list additional space is required to
element. No extra space is required store the pointers
5 Array elements are stored in contiguous List elements need not be stored in
memory locations. contiguous memory location
6 For insertion & deletion, it takes more time. Insertion and deletion take less time. But
For dictionary operation array take less time dictionary operation take very less time.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 12

Prepared By Mr. EBIN PM, AP, IESCE 6


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

Inserting a node on the front f

y
x

0
f

y
x

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 13

Inserting a node at the end of list


If a list exist, we must traverse that list for finding the
end of the list. For that purpose we create a dummy
pointer b which is also point to the first node. This b
become move and f points first node at all times.
If b.link=nil, then we points b.link to x
f b b

A B C D 0

y 0
x

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 14

Prepared By Mr. EBIN PM, AP, IESCE 7


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

Inserting a node between two nodes after a given data value

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 15

f p p q

d 0

p. link x. link= q
y
x

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 16

Prepared By Mr. EBIN PM, AP, IESCE 8


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

• If a list exist, then a pointer p is set which points to f. Then we


traverse this pointer p upto p.data=d.
• Then we check, if p.link=nil. If yes, the new node x will assign as the
last node and convert its link part using nil value.
• Else , we create a new variable q and p.link is assigned to that q.
• P.link is assigned to x and x.link is also given to q

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 17

Delete first node from linked list

f p f

A B C D 0

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 18

Prepared By Mr. EBIN PM, AP, IESCE 9


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

Delete last node from linked list

f p s p
s

A B C D 0 E 0

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 19

Delete a node from middle, after a given data value.

f pq p

d 0

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 20

Prepared By Mr. EBIN PM, AP, IESCE 10


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

DOUBLY LINKED LIST


• In a single linked list, every node has a link to its next node in the
sequence. So, we can traverse from one node to another node only
in one direction and we can not traverse back.
• We can solve this kind of problem by using a double linked list.
• In a double linked list, every node has a link to its previous node
and next node.
• So, we can traverse forward by using the next field and can
traverse backward by using the previous field.
• Every node in a double linked list contains three fields and they are
shown in the following figure
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 21

• 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.

In C, structure of a node in


doubly linked list can be given as :

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 22

Prepared By Mr. EBIN PM, AP, IESCE 11


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

Memory Representation of a doubly linked list


• Generally, doubly linked list consumes more space for every node
and therefore, causes more expansive basic operations such as
insertion and deletion.
• However, we can easily manipulate the elements of the list since
the list maintains pointers in both the directions (forward and
backward).
• In the following image, the first element of the list that is i.e. 13
stored at address 1. The head pointer points to the starting address
1.
• Since this is the first element being added to the list therefore the
prev of the list contains null. The next node of the list resides at
address 4 therefore the first node contains 4 in its next pointer.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 23

• We can traverse the list in this way until we find any node
containing null or -1 in its next part.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 24

Prepared By Mr. EBIN PM, AP, IESCE 12


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

CIRCULAR SINGLY LINKED LIST


• In a circular Singly linked list, the last node of the list contains a
pointer to the first node of the list. We can have circular singly
linked list as well as circular doubly linked list.
• We traverse a circular singly linked list until we reach the same
node where we started.
• The circular singly liked list has no beginning and no ending.
• There is no null value present in the next part of any of the nodes.
• The following image shows a circular singly linked list.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 25

• 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.
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 26

Prepared By Mr. EBIN PM, AP, IESCE 13


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

Memory Representation of circular linked list


• The last node of the list contains the address of the first node of the
list.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 27

CIRCULAR DOUBLY LINKED LIST


• Circular doubly linked list is a more complex type of data structure
in which a node contain pointers to its previous node as well as the
next node.
• Circular doubly linked list doesn't contain NULL in any of the node.
• The last node of the list contains the address of the first node of
the list.
• The first node of the list also contain address of the last node in its
previous pointer.
• A circular doubly linked list is shown in the following figure.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 28

Prepared By Mr. EBIN PM, AP, IESCE 14


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

• 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.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 29

Memory Management of Circular Doubly linked list

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 30

Prepared By Mr. EBIN PM, AP, IESCE 15


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

• 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.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 31

STACK USING LINKED LIST (LINKED STACK)


• In linked list implementation of stack, the nodes are maintained
non-contiguously in the memory. Each node contains a pointer to
its immediate successor node in the stack

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 32

Prepared By Mr. EBIN PM, AP, IESCE 16


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

Algorithm to insert an element in to the stack (PUSH operation)

Top
y Top
x

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 33

Algorithm to delete an item from a linked stack (POP operation)

Top d
x

Top

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 34

Prepared By Mr. EBIN PM, AP, IESCE 17


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

QUEUE USING LINKED LIST(LINKED QUEUE)


• In a linked queue, each node of the queue consists of two parts i.e.
data part and the link part. Each element of the queue points to its
immediate next element in the memory.
• In the linked queue, there are two pointers maintained in the
memory i.e. front pointer and rear pointer.
• The front pointer contains the address of the starting element of
the queue while the rear pointer contains the address of the last
element of the queue.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 35

Enqueue an item in to linked queue (Insertion)

f r

y 0
x

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 36

Prepared By Mr. EBIN PM, AP, IESCE 18


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

Dequeue an item from linked queue (deletion)

f f r

s
x

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 37

MEMORY ALLOCATION SCHEME


FIRST FIT - In the first fit approach is to allocate the first free
partition or hole large enough which can accommodate the process.
It finishes after finding the first suitable free partition.
BEST FIT - The best fit deals with allocating the smallest free
partition which meets the requirement of the requesting process.
This algorithm first searches the entire list of free partitions and
considers the smallest hole that is adequate. It then tries to find a
hole which is close to actual process size needed.
WORST FIT - In worst fit approach is to locate largest available free
portion so that the portion left will be big enough to be useful. It is
the reverse of best fit.
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 38

Prepared By Mr. EBIN PM, AP, IESCE 19


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

Q: Given five memory partitions of 100Kb, 500Kb, 200Kb, 300Kb,


600Kb (in order), how would the first-fit, best-fit, and worst-fit
algorithms place processes of 212 Kb, 417 Kb, 112 Kb, and 426 Kb (in
order)? Which algorithm makes the most efficient use of memory?

100kb 500kb 200kb 300kb 600kb


FIRST FIT
212K is put in 500K partition (remaining 500k-212k= 288k)
417K is put in 600K partition (remaining 600k-417k= 183k)
112K is put in 288K partition (new partition 288K = 500K - 212K)
426K must wait
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 39

100kb 500kb 200kb 300kb 600kb

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

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 40

Prepared By Mr. EBIN PM, AP, IESCE 20


DATA STRUCTURE http://www.youtube.com/c/EDULINEFORCSE
STUDENTS

100kb 500kb 200kb 300kb 600kb

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.

Prepared By Mr.EBIN PM, AP, IESCE EDULINE 41

Prepared By Mr. EBIN PM, AP, IESCE 21

You might also like