Data Structures
Data Structures
#include <stdio.h>
#include <stdlib.h>
// node creation
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// if the linked list is not empty, traverse to the end of the linked list
while (temp->next != NULL)
temp = temp->next;
// 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;
int main() {
// initialize an empty node
struct Node* head = NULL;
insertEnd(&head, 5);
insertFront(&head, 1);
insertFront(&head, 6);
insertEnd(&head, 9);
displayList(head);
displayList(head);
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
return last;
}
return last;
}
return last;
}
p = p->next;
} while (p != last->next);
// delete a node
void deleteNode(struct Node** last, int key) {
// if linked list is empty
if (*last == NULL) return;
// if last is to be deleted
if ((*last)->data == key) {
// find the node before the last node
while (temp->next != *last) temp = temp->next;
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;
traverse(last);
deleteNode(&last, 8);
printf("\n");
traverse(last);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
char user_active = 'Y';
int user_choice;
int data, position;
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;
}
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;
}
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;
}
}
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;
}
}
if (new_node != NULL) {
struct node *temp = head, *prev = NULL;
int i = 1;
void delete_begin()
{
if (head == NULL) {
printf("\nList is Empty\n");
return;
} else if (head->next == head) {
free(head);
head = NULL;
return;
}
free(temp);
temp = NULL;
}
last_node->prev->next = head;
head->prev = last_node->prev;
free(last_node);
last_node = NULL;
}
return 0;
}
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_reverse(temp->next);
printf("%d ", temp->data);
}
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;
}
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
};
if (list == NULL)
{
/* Absence of Linked List at a given Index of Hash Table */
}
else
{
/* A Linked List is present at given index of Hash Table */
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;
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();
if (list == NULL)
{
}
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.
*/
}
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;
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;
}
if (array[index].tail == temp->next)
{
temp->next = NULL;
array[index].tail = temp;
}
else
{
temp->next = temp->next->next;
}
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");
}
}
}
void main()
{
int choice, key, value, n, c;
clrscr();
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:
break;
case 2:
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");
}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);
}
return newNode;
}
int main() {
struct node* root = createNode(1);
insertLeft(root, 2);
insertRight(root, 3);
insertLeft(root->left, 4);
//bitmap
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// bitmap.c
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// this should be in bitmap.h
#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;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// 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
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// this should be in bitmaptest.c
int main(void)
{
printf("sizeof(bitmap_type) = %zd\n", sizeof(bitmap_type));
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;
}
#include <stdio.h>
#include <stdlib.h>
enum nodeColor {
RED,
BLACK
};
struct rbNode {
int data, color;
struct rbNode *link[2];
};
// 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++;
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++;
// 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;
}