0% found this document useful (0 votes)
39 views8 pages

Linked List

Dynamic memory allocation allows defining data structures at runtime by allocating memory dynamically using functions like malloc(), free(), etc. Linked lists are dynamic data structures that allocate nodes of memory dynamically. There are different types of linked lists including singly, doubly, and circular linked lists. Linked lists have several advantages over static structures like arrays including flexible size and efficient insertion/deletion.

Uploaded by

jeneel1811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views8 pages

Linked List

Dynamic memory allocation allows defining data structures at runtime by allocating memory dynamically using functions like malloc(), free(), etc. Linked lists are dynamic data structures that allocate nodes of memory dynamically. There are different types of linked lists including singly, doubly, and circular linked lists. Linked lists have several advantages over static structures like arrays including flexible size and efficient insertion/deletion.

Uploaded by

jeneel1811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Linked List

**Dynamic Memory Allocation**

* What is dynamic memory allocation?


Dynamic Memory Allocation is a process in which the size of a data structure
(like an array) is changed during the runtime1. It allows the programmer to
manage memory explicitly, providing the ability to create data structures of
varying sizes and adjust memory requirements dynamically2. This is done using
functions like malloc(), calloc(), free(), and realloc() defined under the
<stdlib.h> header file in C.

* What are the advantages and disadvantages of dynamic memory allocation?


Advantages of Dynamic Memory Allocation:
 It allows defining data structures at runtime.
 It saves memory space. Memory is allocated as and when required, so it
reduces wastage of memory.
Disadvantages of Dynamic Memory Allocation:
 It takes more time to allocate memory dynamically.
 It can lead to problems like memory leaks if not handled properly (if
dynamically allocated memory is not freed)

* What are the different ways to manage memory in C?


In C programming, memory can be managed in two ways:
Static Memory Allocation: Memory size is fixed when the program is compiled.
This is done for objects that are known at compile time. The memory is
allocated on the stack.
Dynamic Memory Allocation: Memory size can be changed while the program
is running. This is done for objects that are not known at compile time. The
memory is allocated on the heap. This is done using functions like malloc(),
calloc(), realloc(), and free().

What are the different functions of DMA?


 malloc(): Allocates a single block of requested memory.
 calloc(): Allocates multiple blocks of requested memory.
 realloc(): Reallocates a block of memory that was previously allocated.
 free(): Frees the dynamically allocated memory.

**Singly linked list**

* What is a singly linked list?


A Singly Linked List is a linear data structure where each element is a separate
object and each object, or node, contains a pointer to the next node in the list.
It’s called a singly linked list because nodes are connected in one direction,
from the head node through to the last or tail node.
* What are the advantages and disadvantages of using singly linked lists?
Advantages:
 It can change size as needed.
 Adding or removing items at the start is fast.
 It uses memory efficiently.
Disadvantages:
 It uses more memory because each item needs to store the address of
the next one.
 It takes time to go through the list as you have to start from the
beginning.
 You can’t directly access items. You have to start from the beginning and
follow the links.
* How to perform the following operations on a singly linked list in C:
Creation of Node:
struct Node
{
int Data;
Struct Node *next;
};

* Insertion
void insertStart (struct Node **head, int data)
{

struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));

newNode - >
data = data;
newNode - >
next = *head;

//changing the new head to this freshly entered node


*head = newNode;
}

* Deletion
void deleteStart(struct Node **head)
{
struct Node *temp = *head;

// if there are no nodes in Linked List can't delete


if (*head == NULL)
{
printf ("Linked List Empty, nothing to delete");
return;
}

// move head to next node


*head = (*head)->next;

free (temp);
}

* Traversal
void display(struct Node* node)
{
printf("Linked List: ");

// as linked list will end when Node is Null


while(node!=NULL){
printf("%d ",node->data);
node = node->next;
}

printf("\n");
}

**Doubly linked list**

* What is a doubly linked list?


A Doubly Linked List (DLL) is a type of linked list in which each node contains a
data part and two addresses, one for the previous node and one for the next
node.
* What are the advantages and disadvantages of using doubly linked lists?
Advantages of Doubly Linked Lists:
1.Bidirectional: DLLs can be traversed in both forward and backward
directions.
2.Efficient Operations: Insertions and deletions are more efficient if the pointer
to the node is given.
3.Reverse Traversing: Reverse traversing is easier compared to singly linked
lists.
Disadvantages of Doubly Linked Lists:
1.Memory Usage: Extra memory is required for a previous pointer with each
node.
2.Implementation: All operations require an extra pointer previous to be
maintained. For example, in insertion, we need to modify previous pointers
together with next pointers.
3.No Direct Access: Direct access is not allowed. We have to access elements
sequentially starting from the first node.

* How to perform the following operations on a doubly linked list in C:


Creation Of Node:
struct Node
{
int data;
struct Node *next;
struct Node *prev;
};

* Insertion
Insertion at Beginning
void insertStart (struct Node **head, int data)
{

// creating memory for newNode


struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));

// assigning newNode's next as the current head


// Assign data & and make newNode's prev as NULL
newNode->data = data;
newNode->next = *head;
newNode->prev = NULL;

// if list already had item(s)


// We need to make current head previous node as this new node
if (*head != NULL)
(*head)->prev = newNode;

// change head to this newNode


*head = newNode;

Insertion at End:
void insertLast (struct Node **head, int data)
{
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));

newNode->data = data;
newNode->next = NULL;

//need this if there is no node present in linked list at all


if (*head == NULL)
{
*head = newNode;
newNode->prev = NULL;
return;
}

struct Node *temp = *head;

// traverse till the last node


while (temp->next != NULL)
temp = temp->next;

// assign last node's next to this new Node


temp->next = newNode;
// assign this new Node's previous to last node(temp)
newNode->prev = temp;
}

* Deletion
Deletion at beginning:

if (*head == del_node)
*head = del_node->next;

if (del_node->prev != NULL)
del_node->prev->next = del_node->next;

free(del);

Deletion at end:

if (del_node->prev != NULL)
del_node->prev->next = del_node->next;

* Traversal
void display (struct Node *node)
{
struct Node *end;
printf ("\nIn Forward Direction\n");
while (node != NULL)
{
printf (" %d ", node->data);
end = node;
node = node->next;
}
printf ("\nIn Backward direction \n");
while (end != NULL)
{
printf (" %d ", end->data);
end = end->prev;
}
}

**Circular linked list**

* What is a circular linked list?


A Circular Linked List is a type of linked list where all nodes are connected to
form a circle. In a circular linked list, the first node and the last node are
connected to each other which forms a circle. There is no NULL at the end. A
singly linked list or a doubly linked list can be converted to a circular linked list.
* What are the advantages and disadvantages of using circular linked lists?
Advantages:
 Any Direction: You can go to any friend from any friend.
 Easy Add or Remove: You can easily add or remove friends from the
game.
 Space Saving: You don’t need an extra spot to mark the end of the game.

Disadvantages:
 Complexity: Circular lists are complex as compared to singly linked lists.
 Infinite Loop: If not handled carefully, then the code may go in an
infinite loop.
 No Direct Access: Elements are accessed sequentially as they are stored
randomly in memory.
 Slower Traversal: Traversing a DLL can be slower than traversing a singly
linked list.
 Harder to find the end of the list and loop control.
**Applications of linked list**
* What are the different applications of linked lists in computer science?
 Stacks and Queues: Linked lists can be used to make other data
structures like stacks and queues.
 Graphs: Linked lists can represent graphs.
 Memory Management: Linked lists can help manage memory in
programming.
 Directory of Names: Linked lists can keep a list of names.
 Sparse Matrices: Linked lists can represent sparse matrices.
 Music Player: Songs in the music player are linked to the previous and
next songs.
* What are the advantages and disadvantages of using linked lists over other
data structures, such as arrays?
Advantages:
 Dynamic Size: Linked lists can grow or shrink during runtime.
 Efficient Operations: Insertions and deletions are more efficient if the
pointer to the node is given.
 No Memory Reallocation: Unlike arrays, you don’t need to reallocate
memory when adding or removing elements.

Disadvantages:
 More Memory: Each node needs extra space for a pointer to the next
node.
 No Random Access: Elements are accessed sequentially, not randomly
like in arrays.
 Slow Traversal: It can be slower to go through all elements compared to
arrays.

You might also like