DS Jobayer 1st
DS Jobayer 1st
// inserts a new node on the front of // 3. Make next of new node as head
1. Singly Linked list: It is the simplest type of linked list in which every node contains some data and a pointer to the next
node of the same data type. The node contains a pointer to the next node means that the node stores the address of the
next node in the sequence. A single linked list allows the traversal of data only in one way.
class Node {
public:
int data;
Node* next;
};
2
int main()
int data;
}; head->data = 1;
// given node
{ second->data = 2;
second->next = third;
third->data = 3;
n = n->next; printList(head);
} return 0;
} }
Output
123
3
2. Doubly Linked list: A doubly linked list or a two-way linked list is a more complex type of linked list that contains a
pointer to the next as well as the previous node in sequence.
struct Node {
int data;
};
Creation and Traversal of Doubly Linked List: // Function to push a new element in
public:
}; new_node->prev = NULL;
4
// the new node cout << " " << last->data << " ";
(*head_ref)->prev = new_node; }
} {
cout << " " << node->data << " "; // linked list becomes 1->7->6->NULL
node = node->next;
printList(head);
Output
Circular Linked List: A
Created circular
DLL is: linked list is that
in which the last node contains the pointer to the
Traversal
first node in forward
of the list. Below isdirection
the structure of the
Circular Linked List:
1 7 6
6 7 1
5
class Node {
public:
int data;
Node* next;
};
temp->next = ptr1;
class Node {
};
*head_ref = ptr1;
// beginning of Circular LL
do {
}
cout << "Contents of Circular"
int main()
// Function call
{
printList(head);
// Initialize list as empty
}
// Created linked list will
Output
11 2 56 12
The code length of a character depends on how frequently it occurs in the given text.
The character which occurs most frequently gets the smallest code.
The character which occurs least frequently gets the largest code.
Example:
#include<iostream> child1->traverse(code+'1');
#include<string> }else{
using namespace std; cout << "Data: " << data<< ",
char data; }
} frequency
data = 0; frequency[int(str[i])]++;
child0=c0; }
} if(frequency[i]){
in queue }
} while(qu.size() >1){
child0->traverse(code+'0'); qu.pop();
//add 0 with the code as left child node *c1 = new node(qu.top());
8
main(){ huffmanCoding(str);
1. Node Structure:
A node structure is defined to represent each node in the Huffman tree. Each node has information
about the character (data), frequency (freq), and pointers to its left and right children (child0 and
child1).
• Constructors for the node structure are defined. There's one constructor for leaf nodes, taking a
character and frequency, and another for internal nodes, combining two child nodes.
• The < operator is overloaded to define the priority of nodes in the priority queue based on their
frequencies.
} } else {
(factorial(n + 1) * factorial(n)); }
} return 0;
int main() { }
10
Explanation:
1. Factorial Function (factorial): This function calculates the factorial of a given number n. The base cases are
defined for n = 0 and n = 1, where the factorial is 1. For other values, it recursively calculates n * factorial(n - 1).
2. Count Binary Trees Function (countBinaryTrees):
• This function calculates the number of unique binary trees that can be formed with n nodes.
• It uses the Catalan number formula: Catalan(n) = (2n)! / ((n + 1)! * n!). The function calls the factorial function
to calculate the factorials needed for this formula.
4. Output:
• The program outputs the number of binary trees that can be formed with the specified number of nodes.
• This defines a simple structure for a binary tree node, where each node has an integer data value
and pointers to its left and right children.
int l = totalNodes(root->left);
int r = totalNodes(root->right);
return 1 + l + r;
}
• The newNode function is a helper function that creates a new node with the given data value
int main()
{
node* roo t = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(9);
root->right->right = newNode(8);
root->left->left->left = newNode(6);
root->left->left->right = newNode(7);
return 0;
• In the main function, a binary tree is constructed using the newNode function to create nodes with
different data values.
• The totalNodes function is then called with the root of the tree, and the result (total number of
nodes) is printed to the console.