0% found this document useful (0 votes)
34 views

Building Linked Lists With C Your Path To Data Structures

Linked lists are a linear data structure that stores elements in memory locations that are not contiguous. Each element contains a pointer to the next element. There are different types of linked lists including singly, doubly, and circular linked lists. Linked lists allow for efficient insertion and deletion compared to arrays and can dynamically grow or shrink in size. However, accessing elements requires traversing from the start which is slower than random access in arrays.

Uploaded by

shubham
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Building Linked Lists With C Your Path To Data Structures

Linked lists are a linear data structure that stores elements in memory locations that are not contiguous. Each element contains a pointer to the next element. There are different types of linked lists including singly, doubly, and circular linked lists. Linked lists allow for efficient insertion and deletion compared to arrays and can dynamically grow or shrink in size. However, accessing elements requires traversing from the start which is slower than random access in arrays.

Uploaded by

shubham
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

"BUILDING LINKED LISTS WITH C:

YOUR PATH TO DATA


STRUCTURES"
CONTENT
• What is a Linked List ?
• Types of Linked Lists
• Advantages and Disadvantages
• Basic Operations in C
INTRODUCTION TO LINKED
LIST
• Like arrays, Linked List is a linear data structure.

• Unlike arrays, linked list elements are not stored at a contiguous locationA.

• linked list is a way to store data where each piece of data is connected to the next one.

• We use it because it's flexible in size, allows for efficient insertions and deletions, and is

memory-efficient.
WHY LINKED LIST ?
Use an Array when:
• You need to access elements quickly and directly by their index.
• Your data structure has a fixed size, and you don't need it to change dynamically.

• Memory efficiency is crucial, especially for smaller data structures with predictable memory allocation.

Use a Linked List when:


• You want a data structure that can easily grow or shrink as elements are added or removed.

• Efficient insertions and deletions, particularly in the middle of the structure, are important.

• You aim to minimize memory waste, particularly for sparse data structures.

• You require a versatile foundation for building more complex data structures.
REPRESENTATION
In computer science, a linked list is a linear collection of data elements whose order is not given by their

physical placement in memory. Instead, each element points to the next. It is a data structure consisting

of a collection of nodes which together represent a sequence.

A linked list is represented by a pointer to the first node of the linked list. The first node is called the

head. If the linked list is empty, then the value of the head is NULL. Each node in a list consists of at

least two parts: 1) data 2) Pointer (Or Reference) to the next node
TYPES OF LINKED LIST

• Following are the various types of linked list.

• Singly Linked List − Item navigation is forward only.

• Doubly Linked List − Items can be navigated forward and backward.

• Circular Linked List − Last item contains link of the first element as next and the

first element has a link to the last element as previous.


SINGLY LINKED LIST
INSERTION:

INSERTING AT THE BEGINNING (PREPEND):

1. Create a new node with the desired data.


2. Set the "next" pointer of the new node to point to the current head.
3. Update the head to be the new node.

INSERTING AT THE END (APPEND):

1. Create a new node with the desired data.


2. Traverse the list until you reach the last node (where the "next" pointer is None).
3. Set the "next" pointer of the last node to point to the new node.

INSERTING AT A SPECIFIC POSITION:

1. Create a new node with the desired data.


2. Traverse the list to the node just before the desired position.
3. Set the "next" pointer of the new node to the node at the desired position.
4. Update the "next" pointer of the previous node to point to the new node.
DELETION:

IDELETING THE FIRST NODE:

1. Check if the list is empty by verifying if the head is None.


2. If the list is empty, there's nothing to delete.
3. If the list is not empty, update the head to point to the second node (if it exists).

DELETING A SPECIFIC NODE:

1. Start at the head node and traverse the list while keeping track of the current node and the previous node.
2. Check each node's data as you traverse to identify the node to delete.
3. Once you find the node to delete, update the "next" pointer of the previous node to skip the node to be deleted.
4. The node to be deleted can be removed from memory (deallocated), if needed, depending on the programming
language.
DELETION:

IDELETING THE LAST NODE:

1. If the list is empty (head is None), there's nothing to delete.


2. If the list has only one node (head.next is None), set the head to None to make the list empty.
3. Otherwise, start at the head node.
4. Traverse the list while keeping track of the current node and the previous node:
a. Update the previous node to be the current node.
b. Update the current node to be its next node.
5. Once you reach the last node (current node's next is None), set the previous node's next to None to
remove the last node.
DOUBLY LINKED LIST
INSERTION:

INSERTION AT THE BEGINNING:


1. Create a new node with the desired data.
2. Set the "next" pointer of the new node to the current head.
3. Set the "previous" pointer of the current head to the new node (if it exists).
4. Update the head to be the new node.

INSERTION AT THE END:

1. Create a new node with the desired data.


2. Traverse the list until you reach the last node (where the "next" pointer is None).
3. Set the "next" pointer of the last node to point to the new node.
4. Set the "previous" pointer of the new node to the last node.

INSERTION AT A SPECIFIC POSITION:

1. Create a new node with the desired data.


2. Traverse the list to the node just before the desired position.
3. Set the "next" pointer of the new node to the node at the desired position.
4. Set the "previous" pointer of the new node to the node before the desired position.
5. Update the "next" pointer of the previous node to point to the new node.
6. Update the "previous" pointer of the node at the desired position to point to the new node.
DELETION:
DELETION OF THE FIRST NODE:

1. Check if the list is empty by verifying if the head is None.


2. If the list is empty, there's nothing to delete.
3. If the list is not empty, update the head to point to the second node (if it exists) and set the "previous" pointer of
the new head to None.

IDELETION OF THE LAST NODE:

1. Check if the list is empty by verifying if the head is None.


2. If the list is empty, there's nothing to delete.
3. If the list has only one node, set the head to None to make the list empty.
4. Otherwise, traverse the list to reach the last node.
5. Update the "next" pointer of the second-to-last node to None to remove the reference to the last node.

DELETION OF A SPECIFIC NODE:

1. Traverse the list to the node to be deleted.


2. Update the "next" pointer of the previous node to point to the node after the one to be deleted.
3. Update the "previous" pointer of the node after the one to be deleted to point to the previous node.
CIRCULAR LINKED LIST
INSERTION:

INSERTION AT THE BEGINNING:


1. Create a new node with the desired data.
2. Set the "next" pointer of the new node to the current head.
3. Update the "next" pointer of the last node to point to the new node.
4. Update the head to be the new node.

INSERTION AT THE END:

1. Create a new node with the desired data.


2. Set the "next" pointer of the new node to the current head.
3. Update the "next" pointer of the last node to point to the new node.
4. Update the head to be the new node.

INSERTION AT A SPECIFIC POSITION:

1. Create a new node with the desired data.


2. Traverse the list to the node just before the desired position.
3. Set the "next" pointer of the new node to the node at the desired position.
4. Update the "next" pointer of the previous node to point to the new node.
DELETION:
DELETION OF THE FIRST NODE:

1. If the list is empty (the head is None), there's nothing to delete.


2. If the list has only one node (the head's next points to itself), set the head to None to make the list empty.
3. Otherwise, start at the head node.
4. Traverse the list until you reach the last node. You can identify the last node when the next node's "next" pointer points
back to the head.
5. Update the "next" pointer of the last node to point to the second node in the list, effectively removing the reference to
the first node.
6. Update the head to be the second node.

IDELETION OF THE LAST NODE:

1. If the list is empty (the head is None), there's nothing to delete.


2. If the list has only one node (the head's next points to itself), set the head to None to make the list empty.
3. Start at the head node.
4. Traverse the list until you reach the node just before the last node. You can identify the last node when the next
node's "next" pointer points back to the head.
5. Update the "next" pointer of the previous node (second-to-last node) to point back to the head, creating the circular
link without the last node.
DELETION:

DELETION OF A SPECIFIC NODE:

1. Traverse the list to the node to be deleted.


2. Update the "next" pointer of the previous node to point to the node after the one to be deleted.
ADVANTAGES OF LINKED LISTS:

EASY TO CHANGE SIZE: YOU CAN EASILY MAKE THEM BIGGER OR SMALLER AS NEEDED,
UNLIKE ARRAYS, WHICH HAVE A FIXED SIZE.

INSERTIONS AND DELETIONS: ADDING OR REMOVING ITEMS IS OFTEN FASTER AND EASIER
THAN WITH ARRAYS, ESPECIALLY IN THE MIDDLE.

LESS WASTED SPACE: THEY DON'T WASTE MEMORY WHEN ITEMS ARE REMOVED OR THE LIST
CHANGES SIZE.
DISADVANTAGES OF LINKED LISTS

HARD TO FIND THINGS: IT'S SLOWER TO FIND AN ITEM IN A LINKED LIST COMPARED TO AN
ARRAY BECAUSE YOU HAVE TO START AT THE BEGINNING AND LOOK THROUGH EACH ITEM
ONE BY ONE.

EXTRA MEMORY USED: THEY USE EXTRA MEMORY TO STORE THE CONNECTIONS BETWEEN
ITEMS, WHEREAS ARRAYS STORE ONLY THE DATA.

NOT GOOD FOR QUICK ACCESS: IF YOU NEED TO QUICKLY ACCESS ITEMS IN A SPECIFIC ORDER,
LINKED LISTS MIGHT NOT BE THE BEST CHOICE BECAUSE THEY'RE NOT GOOD FOR JUMPING
AROUND TO DIFFERENT ITEMS.

CAN BE MORE COMPLICATED: THEY CAN BE MORE COMPLICATED TO WORK WITH AND
UNDERSTAND COMPARED TO ARRAYS.
THANK YOU

You might also like