DSA – Data Structures and Algorithm

Chapter 6 - Circular Linked Lists

Chapter 6 – Circular Linked Lists

Chapter 6: Circular Linked Lists Welcome to Chapter 6! In this chapter, we will explore Circular Linked Lists, a variation of linked lists where the last node in the list points back to the first node, forming a circular structure. This type of linked list is particularly useful in scenarios where you need a continuous loop through your data. What is a Circular Linked List? A Circular Linked List is a type of linked list in which the last node points back to the first node instead of having a NULL pointer. This circular structure allows you to traverse the list from any node and return to the starting node without having to check for the end. Here’s a graphical representation of a circular linked list: [Data | Next] -> [Data | Next] -> [Data | Next] -+ ^ | | | +——————————————-+ In this diagram, you can see that the next pointer of the last node points back to the first node, creating a loop. Why Use Circular Linked Lists? Circular linked lists are useful for scenarios that require a continuous loop or when you need to repeatedly cycle through the elements. Some applications include: Round-Robin Scheduling: In operating systems, circular linked lists can be used to manage tasks in a round-robin fashion. Circular Buffers: They are used in scenarios where you have a fixed-size buffer and need to overwrite old data when the buffer is full. Navigation Systems: For continuous navigation without end, like in a continuous data feed. Just like how Emancipation Edutech Private Limited might use circular linked lists to cycle through learning modules or resources efficiently, these structures can simplify repetitive tasks. Building a Circular Linked List To implement a circular linked list, we start by defining the node structure: #include <stdio.h> #include <stdlib.h> // Define a node structure struct Node { int data; struct Node* next; }; Creating a Node Creating a node is similar to singly linked lists, but with an additional step to handle the circular nature: struct Node* createNode(int value) { struct Node* newNode = (struct Node*) malloc(sizeof(struct Node)); newNode->data = value; newNode->next = newNode; // Point to itself initially return newNode; } Inserting Elements in a Circular Linked List Here’s how you can insert nodes into a circular linked list: At the Beginning: void insertAtBeginning(struct Node** head, int value) { struct Node* newNode = createNode(value); if (*head == NULL) { *head = newNode; newNode->next = *head; return; } struct Node* temp = *head; while (temp->next != *head) { temp = temp->next; } newNode->next = *head; temp->next = newNode; *head = newNode; } This function inserts a new node at the start of the circular list and adjusts the next pointers to maintain the circular structure. At the End: void insertAtEnd(struct Node** head, int value) { struct Node* newNode = createNode(value); if (*head == NULL) { *head = newNode; newNode->next = *head; return; } struct Node* temp = *head; while (temp->next != *head) { temp = temp->next; } temp->next = newNode; newNode->next = *head; } This function adds a new node to the end of the circular list by traversing to the last node and updating pointers. Inserting After a Given Node: void insertAfter(struct Node* prevNode, int value) { if (prevNode == NULL) { printf("The given previous node cannot be NULL"); return; } struct Node* newNode = createNode(value); newNode->next = prevNode->next; prevNode->next = newNode; } This function inserts a new node after a given node, adjusting the next pointers accordingly. Traversing a Circular Linked List Traversal in a circular linked list involves starting from the head and continuing until you return to the head: void traverse(struct Node* head) { if (head == NULL) return; struct Node* temp = head; do { printf("%d -> ", temp->data); temp = temp->next; } while (temp != head); printf("(back to head)\n"); } This function loops through the list and prints each node’s data until it returns to the head. Example: Creating and Traversing a Circular Linked List Here’s a sample program to create a circular linked list and traverse it: int main() { struct Node* head = NULL; // Inserting nodes insertAtEnd(&head, 10); insertAtEnd(&head, 20); insertAtBeginning(&head, 5); insertAtEnd(&head, 30); insertAfter(head->next, 15); // Insert 15 after the node with data 10 // Traverse the list traverse(head); return 0; } Output: 5 -> 10 -> 15 -> 20 -> 30 -> (back to head) Deleting Nodes in a Circular Linked List Deleting nodes in a circular linked list involves similar steps as in singly linked lists but requires updating the next pointers to maintain the circular structure: Deleting the First Node: void deleteFirstNode(struct Node** head) { if (*head == NULL) return; struct Node* temp = *head; if ((*head)->next == *head) { free(*head); *head = NULL; return; } struct Node* last = *head; while (last->next != *head) { last = last->next; } last->next = (*head)->next; *head = (*head)->next; free(temp); } Deleting the Last Node: void deleteLastNode(struct Node** head) { if (*head == NULL) return; struct Node* temp = *head; if ((*head)->next == *head) { free(*head); *head = NULL; return; } struct Node* prev = NULL; while (temp->next != *head) { prev = temp; temp = temp->next; } prev->next = *head; free(temp); } Deleting a Node by Value: void deleteNodeByValue(struct Node** head, int value) { if (*head == NULL) return; struct Node* temp = *head; struct Node* prev = NULL; do { if (temp->data == value) { if (prev == NULL) { // Node to be deleted is the head deleteFirstNode(head); return; } else { prev->next = temp->next; free(temp); return; } } prev = temp; temp = temp->next; } while (temp != *head); } Advantages and Disadvantages of Circular Linked Lists Advantages: Circular Traversal: Easily traverse through the list without needing to check for the end. Efficient for Round-Robin Scheduling: Ideal for scenarios where cyclic behavior is required. Disadvantages: Complexity: Slightly more complex to manage compared to singly linked lists. Potential for Infinite Loops: If not handled carefully, circular traversal can lead to infinite loops. Wrapping Up Chapter 6

Chapter 6 – Circular Linked Lists Read More »

Chapter 4 - Linked Lists

Chapter 4 – Linked Lists

Chapter 4: Linked Lists Welcome to Chapter 4! Now that you’ve become familiar with pointers and dynamic memory allocation, it’s time to explore how pointers unlock one of the most fundamental and powerful data structures: Linked Lists. If you’ve ever worked with arrays and found their fixed size to be limiting, you’re going to love linked lists. They provide flexibility, allowing you to dynamically manage data, making them perfect for scenarios where you don’t know the size of the data in advance. What is a Linked List? A Linked List is a linear data structure where each element (commonly called a node) points to the next element in the sequence. Unlike arrays, linked lists do not store elements in contiguous memory locations. Instead, each node contains: Here’s a simple graphical representation of a linked list: Each node points to the next, and the last node points to NULL, which signifies the end of the list. Why Use Linked Lists? Linked lists are dynamic, meaning you can add or remove elements from them without worrying about fixed size or shifting elements (as with arrays). Some reasons to use linked lists include: Linked lists, like those used in the backend of platforms like Emancipation Edutech Private Limited, efficiently handle data structures that grow dynamically, such as managing student records or tracking progress across multiple courses. Types of Linked Lists There are several variations of linked lists, each suited to different tasks: For now, we’ll focus on Singly Linked Lists, as they form the foundation for understanding more complex linked lists. Building a Singly Linked List To create a linked list, we first need to define the node structure. In C/C++, this is usually done using struct: Each node will have two fields: Creating a Node We’ll use dynamic memory allocation (malloc) to create nodes dynamically at runtime: This function allocates memory for a new node, initializes its data field, and sets the next pointer to NULL. Inserting Elements in a Linked List Now that we have a node, let’s learn how to insert it into a linked list. There are three common ways to insert nodes: Traversing a Linked List After inserting elements, you’ll want to traverse the list to access or display the data: This function loops through the list, printing each node’s data until it reaches the end (where next is NULL). Example: Creating and Traversing a Linked List Here’s an example of creating a linked list and printing its contents: Output: In this example, we created a list with four nodes and then printed it. Deleting Nodes in a Linked List Removing nodes from a linked list is just as important as adding them. There are three common ways to delete nodes: Advantages of Linked Lists Linked lists offer several advantages over arrays, making them suitable for situations where dynamic memory management is essential: Disadvantages of Linked Lists Linked lists also come with a few drawbacks: Wrapping Up Chapter 4 In this chapter, you’ve learned all about Singly Linked Lists, from creating and inserting nodes to deleting and traversing through them. Linked lists are the backbone of many advanced data structures, and mastering them opens up a world of possibilities in dynamic data management. Key takeaways: Keep practicing by implementing different types of linked lists, and if you ever need more resources, feel free to check out digilearn.cloud for interactive coding exercises. Next up: Stacks and Queues

Chapter 4 – Linked Lists Read More »

Chapter 5 - Doubly Linked Lists

Chapter 5 – Doubly Linked Lists

Chapter 5: Doubly Linked Lists Welcome to Chapter 5! In this chapter, we’ll explore the Doubly Linked List—a more versatile type of linked list compared to the singly linked list. If you think of singly linked lists as one-way streets, doubly linked lists are like multi-lane roads with traffic flowing in both directions. This added flexibility can be particularly useful in various applications where bidirectional traversal is required. What is a Doubly Linked List? A Doubly Linked List is a type of linked list where each node has two pointers: Here’s a graphical representation: Each node contains a data field, a next pointer, and a prev pointer. The prev pointer of the first node is NULL, and the next pointer of the last node is NULL. Why Use Doubly Linked Lists? Doubly linked lists provide several advantages over singly linked lists: Doubly linked lists are used in applications such as navigation systems, where moving in both directions is necessary, similar to handling user interactions in educational platforms like Emancipation Edutech Private Limited. Building a Doubly Linked List To implement a doubly linked list, we start by defining the node structure: Each node now includes an additional pointer to the previous node. Creating a Node Here’s how you can create a new node: This function initializes a new node with the given value and sets both the next and prev pointers to NULL. Inserting Elements in a Doubly Linked List Here’s how you can insert nodes into a doubly linked list: Traversing a Doubly Linked List Traversal in a doubly linked list can be done in both directions: Example: Creating and Traversing a Doubly Linked List Here’s a sample program to create a doubly linked list and traverse it: Output: Deleting Nodes in a Doubly Linked List Deleting nodes in a doubly linked list is straightforward because you have pointers to both the next and previous nodes: Advantages and Disadvantages of Doubly Linked Lists Advantages: Disadvantages: Wrapping Up Chapter 5 In this chapter, we delved into Doubly Linked Lists—an enhanced version of singly linked lists that allows bidirectional traversal and simplifies node deletion. Mastering doubly linked lists will give you greater flexibility in handling dynamic data. Key takeaways: As always, if you want to see more examples or practice with interactive exercises, visit digilearn.cloud. And remember, **Emancipation

Chapter 5 – Doubly Linked Lists Read More »

Chapter 2: Understanding Data Structures

Chapter 2: Understanding Data Structures

In the previous chapter, we discussed what DSA is and got familiar with algorithmic notations like Big O, Little o, and others. Now, let’s dive into data structures — one of the core pillars of DSA. What Are Data Structures? A data structure is a way of organizing data in a computer so that it can be used efficiently. Imagine you have a closet, and you want to keep all your clothes in an organized way — like folding shirts on one shelf and hanging jackets on another. Data structures work the same way for organizing information in a program. Each data structure has a specific purpose and is better suited for particular kinds of tasks. For example, some data structures are great for storing data in order, while others are perfect for quickly finding a specific piece of information. Types of Data Structures Data structures can be classified into two major types: Let’s start with linear data structures and go through each one in detail. Arrays An Array is the simplest and most commonly used data structure. It is a collection of elements (values or variables), each identified by an index or a key. Arrays are usually used to store multiple items of the same type together. Key Points About Arrays: Example: In the example above, arr[0] is 10, arr[1] is 20, and so on. Pros: Cons: Linked Lists A Linked List is a linear data structure where elements (called nodes) are linked using pointers. Unlike arrays, Linked Lists can grow or shrink in size dynamically, which makes them more flexible. Each node contains two parts: Types of Linked Lists: Example: Here, each Node has an integer (data) and a pointer (next) that points to the next node. Pros: Cons: Stacks A Stack is a collection of elements where you can only add or remove elements from one end, called the top. It follows the LIFO (Last In, First Out) principle. Imagine a stack of books; the last book you place on top is the first one you’ll take out. Key Operations in Stacks: Example: Pros: Cons: Queues A Queue is another linear data structure but operates under the FIFO (First In, First Out) principle. Imagine standing in a queue at a ticket counter — the first person to stand in line is the first one to be served. Key Operations in Queues: Example: Pros: Cons: Choosing the Right Data Structure When choosing a data structure, always ask yourself: Each data structure has its strengths and weaknesses, so choosing the right one depends on the problem you’re trying to solve. Wrapping Up We’ve explored some of the key linear data structures: Arrays, Linked Lists, Stacks, and Queues. These structures are foundational and are used frequently in many real-world scenarios. Understanding how they work and when to use them will make your programming much more efficient. In the next chapter, we’ll dive into non-linear data structures, such as Trees and Graphs, and see how they can be used to solve more complex problems. Stay tuned!

Chapter 2: Understanding Data Structures Read More »

Introduction to DSA (Data Structures and Algorithms)

Introduction to DSA (Data Structures and Algorithms)

What is DSA? When we talk about DSA, we’re referring to Data Structures and Algorithms. Let’s break that down: In simple terms, think of DSA as the combination of tools (data structures) and methods (algorithms) that help you solve complex problems in an optimized way. It’s like having a toolkit where each tool (data structure) is suited for a specific job, and the method (algorithm) is how you use that tool. DSA is at the heart of programming and problem-solving, which makes it essential for anyone diving into computer science, coding, or software engineering. Why Learn DSA? Learning DSA equips you with the knowledge to: Algorithmic Notation Before jumping into algorithms, let’s talk about notation. When discussing algorithms, we use notations to describe how fast or slow they are. This helps us understand if an algorithm is efficient enough for a particular problem. Notations to Measure Complexity 1. Big O Notation (O) The most commonly used notation to describe how the runtime of an algorithm increases as the input size increases. Big O focuses on the worst-case scenario. For example: Why it matters: Knowing the worst-case performance helps you plan for the worst possible situation your code might face. 2. Small o Notation (o) This notation is used to describe algorithms that are better than what Big O suggests but don’t quite reach the next best level. It’s a more precise way of saying how close the algorithm’s runtime is to the ideal scenario. For example, if you have a sorting algorithm that’s slightly faster than O(n log n), we might say it’s o(n log n). Capital and Small Notations: What’s the Difference? When we talk about notations like O, Ω, θ, and o, the size of the letter tells us something important: Example: Linear Search vs. Binary Search Let’s take an example of searching for a number in a list: Wrapping It Up Understanding algorithmic notation helps you gauge how well your code will perform as your input grows larger. It’s a critical skill, especially when working on big projects where efficiency can make or break the application. In the next section, we’ll dive into more practical algorithms and how different data structures help us solve various problems. So, stay tuned, and we’ll explore sorting, searching, and more exciting concepts in the world of DSA!

Introduction to DSA (Data Structures and Algorithms) Read More »

Scroll to Top
Contact Form Demo