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

DS New

Data structures and algorithms
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
13 views

DS New

Data structures and algorithms
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 13
include define MAX SIZE 180 // Maximum stock size int stock[MAX SIZE]; int top = -1; 17 Function to push an element onto the stock Void push(int x) { if (top == WAX SIZE - 2) { printf("Stack Overflow\n"); return; } stock[++top] = x; printt("%d pushed to stock\n". x); // Function to pop on element from the stock int popt) { Af (top == -1) { printf ("Stack Underflow\n"); return -1; } return stock{top--]; 17 Function to petk ot the top element pe return stock{top]; ¢/ Function to check if the stock is empty int isempty() ( return top == -1; 47 Function to display the elenents of the stock void Af (isEmety()) ( printf ("Stack is empty\n"); return; } printt("Stock elements: \n"); tor (int i = top: i >= 0; i--) [ printf(*xd\n", stock{i}); } int saint) { push( 18); push( 20); push{ 30); printt("Popped element: %d\n", pop()); print#(*Top element: Adin", peek()); e include Anelude uct Node ( int data; struct Nodet next; uct Node* top = MULL; // Initially, the stack 15 empty // Function to push an element onto the stack void push(int data). ( struct Node* newliade = (struct Node*)malloc (sizeot(struct N newode->data = data; newilode-onext = top; // Link the new node to the current t top = newNede; // Woke the new node the new top Printt("¥d pushed to etock\n", data); ) // Function to pop on element from the stock Ant popd) { At (top == MULL) ( printf ("Stock Under#low\n" return int data = top->dato; struct Nodet temp = top; top = top-mnext; // Move the top pointer to the next node free(temp); //'Free the menory of the popped node return data; ) 17 Function to peek at the top elenent int peok() { Af (top == NULL) { printe("stack is empty\n"); return -4; } Feturn top->dato; // Function to check if the stock is empty int Lstmpty() { i return top == NULL; /7 Function to display the elements of the stack void display() ( if (isEmpty()) { printf ("Stock is empty\n"); return; Struct Nodet temp = top; printf('Stock elements:\n"); white (temp |= NULL) { printe(md\n", tenp->dota); temp = temp->next; } int main() { push(10); pusn(20); push(30); printf(*Popped element: %d\n", popt)); printf ('Top element: d\n", peek()?; display(); return 0; demonstrating a queue implementation using an array: ¢ include fidefine MAX SIZE 10 // Maximum queue size int queue[{MAX SIZE] ; int front = -i, rear = -1; // Function to enqueue an elerent void enqueue(int x) { df (rear == MAX SIZE - 1) ( printf("Quelie Overflaw\n") ; return; } if (front front } reort+; queue[rear] = x; printf("%d enqueued to queue\n". x); } // Function to dequeue an element int dequeue() € if (front == -1 || front > rear) { printf ("Queue Underflow\n"); return -4; > ant x = queve[front]; fronte+; if (front > rear) { front = rear = -1 } return x; ; ff Reset queue if it becomes empty } // Funetion te display the queue elements void display() ( af (rear == -1) { printf("Gueue is empty\n" return; printf("Gueue elements: \n"); for (int 4 = front; i <= reor; i++) { printf("%d\n", queue[i]); } } int maing} { enqueue (10); enqueue ( 20) enqueue (30); Print#("Dequeved element: xd\n", dequeue()); display(); return @; #inelude #Hinelude struct Node ( int dota; struct Node! next; X struct Node* front = NULL; // Front of the qu ‘uct Node* rear = NULL; // Rear of the quet Y// Funetion to enqueue on element void ‘enqueue(int data) { struct Node® newtiode = (struct Node* )malloc(sizeot (struct Node)); newNode->data = data; newNode->next = NULL; if (rear == MULL) { // If the queve is empty front = rear = newNode; return; rear->next = newNode; // Append the nem node to the rear Teor = newNode; // Update the rear pointer printf("%d enqueued to queve\n", dota); ? 11 Function to dequeue an element Ant dequeue() { if (front == NULL) { printr ("Queue Underflow\n") ; return -1; 3 struct Node¥ temp = front; int data = temp-2data; front = front->next; // Move the front pointer to the next if (front == NULL) { _// If the queue becomes empty: rear = NULL; // Reset the rear pointer ) free(temp); // Free the memory of the dequeved node return dota; d ‘4/1 Function to display the queve elenents void disploy() ( if (front == NULL) { printf ("Queue 18 empty\n"); return; struct Node* temp = front; printt("Queue elenents:\n"); while (temp t= NULL) ( printf("Xd\n", temp->data); temp = temp->next; d int main() { queue weing lined bat v enqueve( 32); printt("Dequeued element: Yd\n", dequeve()); display(); Sabie “* Here's aC program implementing binary search, accompanied by visual explanations: C code: c #inelude int binarySeerch(int arr[], int low, int high, int key) ( while (low <= high) { int mid = low + (high - low) / 2; // Prevent overflow // Visualizing the mid-point calculation i [..slow...mid.. high... */ if (orr[mid] == key) { return mid; // Element found } else if (orr{mid] < key) { low = mid + #/ Search in the right suborroy } else { high = mid - 1; // Search in the left subarray ) } return -2; /f Element not found int main() { int orr{] = {2, 3, 4, 18, 40}; 1 int n= sizeof(orr) / sizeat(arr[@]); int key = 10; 2 int result = binarySeorch(arr, @; n- 1, key); df (result == -1) { printf("Element is not present in array\n"); } else { printt("Element is present ot index d\n", result); return Linear search in C #include int linearSearch(int arr], int n, int x) { i void bubbleSort(int arr[], int n) { int i,j, temp; for (i= 0; i arr[j + 1]) { // Swap elements temp = arr{[j]; arr{j] = arrfj + 1]; arr[j + 1] = temp; } | } } int main() { int arr[] = (64, 34, 25, 12, 22, 11, 90}; int n = sizeof (arr) / sizeof (arr[0]); printf("Unsorted array: \n"); for (int i= 0; i void selectionSort(int arr[], int n) { inti, j, minIndex, temp; for (i= 0;i void insertionSort(int arr[], int n) { inti, key, j; // Move elements of arr(0..i-1], that are // greater than key, to one position ahead // of their current position while (j >= 0 && arr[j] > key) { arr[j +1] = arr{j]; jrj-l ) arr[j +1] = key; } } int main() { int arr[] = (12, 11, 13, 5, 6}; int n = sizeof(arr) / sizeof(arr[0}); printf("Unsorted array: \n"); for (int i=0;i < njit+) { printf("%od ", arr[i}); ) insertionSort(arr, n); printf("\nSorted array: \n"); for (int i=0;i < nj i++) [ printf("Yod ", arr[i}); ) return 0; c #include Hdefine MAX NODES 1¢0 char tree[MAX NODES]; // Array to store tree elements int root = @ // Index of the root node /f Function to create a new node void createNode(char dato, int porentIndex) { if (root == -1) { // If the tree is empty root = 0; } int childIndex = 2 * parentIndex + 1; // Calculate child i if (childIndex »= MAX_NODES) { printf ("Tree Overflow\n"); return; tree[childindex] = doto; } éf Function to traverse the tree in preorder void preorderTraversol(int index) { if (index < MAX_NODES && tree[index] I= "\@") { printf("%e *, tree[index]}; preorderTraversol(2 * index + 1); // Wisit left subtre preorderTraversal(2 * index + 2); // Visit right subtr ) int maint) { creoteNode('A', ; // Create the root node creoteNode('B", f/f Greate children of the root creoteNode(‘C', 0); creoteNode('D', 1); // Create children of node ‘B' ereoteNode('E*, 1); printf ("Preorder traversal: “); preorderTraversol(root); printt("\n"); return @; demonstrates the implementation of a binary tree using a linked list: e finelude winelude // Structure to represent mode in the binory tree struct Node { int data; struct Node “lett; struct Node *right; 7/ Function to create a new node Struct Node* newNiode(int dota) { struct Node node = (struct Node*)malloc(sizeot(struct Node)); node->data = dato; node-sleft = NULL node->right = NULL; return node; } // Funetion to perform inorder traversal void inorderTraversol(struct Node* root) Gf (root t= NULL) { inorderTraversal(root->Lett); printf("%d ", root->data); inorderTraversal(root-»right) ; } int main() { /( Greate @ sample binary tree struct Node* root = newNode(19); root-rleft = newWode(s); root->right = newNode (15); root-oleft-sleft = newode(2); root->left->right = newNode(T); printf(“Inorder traversal: “); inorder Traversal (root ); printt("\n"); return @; Breadth first search in C Hinclude #define MAX_VERTICES 100 int graph[MAX_VERTICES][MAX_VERTICES]; int visited[MAX_ VERTICES]; void BFS(int startVertex, int n) { int queue[MAX_VERTICES}, front /fEnqueue the starting vertex reart+; queue|rear] = startVertex; visited|[startVertex] = 1; print{("BES traversal: "); while (front != rear) { // Dequeue a vertex from the queue front++; int vertex = queue|front]; printf("ad ", vertex); // Enqueue all unvisited adjacent vertices for (int i=0; i include #define MAX_VERTICES 100 int graph[MAX_VERTICES][MAX_ VERTICES]; int visited[MAX_VERTICES]; void DFS(int vertex, int n) { printf("%d ", vertex); visited|[vertex] = 1; for (int i = 0;i

You might also like