0% found this document useful (0 votes)
20 views10 pages

CA229 Unit 03

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)
20 views10 pages

CA229 Unit 03

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/ 10

Smt.

Chandaben Mohanbhai Patel Institute of


Computer Applications
CHAROTAR UNIVERSITY OF SCIENCE AND TECHNOLOGY

B.C.A. (Semester-3)
CA229 || Data Structure and Applications

Unit-3: Dynamic Data Structures


• Linked list
• Comparison of sequential and linked organizations
• Dynamic Memory Management
• Singly linked list
• Doubly linked list
• Applications of linked list
What is static and dynamic allocation of memory?
Static Allocation:
Static allocation means compiler allocate memory to variable when variable is declared.
Dynamic Allocation:
Dynamic allocation means memory of variable is allocated using dynamic allocation
function.

Static Allocation Dynamic Allocation


Memory is allocated before the execution of Memory is allocated during the
the program begins.(During Compilation) execution of the program.
No memory allocation or deallocation Memory Bindings are established and
actions are performed during Execution. destroyed during the Execution.
Variables remain permanently allocated. Allocated only when program unit is
active.
Implemented using stacks and heaps. Implemented using data segments.
Pointer is needed to accessing variables. No need of Dynamically allocated
pointers.
Faster execution than Dynamic. Slower execution than static.
More memory Space required. Less Memory space required.
int i; /* `i` only exists during `func` */ int* mem = malloc(1024)

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.

Difference between Linear Array and Linked List:


The difference between Linear Array and Linked List is shown below.
Linear Array Linked List
Deletion and Insertions are difficult. Deletion and Insertion can be done easily
For insertion and deletion, it needs For Insertion and deletion, it does not
movements. require the movement of nodes.
In it, space is wasted. In it, space is not wasted.
It is expensive It is not expensive
It cannot be reduced or extended It can be reduced or extended according to
according to requirements. requirements.
To avail, each element is the same. To avail, each element a different.
Amount of time is required. Amount of time is required.
In consecutive memory locations Elements may or may not be stored in
elements are stored. consecutive memory locations
We can reach them directly if we have to To reach a particular node, you need to go
go to a particular element. through all those nodes that come before
that node.

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

Nextpointer field of the second node.

Information part of the first node.

Advantages and Disadvantages of Linked List:


Advantages:
• They are dynamic, which allocates the memory when required.
• Insertion and deletion operations can be easilyimplemented.
• Stacks and queues can be quickly executed.
• Linked list reduces the access time.
Disadvantages:
• The memory is wasted as pointers require extra memory for storage.
• No element can be accessed randomly; it has to access each node sequentially.
• Reverse Traversing is difficult in a linked list.

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

Algorithm: Inserting a Node at the End of the Linked List:


Step 1: [Obtain a new node from available storage]
NewNode  Term.
Step 2: [Initialize numeric fields]
Id(NewNode) id // node->id=10
Next(NewNode)  NULL // node->next=NULL;
Step 3: [Is the list empty?]
If Head = Null Then
return (NewNode)
Head  NewNode;
Step 4: [Initiate Search for Last node]
Temp  Head; // temp=head;
Step 5: [Search for end of List]
Repeat while Next(temp) ≠ NULL OR Next(last)  Node
Temp  Next(temp) OR lastnode
Step 6: [Set link field to last node to new]
Next(Temp) Node
Step 7: 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

To Delete a Node from End:


Step 1: [Is the list empty?]
If Head = Null Then
Write “List is Empty”
End If
Step 2: [Initiate Search for Last node]
Temp  head
Step 3: [Search for end of List]
Repeat while Next(Temp) ≠ Null
Temp1  Temp
Temp Next(temp)
Step 4: [Set the Value for Last Node]
Next(Temp1) NULL
Free (Temp)
Step 5: Return

Circular Linked List:


In a circular Linked List, the last node of the list contains the address of the first node of
the list. We traverse a circular linked list until we reach the same node where we started.
There is no null value present in the next part of any of the nodes. The following image
shows a circular linked list.

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.

Header Linked List:


Header Linked List is a linked list that always contains a particular node called the Header
Node, at the beginning of the list. It has two types. It is also called Grounded Header List.
In this type of list, the Last Node contains the NULL Pointer.

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.

Advantages and Disadvantages of Doubly Linked List:


Advantages of Doubly Linked List:
1. A doubly linked list can be traversed in both forward and backward directions.
2. To delete a node in a singly linked list, the previous node is required, while in a
doubly-linked list, we can get the previous node using the previous pointer.
3. It is very convenient than a singly linked list. A doubly linked list maintains the
links for bidirectional traversing.
Disadvantages of Doubly Linked List:
1. In a doubly-linked list, each node requires extra space for the previous pointer.
2. All operations such as Insert, Delete, and Traverse requires an extra previous
pointer to be maintained.

Application of Linked List:


Applications of linked list in computer science:
1. Implementation of stacks and queues
2. Implementation of graphs: Adjacency list representation of graphs is the most
popular, which uses a linked list to store adjacent vertices.
3. Dynamic memory allocation: We use a linked list of free blocks.
4. Maintaining a directory of names
5. Performing arithmetic operations on long integers
6. Manipulation of polynomials by storing constants in the node of the linked list
7. representing sparse matrices

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.

Application of Circular Linked Lists:


1. Useful for implementation of a queue. Unlike this implementation, we do not need
to maintain two pointers for the Front and rear if we use a circular linked list. We
can maintain a pointer to the last inserted node, and the Front can always be
obtained as next of last.
2. Circular lists are useful in applications to go around the list repeatedly. For
example, when multiple applications are running on a PC, it is typical for the
operating system to put the running applications on a list and then cycle through
them, giving each of them a slice of time to execute, and then making them wait
while the CPU is given to another application.
3. For example, when multiple applications are running on a PC, it is typical for the
operating system to put the running applications on a list and then cycle through
them.
4. And giving each of them a slice of time to execute, and then making them wait
while the CPU is given to another application.
5. It is convenient for the operating system to use a circular list so that when it
reaches the end of the list, it can cycle around to the front of the list.
6. Circular Doubly Linked Lists are used for the implementation of advanced data
structures like Fibonacci Heaps.

9|Page

You might also like