Category: Data Structures

Introduction to Universal Hashing in Data Structure

Hashing is a great practical tool, with an interesting and subtle theory too. In addition to its use as a dictionary data structure, hashing also comes up in many different areas, including cryptography and complexity theory. This article discusses an important notion: Universal Hashing (also known as universal hash function families).Universal Hashing refers to selecting a […]

Continue Reading

Indexed Priority Queue with Implementation

  #include using namespace std;  template   class indexed_priority_queue {          unordered_map m;          vector v;          long long numberOfElement;          Comparator comp;          long long capacity = LLONG_MAX;          long long int getValueIndex(T1 key)    {        if (m[key] == 0) {            cout

Continue Reading

Print Adjacency List for a Directed Graph

An Adjacency List is used for representing graphs. Here, for every vertex in the graph, we have a list of all the other vertices which the particular vertex has an edge to.Problem: Given the adjacency list and number of vertices and edges of a graph, the task is to represent the adjacency list for a […]

Continue Reading

Modify a Linked List to contain last occurrences of every duplicate element

#include using namespace std;  class Node {public:    int data;    Node* next;      Node(int x)    {        this->data = x;        this->next = NULL;    }};  Node* reverseList(Node* head){    Node *prev = NULL, *nextNode = NULL;    while (head != NULL) {                          nextNode = head->next;                          head->next = prev;          prev = head;          head = nextNode;    }    return prev;}  Node* Remove_Dup_Keep_Last_Occurence(    Node* head){        Node* dummy = new Node(-1);    dummy->next = head;          dummy->next = reverseList(dummy->next);          unordered_set visited;      Node *currNode = dummy, *nextNode;          while (currNode != NULL           && currNode->next != NULL) […]

Continue Reading

When to use each Sorting Algorithms | Set 2

Sorting is the process of arranging a set of data in a specific order, which may be numerical (ascending, descending) or lexicographical (alphabetical) order.Why Sorting Is Required?Sorting is very essential when there is a need to highly optimize the searching algorithm. For example, let’s assume two cases for searching for a certain element.Case 1: Searching […]

Continue Reading

Program to implement Hash Table using Open Addressing

#include #include   struct HashNode {    int key;    int value;};  const int capacity = 20;int size = 0;  struct HashNode** arr;struct HashNode* dummy;  void insert(int key, int V){      struct HashNode* temp        = (struct HashNode*)malloc(sizeof(struct HashNode));    temp->key = key;    temp->value = V;              int hashIndex = key % capacity;          while (arr[hashIndex] != NULL           && arr[hashIndex]->key != key           && arr[hashIndex]->key != -1) {        hashIndex++;        hashIndex %= capacity;    }              if (arr[hashIndex] == NULL        || arr[hashIndex]->key == -1)        size++;      arr[hashIndex] = […]

Continue Reading

Subtract 1 from a number represented as Linked List

  #include using namespace std;  class Node {public:    int data;    Node* next;};  Node* newNode(int data){        Node* new_node = new Node;    new_node->data = data;    new_node->next = NULL;          return new_node;}  int subtractOneUtil(Node* head){          if (head == NULL)        return -1;              int borrow = subtractOneUtil(        head->next);          if (borrow == -1) {                          if (head->data == 0) {            head->data = 9;            return -1;        }                          else {            head->data = head->data – 1;            return 0;        }    }          else {        return 0;    }}  Node* subtractOne(Node* head){              subtractOneUtil(head);              while (head and head->next           and head->data == 0) […]

Continue Reading