0% found this document useful (0 votes)
9 views28 pages

Asd

The document contains multiple C programs demonstrating various operations on linked lists, including insertion, deletion, and display for both singly and doubly linked lists. Key functions include insertion sort, quicksort, and methods for manipulating linked lists such as inserting and deleting nodes at specific locations. Each program includes a main function that tests these operations with sample data.

Uploaded by

Danish S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views28 pages

Asd

The document contains multiple C programs demonstrating various operations on linked lists, including insertion, deletion, and display for both singly and doubly linked lists. Key functions include insertion sort, quicksort, and methods for manipulating linked lists such as inserting and deleting nodes at specific locations. Each program includes a main function that tests these operations with sample data.

Uploaded by

Danish S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 28

sorting \\

#include <stdio.h>

// Function to perform insertion sort


void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

// 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];
j = j - 1;
}
arr[j + 1] = key;
}
}

// Function to perform quicksort


void quickSort(int arr[], int low, int high) {
if (low < high) {
// Partition the array
int pi = partition(arr, low, high);

// Recursively sort the left and right subarrays


quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

// Function to select the pivot element and partition the array


int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high - 1; j++) {


// If the current element is smaller than or equal to the pivot
if (arr[j] <= pivot) {
i++;
// Swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap arr[i + 1] and arr[high] (pivot)
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;

return (i + 1);
}

// Function to print the array


void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver code
int main() {
int arr[] = {9, 2, 5, 1, 7, 6, 8};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


printArray(arr, n);

// Perform insertion sort


insertionSort(arr, n);
printf("Array sorted using Insertion Sort: ");
printArray(arr, n);

// Perform quicksort
quickSort(arr, 0, n - 1);
printf("Array sorted using Quicksort: ");
printArray(arr, n);

return 0;
}

LL display contents \\
#include <stdio.h>
#include <stdlib.h>

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

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at the beginning of the linked list


void insertNode(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}

// Function to display the contents of the linked list


void displayLinkedList(struct Node* head) {
struct Node* current = head;
if (current == NULL) {
printf("Linked list is empty.\n");
return;
}
printf("Contents of the linked list: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

// Driver code
int main() {
struct Node* head = NULL;

// Insert nodes
insertNode(&head, 4);
insertNode(&head, 7);
insertNode(&head, 2);
insertNode(&head, 9);

// Display linked list


displayLinkedList(head);

return 0;
}

insert anywhere in LL \\
#include <stdio.h>
#include <stdlib.h>

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

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at the beginning of the linked list


void insertNodeAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}

// Function to insert a node at the end of the linked list


void insertNodeAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}

// Function to insert a node at a given location in the linked list


void insertNodeAtLocation(struct Node** head, int data, int location) {
if (location == 0) {
insertNodeAtBeginning(head, data);
return;
}
struct Node* newNode = createNode(data);
struct Node* current = *head;
int count = 0;
while (count < location - 1 && current != NULL) {
current = current->next;
count++;
}
if (current == NULL) {
printf("Invalid location. Insertion failed.\n");
return;
}
newNode->next = current->next;
current->next = newNode;
}

// Function to display the contents of the linked list


void displayLinkedList(struct Node* head) {
struct Node* current = head;
if (current == NULL) {
printf("Linked list is empty.\n");
return;
}
printf("Contents of the linked list: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

// Driver code
int main() {
struct Node* head = NULL;

// Insert node at the beginning


insertNodeAtBeginning(&head, 4);

// Insert node at the end


insertNodeAtEnd(&head, 7);

// Insert node at a given location


insertNodeAtLocation(&head, 2, 1);
insertNodeAtLocation(&head, 9, 2);

// Display linked list


displayLinkedList(head);

return 0;
}

delete anywhere in LL \\
#include <stdio.h>
#include <stdlib.h>

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

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at the beginning of the linked list


void insertNodeAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}

// Function to delete the first node of the linked list


void deleteNodeAtBeginning(struct Node** head) {
if (*head == NULL) {
printf("Linked list is empty. Deletion failed.\n");
return;
}
struct Node* temp = *head;
*head = (*head)->next;
free(temp);
}

// Function to delete the last node of the linked list


void deleteNodeAtEnd(struct Node** head) {
if (*head == NULL) {
printf("Linked list is empty. Deletion failed.\n");
return;
}
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
struct Node* current = *head;
struct Node* previous = NULL;
while (current->next != NULL) {
previous = current;
current = current->next;
}
previous->next = NULL;
free(current);
}

// Function to delete a node at a given location in the linked list


void deleteNodeAtLocation(struct Node** head, int location) {
if (*head == NULL) {
printf("Linked list is empty. Deletion failed.\n");
return;
}
if (location == 0) {
deleteNodeAtBeginning(head);
return;
}
struct Node* current = *head;
struct Node* previous = NULL;
int count = 0;
while (count < location && current != NULL) {
previous = current;
current = current->next;
count++;
}
if (current == NULL) {
printf("Invalid location. Deletion failed.\n");
return;
}
previous->next = current->next;
free(current);
}

// Function to display the contents of the linked list


void displayLinkedList(struct Node* head) {
struct Node* current = head;
if (current == NULL) {
printf("Linked list is empty.\n");
return;
}
printf("Contents of the linked list: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

// Driver code
int main() {
struct Node* head = NULL;

// Insert nodes
insertNodeAtBeginning(&head, 4);
insertNodeAtBeginning(&head, 7);
insertNodeAtBeginning(&head, 2);
insertNodeAtBeginning(&head, 9);

// Display linked list


displayLinkedList(head);

// Delete node at the beginning


deleteNodeAtBeginning(&head);
printf("Linked list after deleting node at the beginning: ");
displayLinkedList(head);

// Delete node at the end


deleteNodeAtEnd(&head);
printf("Linked list after deleting node at the end: ");
displayLinkedList(head);

// Delete node at a given location


deleteNodeAtLocation(&head, 1);
printf("Linked list after deleting node at location 1: ");
displayLinkedList(head);

return 0;
}

display for double LL \\


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

// Node structure
struct Node {
int data;
struct Node* prev;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at the end of the doubly linked list


void insertNodeAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}

// Function to display the contents of the doubly linked list


void displayDoublyLinkedList(struct Node* head) {
struct Node* current = head;
if (current == NULL) {
printf("Doubly linked list is empty.\n");
return;
}
printf("Contents of the doubly linked list: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// Driver code
int main() {
struct Node* head = NULL;

// Insert nodes
insertNodeAtEnd(&head, 4);
insertNodeAtEnd(&head, 7);
insertNodeAtEnd(&head, 2);
insertNodeAtEnd(&head, 9);

// Display doubly linked list


displayDoublyLinkedList(head);

return 0;
}

delete and insert in double LL \\


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

// Node structure
struct Node {
int data;
struct Node* prev;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

// Function to insert a node at the beginning of the doubly linked list


void insertNodeAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}

// Function to insert a node at the end of the doubly linked list


void insertNodeAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}

// Function to insert a node at a given location in the doubly linked list


void insertNodeAtLocation(struct Node** head, int data, int location) {
if (location <= 0) {
insertNodeAtBeginning(head, data);
return;
}
struct Node* newNode = createNode(data);
struct Node* current = *head;
int count = 0;
while (count < location - 1 && current != NULL) {
current = current->next;
count++;
}
if (current == NULL) {
printf("Invalid location. Insertion failed.\n");
return;
}
newNode->next = current->next;
if (current->next != NULL) {
current->next->prev = newNode;
}
current->next = newNode;
newNode->prev = current;
}

// Function to delete the first node of the doubly linked list


void deleteNodeAtBeginning(struct Node** head) {
if (*head == NULL) {
printf("Doubly linked list is empty. Deletion failed.\n");
return;
}
struct Node* temp = *head;
*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
}

// Function to delete the last node of the doubly linked list


void deleteNodeAtEnd(struct Node** head) {
if (*head == NULL) {
printf("Doubly linked list is empty. Deletion failed.\n");
return;
}
if ((*head)->next == NULL) {
free(*head);
*head = NULL;
return;
}
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->prev->next = NULL;
free(current);
}

// Function to delete a node at a given location in the doubly linked list


void deleteNodeAtLocation(struct Node** head, int location) {
if (*head == NULL) {
printf("Doubly linked list is empty. Deletion failed.\n");
return;
}
if (location <= 0) {
deleteNodeAtBeginning(head);
return;
}
struct Node* current = *head;
int count = 0;
while (count < location && current != NULL) {
current = current->next;
count++;
}
if (current == NULL) {
printf("Invalid location. Deletion failed.\n");
return;
}
current->prev->next = current->next;
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
}

// Function to display the contents of the doubly linked list


void displayDoublyLinkedList(struct Node* head) {
struct Node* current = head;
if (current == NULL) {
printf("Doubly linked list is empty.\n");
return;
}
printf("Contents of the doubly linked list: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

// Driver code
int main() {
struct Node* head = NULL;

// Insert nodes
insertNodeAtEnd(&head, 4);
insertNodeAtBeginning(&head, 7);
insertNodeAtEnd(&head, 2);
insertNodeAtLocation(&head, 9, 1);

// Display doubly linked list


displayDoublyLinkedList(head);
// Delete node at the beginning
deleteNodeAtBeginning(&head);
printf("Doubly linked list after deleting node at the beginning: ");
displayDoublyLinkedList(head);

// Delete node at the end


deleteNodeAtEnd(&head);
printf("Doubly linked list after deleting node at the end: ");
displayDoublyLinkedList(head);

// Delete node at a given location


deleteNodeAtLocation(&head, 1);
printf("Doubly linked list after deleting node at location 1: ");
displayDoublyLinkedList(head);

return 0;
}

applictn for polynomial represtn, +, * \\


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

// Structure to represent a term in the polynomial


struct Term {
int coefficient;
int exponent;
struct Term* next;
};

// Function to create a new term


struct Term* createTerm(int coefficient, int exponent) {
struct Term* newTerm = (struct Term*)malloc(sizeof(struct Term));
newTerm->coefficient = coefficient;
newTerm->exponent = exponent;
newTerm->next = NULL;
return newTerm;
}

// Function to insert a term at the end of the polynomial


void insertTerm(struct Term** poly, int coefficient, int exponent) {
struct Term* newTerm = createTerm(coefficient, exponent);
if (*poly == NULL) {
*poly = newTerm;
} else {
struct Term* current = *poly;
while (current->next != NULL) {
current = current->next;
}
current->next = newTerm;
}
}

// Function to display the polynomial


void displayPolynomial(struct Term* poly) {
if (poly == NULL) {
printf("Polynomial is empty.\n");
return;
}
struct Term* current = poly;
while (current != NULL) {
printf("%dx^%d ", current->coefficient, current->exponent);
if (current->next != NULL) {
printf("+ ");
}
current = current->next;
}
printf("\n");
}

// Function to add two polynomials


struct Term* addPolynomials(struct Term* poly1, struct Term* poly2) {
struct Term* result = NULL;
struct Term* current1 = poly1;
struct Term* current2 = poly2;
while (current1 != NULL && current2 != NULL) {
if (current1->exponent > current2->exponent) {
insertTerm(&result, current1->coefficient, current1->exponent);
current1 = current1->next;
} else if (current1->exponent < current2->exponent) {
insertTerm(&result, current2->coefficient, current2->exponent);
current2 = current2->next;
} else {
int sum = current1->coefficient + current2->coefficient;
insertTerm(&result, sum, current1->exponent);
current1 = current1->next;
current2 = current2->next;
}
}
while (current1 != NULL) {
insertTerm(&result, current1->coefficient, current1->exponent);
current1 = current1->next;
}
while (current2 != NULL) {
insertTerm(&result, current2->coefficient, current2->exponent);
current2 = current2->next;
}
return result;
}

// Function to multiply two polynomials


struct Term* multiplyPolynomials(struct Term* poly1, struct Term* poly2) {
struct Term* result = NULL;
struct Term* current1 = poly1;
while (current1 != NULL) {
struct Term* current2 = poly2;
while (current2 != NULL) {
int coefficient = current1->coefficient * current2->coefficient;
int exponent = current1->exponent + current2->exponent;
struct Term* existingTerm = result;
while (existingTerm != NULL) {
if (existingTerm->exponent == exponent) {
existingTerm->coefficient += coefficient;
break;
}
existingTerm = existingTerm->next;
}
if (existingTerm == NULL) {
insertTerm(&result, coefficient, exponent);
}
current2 = current2->next;
}
current1 = current1->next;
}
return result;
}

// Driver code
int main() {
struct Term* poly1 = NULL;
struct Term* poly2 = NULL;

// Insert terms into polynomial 1


insertTerm(&poly1, 3, 2);
insertTerm(&poly1, 4, 1);
insertTerm(&poly1, 2, 0);

// Insert terms into polynomial 2


insertTerm(&poly2, 2, 3);
insertTerm(&poly2, 1, 2);
insertTerm(&poly2, 5, 0);

printf("Polynomial 1: ");
displayPolynomial(poly1);

printf("Polynomial 2: ");
displayPolynomial(poly2);

struct Term* sum = addPolynomials(poly1, poly2);


printf("Sum of the polynomials: ");
displayPolynomial(sum);

struct Term* product = multiplyPolynomials(poly1, poly2);


printf("Product of the polynomials: ");
displayPolynomial(product);

return 0;
}

stack static memory allocatn \\


#include <stdio.h>
#define MAX_SIZE 100

// Structure to represent a stack


struct Stack {
int arr[MAX_SIZE];
int top;
};

// Function to initialize the stack


void initializeStack(struct Stack* stack) {
stack->top = -1;
}

// Function to check if the stack is empty


int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
// Function to check if the stack is full
int isFull(struct Stack* stack) {
return stack->top == MAX_SIZE - 1;
}

// Function to push an element onto the stack


void push(struct Stack* stack, int data) {
if (isFull(stack)) {
printf("Stack overflow. Cannot push element %d.\n", data);
return;
}
stack->top++;
stack->arr[stack->top] = data;
printf("Pushed element %d onto the stack.\n", data);
}

// Function to pop an element from the stack


int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow. Cannot pop element.\n");
return -1;
}
int data = stack->arr[stack->top];
stack->top--;
printf("Popped element %d from the stack.\n", data);
return data;
}

// Function to get the top element of the stack


int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty.\n");
return -1;
}
return stack->arr[stack->top];
}

// Function to display the contents of the stack


void displayStack(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty.\n");
return;
}
printf("Contents of the stack: ");
for (int i = stack->top; i >= 0; i--) {
printf("%d ", stack->arr[i]);
}
printf("\n");
}

// Driver code
int main() {
struct Stack stack;
initializeStack(&stack);

push(&stack, 5);
push(&stack, 10);
push(&stack, 15);
displayStack(&stack);

pop(&stack);
displayStack(&stack);

int topElement = peek(&stack);


printf("Top element of the stack: %d\n", topElement);

return 0;
}

stack dynamic allocatn \\


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

// Structure to represent a stack


struct Stack {
int* arr;
int top;
int capacity;
};

// Function to create a stack


struct Stack* createStack(int capacity) {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->arr = (int*)malloc(capacity * sizeof(int));
stack->top = -1;
stack->capacity = capacity;
return stack;
}

// Function to check if the stack is empty


int isEmpty(struct Stack* stack) {
return stack->top == -1;
}

// Function to check if the stack is full


int isFull(struct Stack* stack) {
return stack->top == stack->capacity - 1;
}

// Function to push an element onto the stack


void push(struct Stack* stack, int data) {
if (isFull(stack)) {
printf("Stack overflow. Cannot push element %d.\n", data);
return;
}
stack->top++;
stack->arr[stack->top] = data;
printf("Pushed element %d onto the stack.\n", data);
}

// Function to pop an element from the stack


int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow. Cannot pop element.\n");
return -1;
}
int data = stack->arr[stack->top];
stack->top--;
printf("Popped element %d from the stack.\n", data);
return data;
}

// Function to get the top element of the stack


int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty.\n");
return -1;
}
return stack->arr[stack->top];
}

// Function to display the contents of the stack


void displayStack(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty.\n");
return;
}
printf("Contents of the stack: ");
for (int i = stack->top; i >= 0; i--) {
printf("%d ", stack->arr[i]);
}
printf("\n");
}

// Function to deallocate memory used by the stack


void destroyStack(struct Stack* stack) {
free(stack->arr);
free(stack);
}

// Driver code
int main() {
struct Stack* stack = createStack(100);

push(stack, 5);
push(stack, 10);
push(stack, 15);
displayStack(stack);

pop(stack);
displayStack(stack);

int topElement = peek(stack);


printf("Top element of the stack: %d\n", topElement);

destroyStack(stack);

return 0;
}

queues static \\
#include <stdio.h>
#define MAX_SIZE 100

// Structure to represent a queue


struct Queue {
int arr[MAX_SIZE];
int front;
int rear;
};

// Function to initialize the queue


void initializeQueue(struct Queue* queue) {
queue->front = -1;
queue->rear = -1;
}

// Function to check if the queue is empty


int isEmpty(struct Queue* queue) {
return queue->front == -1;
}

// Function to check if the queue is full


int isFull(struct Queue* queue) {
return (queue->rear + 1) % MAX_SIZE == queue->front;
}

// Function to enqueue an element


void enqueue(struct Queue* queue, int data) {
if (isFull(queue)) {
printf("Queue overflow. Cannot enqueue element %d.\n", data);
return;
}
if (isEmpty(queue)) {
queue->front = 0;
}
queue->rear = (queue->rear + 1) % MAX_SIZE;
queue->arr[queue->rear] = data;
printf("Enqueued element %d into the queue.\n", data);
}

// Function to dequeue an element


int dequeue(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue underflow. Cannot dequeue element.\n");
return -1;
}
int data = queue->arr[queue->front];
if (queue->front == queue->rear) {
initializeQueue(queue);
} else {
queue->front = (queue->front + 1) % MAX_SIZE;
}
printf("Dequeued element %d from the queue.\n", data);
return data;
}

// Function to get the front element of the queue


int front(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return -1;
}
return queue->arr[queue->front];
}
// Function to display the contents of the queue
void displayQueue(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return;
}
printf("Contents of the queue: ");
int i = queue->front;
while (i != queue->rear) {
printf("%d ", queue->arr[i]);
i = (i + 1) % MAX_SIZE;
}
printf("%d\n", queue->arr[i]);
}

// Driver code
int main() {
struct Queue queue;
initializeQueue(&queue);

enqueue(&queue, 5);
enqueue(&queue, 10);
enqueue(&queue, 15);
displayQueue(&queue);

dequeue(&queue);
displayQueue(&queue);

int frontElement = front(&queue);


printf("Front element of the queue: %d\n", frontElement);

return 0;
}

queues dynamic \\
#include <stdio.h>
#include <stdlib.h>

// Structure to represent a queue node


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

// Structure to represent a queue


struct Queue {
struct Node* front;
struct Node* rear;
};

// Function to initialize the queue


void initializeQueue(struct Queue* queue) {
queue->front = NULL;
queue->rear = NULL;
}

// Function to check if the queue is empty


int isEmpty(struct Queue* queue) {
return queue->front == NULL;
}

// Function to enqueue an element


void enqueue(struct Queue* queue, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;

if (isEmpty(queue)) {
queue->front = newNode;
queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}

printf("Enqueued element %d into the queue.\n", data);


}

// Function to dequeue an element


int dequeue(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue underflow. Cannot dequeue element.\n");
return -1;
}

struct Node* temp = queue->front;


int data = temp->data;
queue->front = queue->front->next;
free(temp);

if (queue->front == NULL) {
queue->rear = NULL;
}

printf("Dequeued element %d from the queue.\n", data);


return data;
}

// Function to get the front element of the queue


int front(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return -1;
}
return queue->front->data;
}

// Function to display the contents of the queue


void displayQueue(struct Queue* queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return;
}
printf("Contents of the queue: ");
struct Node* current = queue->front;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

// Function to deallocate memory used by the queue


void destroyQueue(struct Queue* queue) {
struct Node* current = queue->front;
while (current != NULL) {
struct Node* temp = current;
current = current->next;
free(temp);
}
queue->front = NULL;
queue->rear = NULL;
}

// Driver code
int main() {
struct Queue queue;
initializeQueue(&queue);

enqueue(&queue, 5);
enqueue(&queue, 10);
enqueue(&queue, 15);
displayQueue(&queue);

dequeue(&queue);
displayQueue(&queue);

int frontElement = front(&queue);


printf("Front element of the queue: %d\n", frontElement);

destroyQueue(&queue);

return 0;
}

trees traversals \\
1st\\
#include <stdio.h>
#include <stdlib.h>

// Structure for a binary tree node


struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Inorder traversal (Left-Root-Right)
void inorderTraversal(struct Node* root) {
if (root == NULL)
return;

inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}

// Preorder traversal (Root-Left-Right)


void preorderTraversal(struct Node* root) {
if (root == NULL)
return;

printf("%d ", root->data);


preorderTraversal(root->left);
preorderTraversal(root->right);
}

// Postorder traversal (Left-Right-Root)


void postorderTraversal(struct Node* root) {
if (root == NULL)
return;

postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}

// Driver code
int main() {
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);

printf("Inorder traversal: ");


inorderTraversal(root);
printf("\n");

printf("Preorder traversal: ");


preorderTraversal(root);
printf("\n");

printf("Postorder traversal: ");


postorderTraversal(root);
printf("\n");

return 0;
}

2nd\\
#include <stdio.h>
#include <stdlib.h>

// Structure for a graph node


struct Node {
int data;
struct Node** neighbors;
int numNeighbors;
int visited;
};

// Function to create a new graph node


struct Node* createNode(int data, int numNeighbors) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->numNeighbors = numNeighbors;
newNode->visited = 0;

// Allocate memory for neighbors


newNode->neighbors = (struct Node**)malloc(numNeighbors * sizeof(struct
Node*));
for (int i = 0; i < numNeighbors; i++) {
newNode->neighbors[i] = NULL;
}

return newNode;
}

// Function to add an edge between two nodes in the graph


void addEdge(struct Node* node1, struct Node* node2) {
int numNeighbors1 = node1->numNeighbors;
int numNeighbors2 = node2->numNeighbors;

// Resize the neighbors arrays


node1->neighbors = (struct Node**)realloc(node1->neighbors, (numNeighbors1 + 1)
* sizeof(struct Node*));
node2->neighbors = (struct Node**)realloc(node2->neighbors, (numNeighbors2 + 1)
* sizeof(struct Node*));

// Add the edges


node1->neighbors[numNeighbors1] = node2;
node2->neighbors[numNeighbors2] = node1;

// Update the number of neighbors


node1->numNeighbors++;
node2->numNeighbors++;
}

// Breadth-First Search
void BFS(struct Node* start) {
struct Node* queue[100];
int front = 0;
int rear = -1;

// Enqueue the start node


queue[++rear] = start;
start->visited = 1;

printf("Breadth-First Search: ");


while (front <= rear) {
struct Node* current = queue[front++];
printf("%d ", current->data);

// Enqueue unvisited neighbors


for (int i = 0; i < current->numNeighbors; i++) {
struct Node* neighbor = current->neighbors[i];
if (!neighbor->visited) {
queue[++rear] = neighbor;
neighbor->visited = 1;
}
}
}
printf("\n");
}

// Depth-First Search (Helper function)


void DFSHelper(struct Node* node) {
node->visited = 1;
printf("%d ", node->data);

for (int i = 0; i < node->numNeighbors; i++) {


struct Node* neighbor = node->neighbors[i];
if (!neighbor->visited) {
DFSHelper(neighbor);
}
}
}

// Depth-First Search
void DFS(struct Node* start) {
printf("Depth-First Search: ");
DFSHelper(start);
printf("\n");
}

// Driver code
int main() {
struct Node* node1 = createNode(1, 3);
struct Node* node2 = createNode(2, 2);
struct Node* node3 = createNode(3, 2);
struct Node* node4 = createNode(4, 1);
struct Node* node5 = createNode(5, 1);

addEdge(node1, node2);
addEdge(node1, node3);
addEdge(node1, node4);
addEdge(node2, node5);

BFS(node1);
DFS(node1);

return 0;
}

avl tree \\
#include <stdio.h>
#include <stdlib.h>

// Structure for a node in AVL tree


struct Node {
int data;
struct Node* left;
struct Node* right;
int height;
};

// Function to calculate the height of a node


int height(struct Node* node) {
if (node == NULL)
return 0;
return node->height;
}

// Function to calculate the maximum of two integers


int max(int a, int b) {
return (a > b) ? a : b;
}

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
newNode->height = 1;
return newNode;
}

// Function to right rotate a subtree rooted with y


struct Node* rightRotate(struct Node* y) {
struct Node* x = y->left;
struct Node* T2 = x->right;

// Perform rotation
x->right = y;
y->left = T2;

// Update heights
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;

// Return the new root


return x;
}

// Function to left rotate a subtree rooted with x


struct Node* leftRotate(struct Node* x) {
struct Node* y = x->right;
struct Node* T2 = y->left;

// Perform rotation
y->left = x;
x->right = T2;

// Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;

// Return the new root


return y;
}
// Function to get the balance factor of a node
int getBalance(struct Node* node) {
if (node == NULL)
return 0;
return height(node->left) - height(node->right);
}

// Function to insert a node into the AVL tree


struct Node* insert(struct Node* root, int data) {
// Perform the normal BST insertion
if (root == NULL)
return createNode(data);
if (data < root->data)
root->left = insert(root->left, data);
else if (data > root->data)
root->right = insert(root->right, data);
else // Duplicate keys are not allowed in AVL tree
return root;

// Update the height of the current node


root->height = 1 + max(height(root->left), height(root->right));

// Get the balance factor of the current node


int balance = getBalance(root);

// Perform rotations if the node becomes unbalanced


if (balance > 1 && data < root->left->data)
return rightRotate(root);
if (balance < -1 && data > root->right->data)
return leftRotate(root);
if (balance > 1 && data > root->left->data) {
root->left = leftRotate(root->left);
return rightRotate(root);
}
if (balance < -1 && data < root->right->data) {
root->right = rightRotate(root->right);
return leftRotate(root);
}

// Return the unchanged root


return root;
}

// Function to print the AVL tree in inorder traversal


void inorderTraversal(struct Node* root) {
if (root == NULL)
return;
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}

// Driver code
int main() {
struct Node* root = NULL;

// Insert elements into the AVL tree


root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);

// Print the AVL tree


printf("AVL tree in inorder traversal: ");
inorderTraversal(root);
printf("\n");

return 0;
}

adjecancy matrix and graph search methods \\


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

// Function to perform Breadth-First Search (BFS)


void BFS(int** graph, int numVertices, int startVertex) {
// Array to keep track of visited vertices
int* visited = (int*)malloc(numVertices * sizeof(int));
for (int i = 0; i < numVertices; i++) {
visited[i] = 0;
}

// Create a queue for BFS


int* queue = (int*)malloc(numVertices * sizeof(int));
int front = 0, rear = -1;

// Mark the start vertex as visited and enqueue it


visited[startVertex] = 1;
queue[++rear] = startVertex;

printf("Breadth-First Search: ");

while (front <= rear) {


// Dequeue a vertex from the queue and print it
int currentVertex = queue[front++];
printf("%d ", currentVertex);

// Explore all adjacent vertices of the dequeued vertex


for (int i = 0; i < numVertices; i++) {
if (graph[currentVertex][i] == 1 && !visited[i]) {
visited[i] = 1;
queue[++rear] = i;
}
}
}

printf("\n");

// Free dynamically allocated memory


free(visited);
free(queue);
}

// Function to perform Depth-First Search (DFS) recursively


void DFSRecursive(int** graph, int numVertices, int currentVertex, int* visited) {
visited[currentVertex] = 1;
printf("%d ", currentVertex);

for (int i = 0; i < numVertices; i++) {


if (graph[currentVertex][i] == 1 && !visited[i]) {
DFSRecursive(graph, numVertices, i, visited);
}
}
}

// Function to perform Depth-First Search (DFS)


void DFS(int** graph, int numVertices, int startVertex) {
// Array to keep track of visited vertices
int* visited = (int*)malloc(numVertices * sizeof(int));
for (int i = 0; i < numVertices; i++) {
visited[i] = 0;
}

printf("Depth-First Search: ");


DFSRecursive(graph, numVertices, startVertex, visited);
printf("\n");

// Free dynamically allocated memory


free(visited);
}

// Driver code
int main() {
int numVertices;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &numVertices);

// Create the adjacency matrix


int** graph = (int**)malloc(numVertices * sizeof(int*));
for (int i = 0; i < numVertices; i++) {
graph[i] = (int*)malloc(numVertices * sizeof(int));
}

// Read the adjacency matrix from the user


printf("Enter the adjacency matrix:\n");
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
scanf("%d", &graph[i][j]);
}
}

int startVertex;
printf("Enter the starting vertex for traversal: ");
scanf("%d", &startVertex);

// Perform BFS and DFS


BFS(graph, numVertices, startVertex);
DFS(graph, numVertices, startVertex);

// Free dynamically allocated memory


for (int i = 0; i < numVertices; i++) {
free(graph[i]);
}
free(graph);
return 0;
}

two dimensional matrix and obtain the Traversals on the graph using DFS and BFS
methods \\
#include <stdio.h>
#include <stdlib.h>

// Function to perform Breadth-First Search (BFS)


void BFS(int** graph, int numVertices, int startVertex) {
// Rest of the code for BFS traversal
// ...
}

// Function to perform Depth-First Search (DFS)


void DFS(int** graph, int numVertices, int startVertex) {
// Rest of the code for DFS traversal
// ...
}

int main() {
int numVertices;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &numVertices);

// Create the adjacency matrix


int** graph = (int**)malloc(numVertices * sizeof(int*));
for (int i = 0; i < numVertices; i++) {
graph[i] = (int*)malloc(numVertices * sizeof(int));
}

// Read the adjacency matrix from the user


printf("Enter the adjacency matrix:\n");
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
scanf("%d", &graph[i][j]);
}
}

int startVertex;
printf("Enter the starting vertex for traversal: ");
scanf("%d", &startVertex);

// Perform BFS and DFS


BFS(graph, numVertices, startVertex);
DFS(graph, numVertices, startVertex);

// Free dynamically allocated memory


for (int i = 0; i < numVertices; i++) {
free(graph[i]);
}
free(graph);

return 0;
}

You might also like