cl1p.net - The internet clipboard
Login/Sign Up
cl1p.net/code
cl1p.net/code
Login/Sign Up
This cl1p will be deleted in in 3 days.
Copy
BINARY SEARCH TREE #include
#include
struct node { int info; struct node *llink; struct node *rlink; }; typedef struct node *NODE; NODE getnode() { NODE x; x = (NODE)malloc(sizeof(struct node)); if (x == NULL) { printf("Insufficient memory"); exit(0); } return x; } void preorder(NODE root) { if (root == NULL) return; printf("%d\t", root->info); preorder(root->llink); preorder(root->rlink); } void postorder(NODE root) { if (root == NULL) return; postorder(root->llink); postorder(root->rlink); printf("%d\t", root->info); } void inorder(NODE root) { if (root == NULL) return; inorder(root->llink); printf("%d\t", root->info); inorder(root->rlink); } NODE insert(int item, NODE root) { NODE temp, cur, prev; temp = getnode(); temp->info = item; temp->llink = NULL; temp->rlink = NULL; if (root == NULL) return temp; prev = NULL; cur = root; while (cur != NULL) { prev = cur; if (item < cur->info) cur = cur->llink; else cur = cur->rlink; } if (item < prev->info) prev->llink = temp; else prev->rlink = temp; return root; } NODE search(int item, NODE root) { NODE cur = root; while (cur != NULL) { if (item == cur->info) return cur; if (item < cur->info) cur = cur->llink; else cur = cur->rlink; } return NULL; } int main() { int item, ch, n, i; NODE root = NULL, cur; while (1) { printf("\nEnter your choice:\n"); printf("1. Insert\n2. Preorder\n3. Inorder\n4. Postorder\n5. Search an Element\n6. Exit\n"); scanf("%d", &ch); switch (ch) { case 1: printf("Enter the number of items: "); scanf("%d", &n); printf("Enter the items to be inserted:\n"); for (i = 0; i < n; i++) { scanf("%d", &item); root = insert(item, root); } break; case 2: if (root == NULL) { printf("Tree is empty\n"); } else { printf("Preorder traversal is:\n"); preorder(root); printf("\n"); } break; case 3: if (root == NULL) { printf("Tree is empty\n"); } else { printf("Inorder traversal is:\n"); inorder(root); printf("\n"); } break; case 4: if (root == NULL) { printf("Tree is empty\n"); } else { printf("Postorder traversal is:\n"); postorder(root); printf("\n"); } break; case 5: printf("Enter the item to be searched: "); scanf("%d", &item); cur = search(item, root); if (cur == NULL) printf("Item not found\n"); else printf("Item found\n"); break; case 6: exit(0); default: printf("Enter a valid choice\n"); } } return 0; } ------------------------------------------------------------------------------------------------------------- BFS GRAPH #include
void bfs(int a[10][10], int n, int u) { int f, r, q[10], v; int s[10] = {0}; // Initialize all elements in s to 0, meaning no node is visited initially printf("The nodes visited from %d: ", u); f = 0; // Front of the queue r = -1; // Rear of the queue (initially empty) q[++r] = u; // Insert u into queue s[u] = 1; // Mark u as visited printf("%d ", u); // Print the starting node while (f <= r) { u = q[f++]; // Dequeue an element from queue for (v = 0; v < n; v++) { if (a[u][v] == 1 && s[v] == 0) { // If v is adjacent to u and not yet visited printf("%d ", v); // Print the node visited s[v] = 1; // Mark v as visited q[++r] = v; // Enqueue v } } } printf("\n"); } int main() { int n, a[10][10], source, i, j; printf("Enter the number of nodes: "); scanf("%d", &n); printf("Enter the adjacency matrix:\n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &a[i][j]); } } for (source = 0; source < n; source++) { bfs(a, n, source); } return 0; } ----------------------------------------------------------------------------------------------------------------- DFS GRAPH #include
int a[10][10], s[10], n; void read_AM(int a[10][10], int n) { int i, j; // Read the adjacency matrix for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &a[i][j]); } } } void dfs(int u) { int v; s[u] = 1; // Mark node u as visited printf("%d ", u); // Print the visited node for (v = 0; v < n; v++) { // If v is adjacent to u and not yet visited, recursively call dfs if (a[u][v] == 1 && s[v] == 0) { dfs(v); } } } int main() { int i, source; printf("Enter the number of nodes in the graph: "); scanf("%d", &n); printf("Enter the adjacency matrix:\n"); read_AM(a, n); for (source = 0; source < n; source++) { // Reset the visited array for each source node for (i = 0; i < n; i++) { s[i] = 0; } printf("The nodes reachable from %d: ", source); dfs(source); printf("\n"); } return 0; } ------------------------------------------------------------------------------------------------------------------------------ HASHING #include
#include
#include
#define HASH_SIZE 100 typedef struct employee { int id; char name[20]; } EMPLOYEE; /* Initialize hash table */ void initialize_hash_table(EMPLOYEE a[]) { int i; for (i = 0; i < HASH_SIZE; i++) { a[i].id = 0; } } /* Compute hash value using the function: H(K) = k % HASH_SIZE */ int H(int k) { return k % HASH_SIZE; } /* Insert an item into the empty slot using linear probing */ void insert_table(int id, char name[], EMPLOYEE a[]) { int i, index, h_value; h_value = H(id); for (i = 0; i < HASH_SIZE; i++) { index = (h_value + i) % HASH_SIZE; if (a[index].id == 0) { // Empty slot found a[index].id = id; // Insert employee id strcpy(a[index].name, name); // Insert employee name break; } } if (i == HASH_SIZE) { printf("Hash table full\n"); // No empty slot found } } /* Display the hash table */ void display_hash_table(EMPLOYEE a[], int n) { int i; printf("H_Value\t Emp_id\t Emp_name\n"); for (i = 0; i < HASH_SIZE; i++) { if (a[i].id != 0) { printf("%d\t %d\t %s\n", i, a[i].id, a[i].name); } } } int main() { EMPLOYEE a[HASH_SIZE]; char name[20]; int id, choice; initialize_hash_table(a); // Create initial hash table for (;;) { printf("1: Insert\n2: Display\n3: Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter employee ID: "); scanf("%d", &id); printf("Enter the name: "); scanf("%s", name); insert_table(id, name, a); break; case 2: printf("Contents of the hash table:\n"); display_hash_table(a, HASH_SIZE); printf("\n"); break; case 3: exit(0); default: printf("Enter a valid choice\n"); } } return 0; } Doubly linked list #include
#include
typedef struct student{int data;struct student *next, *prev;}NODE;NODE* getnode( ){NODE *x;x=(NODE*)malloc(sizeof(NODE));printf("\n Enter Data of Node to be Inserted: ");scanf("%d",&x->data);x->next=x->prev=NULL;return x;}NODE* insert_front(NODE* first){NODE *temp;if(first==NULL){temp=getnode();first=temp;}else{temp=getnode();temp->next=first;first->prev=temp;first=temp;}return first;}NODE* insert_left(NODE* first){NODE *temp,*cur,*pre;int data;if(first==NULL){temp=getnode();first=temp;}else{printf("Enter the node data to which left part new node to be inserted: ");scanf("%d",&data);temp=getnode();cur=first;while(cur->data!=data){pre=cur;cur=cur->next;}pre->next=temp;temp->prev=pre;temp->next=cur;cur->prev=temp;}return first;}NODE* delete_node(NODE* first){NODE *cur;int data;cur=first;printf("Enter the data of the NODE to be deleted: ");scanf("%d",&data);if(first==NULL){printf("\n List is empty\n");}else if(first->data==data){first=first->next;free(cur);}else{while(cur!=NULL){if(cur->data==data)break;cur=cur->next;}if(cur!=NULL){if(cur->next!=NULL){(cur->next)->prev=cur->prev;(cur->prev)->next=cur->next;free(cur);}else{(cur->prev)->next=NULL;free(cur);}}else{printf("No such node is present in the list\n");}}return first;}NODE* display(NODE* first){NODE *cur;if(first == NULL)printf("No nodes present\n");else{cur=first;while(cur!=NULL){printf("-->%d", cur->data);cur = cur->next;}}return first;}int main(){NODE *first;first=NULL;int ch;while(1){printf("\n1.InsertFront\t 2. InsertLeft\t 3.Delete\t 4.Display\t 5.exit\n");printf("Enter Your Choice: ");scanf("%d",&ch);switch(ch){case 1:first=insert_front(first);break;case 2:first=insert_left(first);break;case 3:first=delete_node(first);break;case 4:first=display(first);break;case 5:exit(0);break;default: printf("\n Invalid choice\n");break;}}return 0;}