CA229 Unit 03
CA229 Unit 03
B.C.A. (Semester-3)
CA229 || Data Structure and Applications
Malloc vs Calloc:
Malloc Calloc
The name malloc stands for memory The name calloc stands for contiguous
allocation. allocation.
void *malloc(size_t n) returns a pointer void *calloc(size_t n, size_t size)returns a
to n bytes of uninitialized storage, pointer to enough free space for an array
or NULL if the request cannot be satisfied. of n objects of the specified size,
If the space assigned by malloc() is or NULL if the request cannot be satisfied.
overrun, the results are undefined. The storage is initialized to zero.
malloc() takes one argument that calloc() take two arguments those
is, number of bytes. are: number of blocks and size of each
block.
1|Page
syntax of malloc(): syntax of calloc():
void *malloc(size_t n); void *calloc(size_t n, size_t size);
Allocates n bytes of memory. If the Allocates a contiguous block of memory
allocation succeeds, a void pointer to the large enough to hold n elements
allocated memory is returned. of sizebytes each. The allocated region is
Otherwise NULL is returned. initialized to zero.
malloc is faster than calloc. calloc takes little longer than malloc
because of the extra step of initializing the
allocated memory by zero. However, in
practice the difference in speed is very
tiny and not recognizable.
Introduction:
The everyday usage of the term “list” refers to a linear collection of data items. The below
example shows a shopping list; it contains a first element, a second element ……., and the
last element. We can add an item to a list or remove the item from a list.
Example:
Milk - Eggs -Butter - Apples - Tomatoes - Oranges
After adding an item and removing an item a list will be
Milk – Eggs Butter – Apples - Grapes - Tomatoes - Oranges
Linked List:
A linked list is a linear collection of data elements, called nodes, where the linear order is
given using pointers. That is, each node is divided into two parts. The first part contains
the information of the element and the second part, called the link field or next pointer
field, contains the address of the next node in the list.
Node
2|Page
Types of Linked List:
The following are the various types of linked lists.
1. Singly Linked List 4. Circular Header Linked List
2. Circular Linked List 5. Doubly Linked List
3. Header Linked List 6. Doubly Circular Linked List
Basic Operation:
The following are the basic operations supported by a list.
1. Insertion: Adds an element in the Linked List
2. Deletion: Delete an element from the Linked List.
3. Display: Displays the complete list.
4. Search: Searches an element using the given key
5. Delete: Delete an element using the given key.
3|Page
Singly Linked List:
The below figure is a schematic diagram of a linked list with four nodes. Each node is
pictured with two parts. The left part represents the information part of the node, which
may contain an entire record of data items. The right part represents the next pointer field
of the node, and there is an arrow drawn from it to a node when the address of the node
appears in the given field. The pointer of the last node contains an exceptional value,
called the null pointer, which is an invalid address. (In actual practice, 0 or a negative
number is used for the null pointer.) The null pointer, denoted by X in the diagram, signals
the end of the list.
The linked list also contains a list pointer variable – called START or NAME or FIRST or
HEAD – which contains the address of the first node in the list, hence there is an arrow
drawn from Head to the first node. A particular case is a list that has no nodes. Such a list
is called the null list or empty list and is denoted by the null pointer in the variable Head.
Head
4|Page
Algorithm for Singly Linked List:
Inserting a Node in Front of the Linked List:
Step 1: [Obtain a new node from available storage]
NewNode Term.
Step 2: [Initialize numeric fields]
Id(NewNode) id
Step 3: [Set the link to the list]
Next(NewNode) head;
Step 4: [Set new node as head]
Head NewNode;
Step 5: Return
5|Page
Deleting a Node at First:
Step 1: [Check whether the list is empty or not]
If Head = Null Then
Write (“List is Empty”)
End if
Step 2: [Copy value of Head in temporary variable]
Temp = Head
Step 3: [Set the Head]
Head = Temp → Next
Step 4: [Delete the Node]
Freenode (Temp)
Step 5: Exit
6|Page
First
A circular linked list is most used in task maintenance in the operating system. There are
many examples where the circular linked list is being used in computer science, such as
browser surfing. Where a record of pages visited in the past by the user, is maintained in
the form of circular linked lists. It can be re-accessed by clicking the previous button.
First
. Header Node
Doubly Linked List:
Doubly Linked List is a type of linked list in which navigation is possible in both ways,
either forward or backward easily as compared to the Singly Linked List. The following are
the essential terms to understand the concept of a doubly linked list.
Doubly Linked List Representation:
L R
X X
7|Page
As per the above illustration, the following are the crucial points to be considered.
• Doubly Linked List contains a link element called first (Left) and last (Right).
• Each link carries a data field(s) and two link fields called next and prev.
• Each node is linked with its next node using its next link.
• Each node is linked with its previous node using its previous link.
• The next part of the last node carries a null to mark the end of the list.
• The previous part of the first node carries a null to mark the end of the list from
right to left.
8|Page
Applications of linked list in the real world:
1. Image viewer: Previous and next images are linked; hence they can be accessed
by the next and previous buttons.
2. Previous and next page in a web browser: We can access previous and next
URLs searched in the web browser by pressing the back and next buttons since
they are linked as a linked list.
3. Music Player: Songs in music players are linked to the previous and next song.
You can play songs either from the start or end of the list.
9|Page