Exercise 1-6 (1)
Exercise 1-6 (1)
int main() {
int n;
int arr[100];
// Ask the user for the number of elements
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
return 0;
}
C Programs to implement the Searching Techniques – Linear & Binary Search
#include <stdio.h>
int main() {
int n, value, position;
if (position == -1) {
printf("Value not found in the array.\n");
} else {
printf("Value found at position: %d\n", position + 1); // Position is index+1 for user-friendly output
}
return 0;
}
Binary Search
#include <stdio.h>
// Function to perform binary search on a sorted array
int binarySearch(int arr[], int l, int r, int x) {
while (l <= r) {
int m = l + (r - l) / 2;
int main() {
int arr[50], n, x, result;
// User input for array size
printf("Enter number of elements in the array: ");
scanf("%d", &n);
// Output result
if (result == -1)
printf("Element is not present in the array.");
else
printf("Element is present at index %d.", result);
return 0;
}
Bubble Sort
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1]) {
// swap temp and arr[i]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
int main() {
int array[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
bubbleSort(array, n);
return 0;
}
Selection Sort
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, min_idx, temp;
int main() {
int arr[100], n, i;
return 0;
}
Insertion Sort
#include <stdio.h>
int main() {
int n, i;
return 0;
}
Exercise 2: Linked List Implementation
i) Implement a singly linked list and perform insertion and deletion operations.
#include <stdio.h>
#include <stdlib.h>
// Function to delete a node with given key from the linked list
void deleteNode(struct Node** head_ref, int key) {
// Store head node
struct Node* temp = *head_ref, *prev;
// Search for the key to be deleted, keep track of the previous node as we need to change
'prev->next'
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
// Main function
int main() {
struct Node* head = NULL;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* head = NULL;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* head = NULL;
insert(&head, 5);
insert(&head, 4);
insert(&head, 3);
insert(&head, 2);
insert(&head, 1);
head = reverse(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure for a node in the linked list representing a digit
struct Node {
int digit;
struct Node* next;
};
insert(&result, sum);
}
return result;
}
// Main function
int main() {
struct Node* num1 = NULL;
struct Node* num2 = NULL;
printf("Sum: ");
printList(sum);
printf("\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* head = NULL;
// Insert elements into the linked list
insert(&head, 5);
insert(&head, 4);
insert(&head, 4);
insert(&head, 2);
insert(&head, 2);
insert(&head, 1);
// Remove duplicates
removeDuplicates(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// If there are any remaining terms in the first polynomial, add them to the result
while (poly1 != NULL) {
insertTerm(&result, poly1->coefficient, poly1->exponent);
poly1 = poly1->next;
}
// If there are any remaining terms in the second polynomial, add them to the result
while (poly2 != NULL) {
insertTerm(&result, poly2->coefficient, poly2->exponent);
poly2 = poly2->next;
}
return result;
}
// Main function
int main() {
struct Term* poly1 = NULL;
struct Term* poly2 = NULL;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Deque* deque = createDeque();
insertFront(deque, 1);
insertFront(deque, 2);
insertRear(deque, 3);
insertRear(deque, 4);
printDeque(deque);
deleteFront(deque);
deleteRear(deque);
printDeque(deque);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
int main() {
struct Node* head = NULL;
printf("Insert 1 at the front\n");
insertAtFront(&head, 1);
traverse(head);
printf("Insert 2 at the front\n");
insertAtFront(&head, 2);
traverse(head);
printf("Insert 4 at the end\n");
insertAtEnd(&head, 4);
traverse(head);
printf("Insert 5 at the front\n");
insertAtEnd(&head, 5);
traverse(head);
printf("Insert 3 after head->next\n");
insertAfterNode(head->next, 3);
traverse(head);
printf("Insert -1 before the head\n");
insertBeforeNode(&head, head, -1);
traverse(head);
printf("Insert 100 before head->next->next\n");
insertBeforeNode(&head, head->next->next, 100);
traverse(head);
printf("Delete the front node.\n");
deleteAtFront(&head);
traverse(head);
printf("Delete the last node\n");
deleteAtLast(&head);
traverse(head);
printf("Delete the second node from the front.\n");
deleteAtPosition(&head, 2);
traverse(head);
return 0;
}
ii) Implement a circular linked list and perform insertion, deletion, and traversal.
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node *next;
};
new_node->data = data;
new_node->next = *head_ref;
// Search for the key to be deleted, keep track of the previous node
do {
if (temp->data == key)
break;
prev = temp;
temp = temp->next;
} while (temp != *head_ref);
int main() {
struct Node *head = NULL;
// Delete a node
deleteNode(&head, 3);
printf("Circular Linked List after deleting node with value 3: ");
display(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Stack stack;
initializeStack(&stack);
return 0;
}
ii) Implement a stack using linked lists.
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* root = NULL;
push(&root, 10);
push(&root, 20);
push(&root, 30);
printf("%d popped from stack\n", pop(&root));
printf("Top element is %d\n", peek(root));
return 0;
}
iii) Write a program to evaluate a postfix expression using a stack.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// Stack structure
struct Stack {
int top;
int items[MAX_STACK_SIZE];
};
// Function to initialize a stack
void initializeStack(struct Stack *s) {
s->top = -1;
}
int main() {
char expression[100];
printf("Enter a postfix expression: ");
scanf("%s", expression);
int result = evaluatePostfix(expression);
printf("Result of evaluation: %d\n", result);
return 0;
}
int main() {
char expression[100];
printf("Enter an expression: ");
scanf("%s", expression);
if (areParenthesesBalanced(expression)) {
printf("Parentheses are balanced\n");
} else {
printf("Parentheses are not balanced\n");
}
return 0;
}
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
ii) Implement a queue using arrays and linked lists.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n");
printf("\n=================================================================
\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;
#include <stdio.h>
#include <stdlib.h>
// Queue structure
struct Queue {
int front, rear, size;
unsigned capacity;
struct PrintJob* array;
};
int main() {
struct Queue* printQueue = createQueue(MAX_QUEUE_SIZE);
// Enqueue print jobs with different priorities
struct PrintJob job1 = {1, 3};
struct PrintJob job2 = {2, 1};
struct PrintJob job3 = {3, 2};
struct PrintJob job4 = {4, 2};
enQueue(printQueue, job1);
enQueue(printQueue, job2);
enQueue(printQueue, job3);
enQueue(printQueue, job4);
return 0;
}
iii) Solve problems involving circular queues.
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 5
int main() {
struct CircularQueue* ticketCounterQueue = createCircularQueue(MAX_QUEUE_SIZE);
return 0;
}