Diamond Application Development Crafting Solutions with Precision
linked list.pptx
1. Linked LIST
• A linked list is a linear and a non-primitive
data structure in which each element is
allocated dynamically, and each element
points to the next element.
• In other words, we can say that it is a data
structure consisting of a group of nodes that
concurrently represent a sequence.
2.
3. S.No. ARRAY LINKED LIST
1.
An array is a grouping of data elements of
equivalent data type.
A linked list is a group of entities called a
node. The node includes two segments:
data and address.
2.
It stores the data elements in a
contiguous memory zone.
It stores elements randomly, or we can say
anywhere in the memory zone.
3.
In the case of an array, memory size is
fixed, and it is not possible to change it
during the run time.
In the linked list, the placement of
elements is allocated during the run time.
4.
The elements are not dependent on each
other.
The data elements are dependent on each
other.
5. The memory is assigned at compile time. The memory is assigned at run time.
6.
It is easier and faster to access the
element in an array.
In a linked list, the process of accessing
elements takes more time.
7.
In the case of an array, memory utilization
is ineffective.
In the case of the linked list, memory
utilization is effective.
8
When it comes to executing any operation
like insertion, deletion, array takes more
time.
When it comes to executing any operation
like insertion, deletion, the linked list takes
less time.
4. Types of Linked Lists:
Simple Linked List
Doubly Linked List
Circular Linked List
Doubly Circular Linked List
Header Linked List
A header linked list is a special type of linked list that contains a header node at
the beginning of the list.
5. Simple Linked List – In this type of linked list, one can move or traverse the
linked list in only one direction. where the next pointer of each node points to
other nodes but the next pointer of the last node points to NULL. It is also
called “Singly Linked List”.
6. Doubly Linked List – In this type of linked list, one can move or traverse the
linked list in both directions (Forward and Backward)
7. Circular Linked List – In this type of linked list, the last node of the linked list
contains the link of the first/head node of the linked list in its next pointer.
8. Doubly Circular Linked List – A Doubly Circular linked list or a circular two-way
linked list is a more complex type of linked list that contains a pointer to the
next as well as the previous node in the sequence.
. The difference between the doubly linked and circular doubly list is the same as that
between a singly linked list and a circular linked list.
The circular doubly linked list does not contain null in the previous field of the first
node.
9. • A linked list is a way to store a collection of
elements.
• Each element in a linked list is stored in the
form of a node.
• A node is a collection of two sub-elements or
parts.
– A data part that stores the element
– next part that stores the link to the next node
10.
11. Declaring a Linked list :
a linked list can be implemented using structure and
pointers .
struct LinkedList
{
int data;
struct LinkedList *next;
};
The above definition is used to create every node in the
list.
– The data field stores the element
– the next is a pointer to store the address of the next node.
12. struct LinkedList
{
int data;
struct LinkedList *next;
};
In place of a data type, struct LinkedList is written before next.
That's because its a self-referencing pointer. It means a pointer
that points to whatever it is a part of.
Here next is a part of a node and it will point to the next node.
13. 13
MEMORY ALLOCATION PROCESS
The program instructions, global and static variables are stored in a region known as
permanent storage area.
Local variables are stored in another region called stack.
The memory spaced located between stack and permanent storage area is available
for dynamic memory allocation during execution of the program. This free memory region is
called as heap.
14. 14
The memory allocation may be classified as
static memory allocation and dynamic memory
allocation.
Static memory allocation: Memory for the variables
is created at the time of compilation is known as static
memory.
Dynamic memory allocation: Memory for the
variables is allocated at the time of execution of the
program is called dynamic memory.
15. 15
Dynamic memory allocation functions are
defined in stdlib.h and alloc.h header files.
1. malloc()
2. calloc()
3. realloc()
4. free()
malloc(), calloc() and realloc() are memory
allocation functions and free() is a memory releasing
function.
16. 16
1. malloc() function:
malloc() function is used to allocate memory for the
variables at run time.
Syntax: prtvariable = (casttype*)malloc(size);
Where,
ptrvariable is a pointer variable of type casttype.
size represents number of bytes of memory to be
allocated.
17. 17
Example: int *X;
X = (int*)malloc(5*2); (or)
X = (int*)malloc(5*sizeof(int));
Here, malloc() function reserves a single block of
memory with the specified size and returns a pointer of type
void. With this, we can assign it to any type of pointer
variable. By default memory location is filled with garbage
values.
18. Creating a Node:
typedef is used to define a data type in C.
malloc() is used to dynamically allocate a single block of memory in C, it is
available in the header file stdlib.h.
sizeof() is used to determine size in bytes of an element in C. Here it is
used to determine size of each node and sent as a parameter to malloc.
The above code will create a node with data as value and next pointing to
NULL.