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

Data Structures

The document describes operations on doubly linked lists, circular singly linked lists, and circular doubly linked lists. It includes functions to insert, delete and traverse nodes in the lists. Main functions include insertFront, insertAfter, insertEnd, deleteNode and displayList for doubly linked lists, addFront, addEnd, addAfter, deleteNode and traverse for circular singly linked lists, and various functions like create, insert_begin, insert_end, insert_mid, delete_begin, delete_end, delete_mid to perform operations on circular doubly linked lists.

Uploaded by

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

Data Structures

The document describes operations on doubly linked lists, circular singly linked lists, and circular doubly linked lists. It includes functions to insert, delete and traverse nodes in the lists. Main functions include insertFront, insertAfter, insertEnd, deleteNode and displayList for doubly linked lists, addFront, addEnd, addAfter, deleteNode and traverse for circular singly linked lists, and various functions like create, insert_begin, insert_end, insert_mid, delete_begin, delete_end, delete_mid to perform operations on circular doubly linked lists.

Uploaded by

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

//Doubly linked list

#include <stdio.h>
#include <stdlib.h>

// node creation
struct Node {
int data;
struct Node* next;
struct Node* prev;
};

// insert node at the front


void insertFront(struct Node** head, int data) {
// allocate memory for newNode
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

// assign data to newNode


newNode->data = data;

// make newNode as a head


newNode->next = (*head);

// assign null to prev


newNode->prev = NULL;

// previous of head (now head is the second node) is newNode


if ((*head) != NULL)
(*head)->prev = newNode;

// head points to newNode


(*head) = newNode;
}

// insert a node after a specific node


void insertAfter(struct Node* prev_node, int data) {
// check if previous node is null
if (prev_node == NULL) {
printf("previous node cannot be null");
return;
}

// allocate memory for newNode


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

// assign data to newNode


newNode->data = data;

// set next of newNode to next of prev node


newNode->next = prev_node->next;

// set next of prev node to newNode


prev_node->next = newNode;

// set prev of newNode to the previous node


newNode->prev = prev_node;

// set prev of newNode's next to newNode


if (newNode->next != NULL)
newNode->next->prev = newNode;
}

// insert a newNode at the end of the list


void insertEnd(struct Node** head, int data) {
// allocate memory for node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

// assign data to newNode


newNode->data = data;

// assign null to next of newNode


newNode->next = NULL;

// store the head node temporarily (for later use)


struct Node* temp = *head;

// if the linked list is empty, make the newNode as head node


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

// if the linked list is not empty, traverse to the end of the linked list
while (temp->next != NULL)
temp = temp->next;

// now, the last node of the linked list is temp

// assign next of the last node (temp) to newNode


temp->next = newNode;

// assign prev of newNode to temp


newNode->prev = temp;
}

// delete a node from the doubly linked list


void deleteNode(struct Node** head, struct Node* del_node) {
// if head or del is null, deletion is not possible
if (*head == NULL || del_node == NULL)
return;

// if del_node is the head node, point the head pointer to the next of del_node
if (*head == del_node)
*head = del_node->next;

// if del_node is not at the last node, point the prev of node next to del_node
to the previous of del_node
if (del_node->next != NULL)
del_node->next->prev = del_node->prev;

// if del_node is not the first node, point the next of the previous node to the
next node of del_node
if (del_node->prev != NULL)
del_node->prev->next = del_node->next;

// free the memory of del_node


free(del_node);
}

// print the doubly linked list


void displayList(struct Node* node) {
struct Node* last;

while (node != NULL) {


printf("%d->", node->data);
last = node;
node = node->next;
}
if (node == NULL)
printf("NULL\n");
}

int main() {
// initialize an empty node
struct Node* head = NULL;

insertEnd(&head, 5);
insertFront(&head, 1);
insertFront(&head, 6);
insertEnd(&head, 9);

// insert 11 after head


insertAfter(head, 11);

// insert 15 after the seond node


insertAfter(head->next, 15);

displayList(head);

// delete the last node


deleteNode(&head, head->next->next->next->next->next);

displayList(head);
}

//Circular Singly Linked List

// C code to perform circular linked list operations

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

struct Node* addToEmpty(struct Node* last, int data) {


if (last != NULL) return last;

// allocate memory to the new node


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

// assign data to the new node


newNode->data = data;
// assign last to newNode
last = newNode;

// create link to iteself


last->next = last;

return last;
}

// add node to the front


struct Node* addFront(struct Node* last, int data) {
// check if the list is empty
if (last == NULL) return addToEmpty(last, data);

// allocate memory to the new node


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

// add data to the node


newNode->data = data;

// store the address of the current first node in the newNode


newNode->next = last->next;

// make newNode as head


last->next = newNode;

return last;
}

// add node to the end


struct Node* addEnd(struct Node* last, int data) {
// check if the node is empty
if (last == NULL) return addToEmpty(last, data);

// allocate memory to the new node


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

// add data to the node


newNode->data = data;

// store the address of the head node to next of newNode


newNode->next = last->next;

// point the current last node to the newNode


last->next = newNode;

// make newNode as the last node


last = newNode;

return last;
}

// insert node after a specific node


struct Node* addAfter(struct Node* last, int data, int item) {
// check if the list is empty
if (last == NULL) return NULL;

struct Node *newNode, *p;


p = last->next;
do {
// if the item is found, place newNode after it
if (p->data == item) {
// allocate memory to the new node
newNode = (struct Node*)malloc(sizeof(struct Node));

// add data to the node


newNode->data = data;

// make the next of the current node as the next of newNode


newNode->next = p->next;

// put newNode to the next of p


p->next = newNode;

// if p is the last node, make newNode as the last node


if (p == last) last = newNode;
return last;
}

p = p->next;
} while (p != last->next);

printf("\nThe given node is not present in the list");


return last;
}

// delete a node
void deleteNode(struct Node** last, int key) {
// if linked list is empty
if (*last == NULL) return;

// if the list contains only a single node


if ((*last)->data == key && (*last)->next == *last) {
free(*last);
*last = NULL;
return;
}

struct Node *temp = *last, *d;

// if last is to be deleted
if ((*last)->data == key) {
// find the node before the last node
while (temp->next != *last) temp = temp->next;

// point temp node to the next of last i.e. first node


temp->next = (*last)->next;
free(*last);
*last = temp->next;
}

// travel to the node to be deleted


while (temp->next != *last && temp->next->data != key) {
temp = temp->next;
}

// if node to be deleted was found


if (temp->next->data == key) {
d = temp->next;
temp->next = d->next;
free(d);
}
}

void traverse(struct Node* last) {


struct Node* p;

if (last == NULL) {
printf("The list is empty");
return;
}

p = last->next;

do {
printf("%d ", p->data);
p = p->next;

} while (p != last->next);
}

int main() {
struct Node* last = NULL;

last = addToEmpty(last, 6);


last = addEnd(last, 8);
last = addFront(last, 2);

last = addAfter(last, 10, 2);

traverse(last);

deleteNode(&last, 8);

printf("\n");

traverse(last);

return 0;
}

//Circular Doubly Linked List

#include<stdio.h>
#include<stdlib.h>

// structure of the node


struct node
{
struct node* prev;
int data;
struct node* next;
};

// global declaration of head node


struct node* head = NULL;
// function prototyping
struct node* create(int);
void insert_begin(int);
void insert_end(int);
void insert_mid(int, int);
void delete_begin();
void delete_end();
void delete_mid();
int search(int);
void update(int, int);
void sort();
int list_size();
void display();
void display_reverse(struct node*);
int get_data();
int get_position();

int main()
{
char user_active = 'Y';
int user_choice;
int data, position;

while(user_active == 'Y' || user_active == 'y')


{
printf("\n\n------ Circular Doubly Linked List -------\n");
printf("\n1. Insert a node at beginning");
printf("\n2. Insert a node at end");
printf("\n3. Insert a node at given position");
printf("\n\n4. Delete a node from beginning");
printf("\n5. Delete a node from end");
printf("\n6. Delete a node from given position");
printf("\n\n7. Print list from beginning");
printf("\n8. Print list from end");
printf("\n9. Search a node data");
printf("\n10. Update a node data");
printf("\n11. Sort the list");
printf("\n12. Exit");
printf("\n\n------------------------------\n");

printf("\nEnter your choice: ");


scanf("%d", &user_choice);

printf("\n------------------------------\n");
switch(user_choice)
{
case 1:
printf("\nInserting a node at beginning");
data = get_data();
insert_begin(data);
break;

case 2:
printf("\nInserting a node at end");
data = get_data();
insert_end(data);
break;
case 3:
printf("\nInserting a node at the given position");
data = get_data();
position = get_position();
insert_mid(position, data);
break;

case 4:
printf("\nDeleting a node from beginning\n");
delete_begin();
break;

case 5:
printf("\nDeleting a node from end\n");
delete_end();
break;

case 6:
printf("\nDelete a node from given position\n");
position = get_position();
delete_mid(position);
break;

case 7:
printf("\nPrinting the list from beginning\n\n");
display();
break;

case 8:
printf("\nPrinting the list from end\n\n");
if (head == NULL)
{
printf("\n\tList is Empty!\n");
} else {
display_reverse(head);
}
break;

case 9:
printf("\nSearching the node data");
data = get_data();
if (search(data) == 1) {
printf("\n\tNode Found\n");
} else {
printf("\n\tNode Not Found\n");
}
break;

case 10:
printf("\nUpdating the node data");
data = get_data();
position = get_position();
update(position, data);
break;

case 11:
sort();
printf("\nList was sorted\n");
break;
case 12:
printf("\nProgram was terminated\n\n");
return 0;

default:
printf("\n\tInvalid Choice\n");
}

printf("\n...............................\n");
printf("\nDo you want to continue? (Y/N) : ");
fflush(stdin);
scanf(" %c", &user_active);
}

return 0;
}

// creates a new node


struct node* create(int data)
{
struct node* new_node = (struct node*) malloc (sizeof(struct node));

if (new_node == NULL)
{
printf("\nMemory can't be allocated\n");
return NULL;
}

new_node->data = data;
new_node->next = NULL;
new_node->prev = NULL;

return new_node;
}

// insert a new node at the beginning of the list


void insert_begin(int data)
{
struct node* new_node = create(data);

if (new_node)
{
// if list is empty
if (head == NULL)
{
new_node->next = new_node;
new_node->prev = new_node;
head = new_node;
return;
}
head->prev->next = new_node;
new_node->prev = head->prev;
new_node->next = head;
head->prev = new_node;
head = new_node;
}
}

// inserts a new node at the end


void insert_end(int data)
{
struct node* new_node = create(data);

if (new_node)
{
if (head == NULL)
{
new_node->next = new_node;
new_node->prev = new_node;
head = new_node;
return;
}
head->prev->next = new_node;
new_node->prev = head->prev;
new_node->next = head;
head->prev = new_node;
}
}

// inserts node at the given position


void insert_mid(int position, int data)
{
// checking if the position is valid or not
if (position <= 0)
{
printf("\nInvalid Position\n");
} else if (head == NULL && position > 1) {
printf("\nInvalid Position\n");
} else if (head != NULL && position > list_size()) {
printf("\nInvalid Position\n");
} else if (position == 1) {
insert_begin(data);
} else {
struct node *new_node = create(data);

if (new_node != NULL) {
struct node *temp = head, *prev = NULL;
int i = 1;

// traverse the list to the given position


while (++i <= position) {
prev = temp;
temp = temp->next;
}

// update the prev node to the new noe


prev->next = new_node;

// update the new node to the temp (position node)


new_node->next = temp;
}
}
}

void delete_begin()
{
if (head == NULL) {
printf("\nList is Empty\n");
return;
} else if (head->next == head) {
free(head);
head = NULL;
return;
}

struct node* temp = head;


head->prev->next = head->next;
head->next->prev = head->prev;
head = head->next;

free(temp);
temp = NULL;
}

// deletes the node from the end of the list


void delete_end()
{
if (head == NULL) {
printf("\nList is Empty\n");
return;
} else if (head->next == head) {
free(head);
head = NULL;
return;
}

struct node* last_node = head->prev;

last_node->prev->next = head;
head->prev = last_node->prev;

free(last_node);
last_node = NULL;
}

// deletes the node from the given position


void delete_mid(int position)
{
if (position <= 0) {
printf("\n Invalid Position \n");
}
else if (position > list_size()) {
printf("\n Invalid position \n");
}
else if (position == 1) {
delete_begin();
}
else if (position == list_size()) {
delete_end();
}
else {
struct node *temp = head;
struct node *prev = NULL;
int i = 1;

while (i < position) {


prev = temp;
temp = temp->next;
i += 1;
}
prev->next = temp->next;
temp->next->prev = prev;
free(temp);
temp = NULL;
}
}

// search the node with the given key item


int search(int key)
{
if (head == NULL) {
printf("\n Not Found \n");
return 0;
}

struct node* temp = head;


do
{
if (temp->data == key) {
return 1;
}
temp = temp->next;
} while (temp != head);

return 0;
}

// updates the data of the given node position


void update(int position, int new_value)
{
if (head == NULL) {
printf("\n List is Empty \n");
return;
} else if (position <= 0 || position > list_size()) {
printf("\nInvalid position\n");
return;
}

struct node* temp = head;


int i = 0;

while (++i < position) {


temp = temp->next;
}
temp->data = new_value;
}

// sorts the linked list data using insertion sort


void sort()
{
if (head == NULL) {
printf("\nList is Empty\n");
return;
}
struct node* temp1 = head;
struct node* temp2 = head;
int key = 0, value;

do {
temp2 = temp1->next;

while(temp2 != head)
{
if (temp1->data > temp2->data)
{
value = temp1->data;
temp1->data = temp2->data;
temp2->data = value;
}
temp2 = temp2->next;
}
temp1 = temp1->next;
}while (temp1->next != head);

// display the list


void display()
{
if (head == NULL) {
printf("\nList is empty!\n");
return;
}

struct node* temp = head;


do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
}

// display the list from end to start


void display_reverse(struct node* temp)
{
if (temp->next == head) {
printf("%d ", temp->data);
return;
}

display_reverse(temp->next);
printf("%d ", temp->data);
}

// calculate the size of the list


int list_size()
{
if (head == NULL) {
return 0;
}

struct node* temp = head;


int count = 0;
do {
count += 1;
temp = temp->next;
} while (temp != head);

return count;
}

int get_data()
{
int data;
printf("\n\nEnter Data: ");
scanf("%d", &data);

return data;
}

int get_position()
{
int position;
printf("\n\nEnter Position: ");
scanf("%d", &position);

return position;
}

//Hash Table with Linked List

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

/* Node for storing an item in a Linked List */


struct node
{
int key;
int value;
struct node *next;
};

/* For storing a Linked List at each index of Hash Table */


struct arrayitem
{

struct node *head;


/* head pointing the first element of Linked List at an index of Hash Table
*/

struct node *tail;


/* tail pointing the last element of Linked List at an index of Hash Table */

};

struct arrayitem *array;


int size = 0; /* Determines the no. of elements present in Hash Table */
int max = 10; /* Determines the maximum capacity of Hash Table array */

/* This function creates an index corresponding to the every given key */


int hashcode(int key)
{
return (key % max);
}

struct node* get_element(struct node *list, int find_index);


void remove_element(int key);
void rehash();
void init_array();

void insert(int key, int value)


{
float n = 0.0;
/* n => Load Factor, keeps check on whether rehashing is required or not */

int index = hashcode(key);

/* Extracting Linked List at a given index */


struct node *list = (struct node*) array[index].head;

/* Creating an item to insert in the Hash Table */


struct node *item = (struct node*) malloc(sizeof(struct node));
item->key = key;
item->value = value;
item->next = NULL;

if (list == NULL)
{
/* Absence of Linked List at a given Index of Hash Table */

printf("Inserting %d(key) and %d(value) \n", key, value);


array[index].head = item;
array[index].tail = item;
size++;

}
else
{
/* A Linked List is present at given index of Hash Table */

int find_index = find(list, key);


if (find_index == -1)
{
/*
*Key not found in existing linked list
*Adding the key at the end of the linked list
*/

array[index].tail->next = item;
array[index].tail = item;
size++;

}else
{
/*
*Key already present in linked list
*Updating the value of already existing key
*/
struct node *element = get_element(list, find_index);
element->value = value;

//Calculating Load factor


n = (1.0 * size) / max;
if (n >= 0.75)
{
//rehashing

printf("going to rehash\n");
rehash();

void rehash()
{
struct arrayitem *temp = array;
/* temp pointing to the current Hash Table array */

int i = 0, n = max;
size = 0;
max = 2 * max;

/*
*array variable is assigned with newly created Hash Table
*with double of previous array size
*/
array = (struct arrayitem*) malloc(max * sizeof(struct node));
init_array();

for (i = 0; i < n; i++)


{

/* Extracting the Linked List at position i of Hash Table array */


struct node* list = (struct node*) temp[i].head;

if (list == NULL)
{

/* if there is no Linked List, then continue */


continue;

}
else
{
/*
*Presence of Linked List at i
*Keep moving and accessing the Linked List item until the end.
*Get one key and value at a time and add it to new Hash Table
array.
*/

while (list != NULL)


{
insert(list->key, list->value);
list = list->next;
}

}
temp = NULL;
}

/*
*This function finds the given key in the Linked List
*Returns it's index
*Returns -1 in case key is not present
*/
int find(struct node *list, int key)
{
int retval = 0;
struct node *temp = list;
while (temp != NULL)
{
if (temp->key == key)
{
return retval;
}
temp = temp->next;
retval++;
}
return -1;

/* Returns the node (Linked List item) located at given find_index */


struct node* get_element(struct node *list, int find_index)
{
int i = 0;
struct node *temp = list;
while (i != find_index)
{
temp = temp->next;
i++;
}
return temp;
}

/* To remove an element from Hash Table */


void remove_element(int key)
{
int index = hashcode(key);
struct node *list = (struct node*) array[index].head;

if (list == NULL)
{
printf("This key does not exists\n");

}
else
{
int find_index = find(list, key);

if (find_index == -1)
{
printf("This key does not exists\n");

}
else
{
struct node *temp = list;
if (temp->key == key)
{

array[index].head = temp->next;
printf("This key has been removed\n");
return;
}

while (temp->next->key != key)


{
temp = temp->next;
}

if (array[index].tail == temp->next)
{
temp->next = NULL;
array[index].tail = temp;

}
else
{
temp->next = temp->next->next;

printf("This key has been removed\n");

/* To display the contents of Hash Table */


void display()
{
int i = 0;
for (i = 0; i < max; i++)
{
struct node *temp = array[i].head;
if (temp == NULL)
{
printf("array[%d] has no elements\n", i);

}
else
{
printf("array[%d] has elements-: ", i);
while (temp != NULL)
{
printf("key= %d value= %d\t", temp->key, temp->value);
temp = temp->next;
}
printf("\n");

}
}
}

/* For initializing the Hash Table */


void init_array()
{
int i = 0;
for (i = 0; i < max; i++)
{
array[i].head = NULL;
array[i].tail = NULL;
}

/* Returns size of Hash Table */


int size_of_array()
{
return size;
}

void main()
{
int choice, key, value, n, c;
clrscr();

array = (struct arrayitem*) malloc(max * sizeof(struct arrayitem*));


init_array();

do {
printf("Implementation of Hash Table in C chaining with Singly Linked
List \n\n");
printf("MENU-: \n1.Inserting item in the Hash Table"
"\n2.Removing item from the Hash Table"
"\n3.Check the size of Hash Table"
"\n4.To display a Hash Table"
"\n\n Please enter your choice -: ");

scanf("%d", &choice);

switch(choice)
{

case 1:

printf("Inserting element in Hash Table\n");


printf("Enter key and value-:\t");
scanf("%d %d", &key, &value);
insert(key, value);

break;
case 2:

printf("Deleting in Hash Table \nEnter the key to delete-:");


scanf("%d", &key);
remove_element(key);

break;

case 3:

n = size_of_array();
printf("Size of Hash Table is-:%d\n", n);

break;

case 4:

display();

break;

default:

printf("Wrong Input\n");

printf("\nDo you want to continue-:(press 1 for yes)\t");


scanf("%d", &c);

}while(c == 1);

getch();

//Binary Tree

// Tree traversal in C

#include <stdio.h>
#include <stdlib.h>

struct node {
int item;
struct node* left;
struct node* right;
};

// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}

// Preorder traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}

// Postorder traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}

// Create a new Node


struct node* createNode(value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

// Insert on the left of the node


struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}

// Insert on the right of the node


struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
}

int main() {
struct node* root = createNode(1);
insertLeft(root, 2);
insertRight(root, 3);
insertLeft(root->left, 4);

printf("Inorder traversal \n");


inorderTraversal(root);

printf("\nPreorder traversal \n");


preorderTraversal(root);

printf("\nPostorder traversal \n");


postorderTraversal(root);
}

//bitmap

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// bitmap.c

// compile with one of


// gcc -std=c99 -Wall -Wextra -O bitmap.c
// gcc -std=c99 -Wall -Wextra -O -Dbitmap_64 bitmap.c

// tested on Linux, Mac OS X and Solaris

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// this should be in bitmap.h

// use dynamic allocation with these functions


// compile with -Dbitmap_64 to force 64-bit words
// compile with -O [dash capital o] for more efficient machine code

#ifdef bitmap_64
#define bitmap_type unsigned long long int
#else // assumed to be 32 bits
#define bitmap_type unsigned int
#endif

typedef struct {
int bits; // number of bits in the array
int words; // number of words in the array
bitmap_type *array;
} bitmap;

void bitmap_set (bitmap *b, int n); // n is a bit index


void bitmap_clear(bitmap *b, int n);
int bitmap_read (bitmap *b, int n);

bitmap * bitmap_allocate(int bits);


void bitmap_deallocate(bitmap *b);

void bitmap_print(bitmap *b);

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// this should be in bitmap.c

#include <stdio.h>
#include <stdlib.h>

#ifdef bitmap_64
#define bitmap_type unsigned long long int
#define bitmap_shift 6
#define bitmap_mask 63
#define bitmap_wordlength 64
#define bitmap_fmt "%016llx"
#else // assumed to be 32 bits
#define bitmap_type unsigned int
#define bitmap_shift 5
#define bitmap_mask 31
#define bitmap_wordlength 32
#define bitmap_fmt "%08x"
#endif

// get the types right


#define bitmap_one (bitmap_type)1

// we expect 0 <= n and n < b->bits, but it is not verified

void bitmap_set(bitmap *b, int n)


{
int word = n >> bitmap_shift; // n / bitmap_wordlength
int position = n & bitmap_mask; // n % bitmap_wordlength
b->array[word] |= bitmap_one << position;
}

void bitmap_clear(bitmap *b, int n)


{
int word = n >> bitmap_shift; // n / bitmap_wordlength
int position = n & bitmap_mask; // n % bitmap_wordlength
b->array[word] &= ~(bitmap_one << position);
}

int bitmap_read(bitmap *b, int n)


{
int word = n >> bitmap_shift; // n / bitmap_wordlength
int position = n & bitmap_mask; // n % bitmap_wordlength
return (b->array[word] >> position) & 1;
}

bitmap * bitmap_allocate(int bits)


{
// error-checking should be better :-)
bitmap *b = malloc(sizeof(bitmap));
b->bits = bits;
b->words = (bits + bitmap_wordlength - 1) / bitmap_wordlength;
// divide, but round up for the ceiling
b->array = calloc(b->words, sizeof(bitmap_type));
return b;
}

void bitmap_deallocate(bitmap *b)


{
// error-checking should be better :-)
free(b->array);
free(b);
}

void bitmap_print(bitmap *b)


{
for (int i = 0; i < b->words; i++)
{ printf(" " bitmap_fmt, b->array[i]); }
printf("\n");
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// this should be in bitmaptest.c

int main(void)
{
printf("sizeof(bitmap_type) = %zd\n", sizeof(bitmap_type));

const int testlen = 250; // number of bits in the bitmap


bitmap *B = bitmap_allocate(testlen);

for (int i = 0; i < bitmap_wordlength; i++) { printf("%d", bitmap_read(B, i)); }


printf("\n");
for (int i = 0; i < bitmap_wordlength; i++) { bitmap_set(B, i); }
for (int i = 0; i < bitmap_wordlength; i++) { printf("%d", bitmap_read(B, i)); }
printf("\n");
for (int i = 0; i < bitmap_wordlength; i++) { bitmap_clear(B, i); }
for (int i = 0; i < bitmap_wordlength; i++) { printf("%d", bitmap_read(B, i)); }
printf("\n");

bitmap_print(B);
for (int i = 0; i < testlen; i += 4) { bitmap_set(B, i); }
bitmap_print(B);
for (int i = 0; i < testlen; i += 4) { bitmap_set(B, i); }
bitmap_print(B);
for (int i = 0; i < testlen; i += 4) { bitmap_set(B, i+1); }
bitmap_print(B);
for (int i = 0; i < testlen; i += 4) { bitmap_set(B, i+2); }
bitmap_print(B);
for (int i = 0; i < testlen; i += 4) { bitmap_set(B, i+3); }
bitmap_print(B);
for (int i = 0; i < testlen; i += 4) { bitmap_clear(B, i+2); }
bitmap_print(B);
for (int i = 0; i < testlen; i += 4) { bitmap_clear(B, i); }
bitmap_print(B);

bitmap_deallocate(B);
B = NULL; // no dangling pointers!

return 0;
}

//red black trees

// Implementing Red-Black Tree in C

#include <stdio.h>
#include <stdlib.h>

enum nodeColor {
RED,
BLACK
};

struct rbNode {
int data, color;
struct rbNode *link[2];
};

struct rbNode *root = NULL;

// Create a red-black tree


struct rbNode *createNode(int data) {
struct rbNode *newnode;
newnode = (struct rbNode *)malloc(sizeof(struct rbNode));
newnode->data = data;
newnode->color = RED;
newnode->link[0] = newnode->link[1] = NULL;
return newnode;
}

// Insert an node
void insertion(int data) {
struct rbNode *stack[98], *ptr, *newnode, *xPtr, *yPtr;
int dir[98], ht = 0, index;
ptr = root;
if (!root) {
root = createNode(data);
return;
}

stack[ht] = root;
dir[ht++] = 0;
while (ptr != NULL) {
if (ptr->data == data) {
printf("Duplicates Not Allowed!!\n");
return;
}
index = (data - ptr->data) > 0 ? 1 : 0;
stack[ht] = ptr;
ptr = ptr->link[index];
dir[ht++] = index;
}
stack[ht - 1]->link[index] = newnode = createNode(data);
while ((ht >= 3) && (stack[ht - 1]->color == RED)) {
if (dir[ht - 2] == 0) {
yPtr = stack[ht - 2]->link[1];
if (yPtr != NULL && yPtr->color == RED) {
stack[ht - 2]->color = RED;
stack[ht - 1]->color = yPtr->color = BLACK;
ht = ht - 2;
} else {
if (dir[ht - 1] == 0) {
yPtr = stack[ht - 1];
} else {
xPtr = stack[ht - 1];
yPtr = xPtr->link[1];
xPtr->link[1] = yPtr->link[0];
yPtr->link[0] = xPtr;
stack[ht - 2]->link[0] = yPtr;
}
xPtr = stack[ht - 2];
xPtr->color = RED;
yPtr->color = BLACK;
xPtr->link[0] = yPtr->link[1];
yPtr->link[1] = xPtr;
if (xPtr == root) {
root = yPtr;
} else {
stack[ht - 3]->link[dir[ht - 3]] = yPtr;
}
break;
}
} else {
yPtr = stack[ht - 2]->link[0];
if ((yPtr != NULL) && (yPtr->color == RED)) {
stack[ht - 2]->color = RED;
stack[ht - 1]->color = yPtr->color = BLACK;
ht = ht - 2;
} else {
if (dir[ht - 1] == 1) {
yPtr = stack[ht - 1];
} else {
xPtr = stack[ht - 1];
yPtr = xPtr->link[0];
xPtr->link[0] = yPtr->link[1];
yPtr->link[1] = xPtr;
stack[ht - 2]->link[1] = yPtr;
}
xPtr = stack[ht - 2];
yPtr->color = BLACK;
xPtr->color = RED;
xPtr->link[1] = yPtr->link[0];
yPtr->link[0] = xPtr;
if (xPtr == root) {
root = yPtr;
} else {
stack[ht - 3]->link[dir[ht - 3]] = yPtr;
}
break;
}
}
}
root->color = BLACK;
}

// Delete a node
void deletion(int data) {
struct rbNode *stack[98], *ptr, *xPtr, *yPtr;
struct rbNode *pPtr, *qPtr, *rPtr;
int dir[98], ht = 0, diff, i;
enum nodeColor color;

if (!root) {
printf("Tree not available\n");
return;
}

ptr = root;
while (ptr != NULL) {
if ((data - ptr->data) == 0)
break;
diff = (data - ptr->data) > 0 ? 1 : 0;
stack[ht] = ptr;
dir[ht++] = diff;
ptr = ptr->link[diff];
}

if (ptr->link[1] == NULL) {
if ((ptr == root) && (ptr->link[0] == NULL)) {
free(ptr);
root = NULL;
} else if (ptr == root) {
root = ptr->link[0];
free(ptr);
} else {
stack[ht - 1]->link[dir[ht - 1]] = ptr->link[0];
}
} else {
xPtr = ptr->link[1];
if (xPtr->link[0] == NULL) {
xPtr->link[0] = ptr->link[0];
color = xPtr->color;
xPtr->color = ptr->color;
ptr->color = color;

if (ptr == root) {
root = xPtr;
} else {
stack[ht - 1]->link[dir[ht - 1]] = xPtr;
}

dir[ht] = 1;
stack[ht++] = xPtr;
} else {
i = ht++;
while (1) {
dir[ht] = 0;
stack[ht++] = xPtr;
yPtr = xPtr->link[0];
if (!yPtr->link[0])
break;
xPtr = yPtr;
}

dir[i] = 1;
stack[i] = yPtr;
if (i > 0)
stack[i - 1]->link[dir[i - 1]] = yPtr;

yPtr->link[0] = ptr->link[0];

xPtr->link[0] = yPtr->link[1];
yPtr->link[1] = ptr->link[1];

if (ptr == root) {
root = yPtr;
}

color = yPtr->color;
yPtr->color = ptr->color;
ptr->color = color;
}
}

if (ht < 1)
return;

if (ptr->color == BLACK) {
while (1) {
pPtr = stack[ht - 1]->link[dir[ht - 1]];
if (pPtr && pPtr->color == RED) {
pPtr->color = BLACK;
break;
}

if (ht < 2)
break;

if (dir[ht - 2] == 0) {
rPtr = stack[ht - 1]->link[1];
if (!rPtr)
break;

if (rPtr->color == RED) {
stack[ht - 1]->color = RED;
rPtr->color = BLACK;
stack[ht - 1]->link[1] = rPtr->link[0];
rPtr->link[0] = stack[ht - 1];

if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
dir[ht] = 0;
stack[ht] = stack[ht - 1];
stack[ht - 1] = rPtr;
ht++;

rPtr = stack[ht - 1]->link[1];


}

if ((!rPtr->link[0] || rPtr->link[0]->color == BLACK) &&


(!rPtr->link[1] || rPtr->link[1]->color == BLACK)) {
rPtr->color = RED;
} else {
if (!rPtr->link[1] || rPtr->link[1]->color == BLACK) {
qPtr = rPtr->link[0];
rPtr->color = RED;
qPtr->color = BLACK;
rPtr->link[0] = qPtr->link[1];
qPtr->link[1] = rPtr;
rPtr = stack[ht - 1]->link[1] = qPtr;
}
rPtr->color = stack[ht - 1]->color;
stack[ht - 1]->color = BLACK;
rPtr->link[1]->color = BLACK;
stack[ht - 1]->link[1] = rPtr->link[0];
rPtr->link[0] = stack[ht - 1];
if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
break;
}
} else {
rPtr = stack[ht - 1]->link[0];
if (!rPtr)
break;

if (rPtr->color == RED) {
stack[ht - 1]->color = RED;
rPtr->color = BLACK;
stack[ht - 1]->link[0] = rPtr->link[1];
rPtr->link[1] = stack[ht - 1];

if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
dir[ht] = 1;
stack[ht] = stack[ht - 1];
stack[ht - 1] = rPtr;
ht++;

rPtr = stack[ht - 1]->link[0];


}
if ((!rPtr->link[0] || rPtr->link[0]->color == BLACK) &&
(!rPtr->link[1] || rPtr->link[1]->color == BLACK)) {
rPtr->color = RED;
} else {
if (!rPtr->link[0] || rPtr->link[0]->color == BLACK) {
qPtr = rPtr->link[1];
rPtr->color = RED;
qPtr->color = BLACK;
rPtr->link[1] = qPtr->link[0];
qPtr->link[0] = rPtr;
rPtr = stack[ht - 1]->link[0] = qPtr;
}
rPtr->color = stack[ht - 1]->color;
stack[ht - 1]->color = BLACK;
rPtr->link[0]->color = BLACK;
stack[ht - 1]->link[0] = rPtr->link[1];
rPtr->link[1] = stack[ht - 1];
if (stack[ht - 1] == root) {
root = rPtr;
} else {
stack[ht - 2]->link[dir[ht - 2]] = rPtr;
}
break;
}
}
ht--;
}
}
}

// Print the inorder traversal of the tree


void inorderTraversal(struct rbNode *node) {
if (node) {
inorderTraversal(node->link[0]);
printf("%d ", node->data);
inorderTraversal(node->link[1]);
}
return;
}

// Driver code
int main() {
int ch, data;
while (1) {
printf("1. Insertion\t2. Deletion\n");
printf("3. Traverse\t4. Exit");
printf("\nEnter your choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the element to insert:");
scanf("%d", &data);
insertion(data);
break;
case 2:
printf("Enter the element to delete:");
scanf("%d", &data);
deletion(data);
break;
case 3:
inorderTraversal(root);
printf("\n");
break;
case 4:
exit(0);
default:
printf("Not available\n");
break;
}
printf("\n");
}
return 0;
}

You might also like