Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 62

SLL

1)

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} Node;

Node* head = NULL;

void printList() {

printf("Elements are: ");

Node* temp = head;

while(temp != NULL) {

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

temp = temp->next;

if(temp == NULL){

printf("(NULL)\n");

void insertFirst(int val) {

Node* newNode = (Node*)malloc(sizeof(Node));


newNode->data = val;

newNode->next = head;

head = newNode;

printList();

void insertLast(int val) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = val;

newNode->next = NULL;

if(head == NULL) {

head = newNode;

} else {

Node* temp = head;

while(temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

printList();

void insertAt(int pos, int val) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = val;

if(pos == 0) {

newNode->next = head;
head = newNode;

} else {

Node* temp = head;

for(int i=0; i<pos-1; i++) {

if(temp == NULL) return;

temp = temp->next;

if(temp == NULL) return;

newNode->next = temp->next;

temp->next = newNode;

printList();

void deleteFirst() {

if(head == NULL) return;

Node* temp = head;

head = head->next;

free(temp);

printList();

void deleteLast() {

if(head == NULL) return;

if(head->next == NULL) {

free(head);
head = NULL;

} else {

Node* secondLast = head;

while(secondLast->next->next != NULL) {

secondLast = secondLast->next;

free(secondLast->next);

secondLast->next = NULL;

printList();

void deleteAt(int pos) {

if(head == NULL) return;

if(pos == 0) {

Node* temp = head;

head = head->next;

free(temp);

} else {

Node* temp = head;

for(int i=0; i<pos-1; i++) {

if(temp == NULL) return;

temp = temp->next;

if(temp == NULL || temp->next == NULL) return;

Node* nextNode = temp->next->next;


free(temp->next);

temp->next = nextNode;

printList();

int main() {

int choice, pos, val;

while(1) {

printf("\n1. Insert First\n2. Insert Last\n3. Insert at Position\n4. Delete First\n5. Delete Last\n6.
Delete at Position\n7. Print List\n8. Exit\n");

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch(choice) {

case 1:

printf("Enter value: ");

scanf("%d", &val);

insertFirst(val);

break;

case 2:

printf("Enter value: ");

scanf("%d", &val);

insertLast(val);

break;

case 3:

printf("Enter positionand value: ");


scanf("%d %d", &pos, &val);

insertAt(pos, val);

break;

case 4:

deleteFirst();

break;

case 5:

deleteLast();

break;

case 6:

printf("Enter position: ");

scanf("%d", &pos);

deleteAt(pos);

break;

case 7:

printList();

break;

case 8:

return 0;

return 0;

}
DLL

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* prev;

struct Node* next;

} Node;

Node* head = NULL;

void printList() {

Node* temp = head;

while(temp != NULL) {

printf("%d <-> ", temp->data);

temp = temp->next;

printf("(NULL)\n");

void insertFirst(int val) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = val;

newNode->prev = NULL;

newNode->next = head;

if(head != NULL) {

head->prev = newNode;
}

head = newNode;

printList();

void insertLast(int val) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = val;

newNode->next = NULL;

if(head == NULL) {

newNode->prev = NULL;

head = newNode;

} else {

Node* temp = head;

while(temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

printList();

void insertAt(int pos, int val) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = val;
if(pos == 0) {0,

newNode->prev = NULL;

newNode->next = head;

if(head != NULL) {

head->prev = newNode;

head = newNode;

} else {

Node* temp = head;

for(int i=0; i<pos-1; i++) {

if(temp == NULL) return;

temp = temp->next;

if(temp == NULL || temp->next == NULL) return;

newNode->prev = temp;

newNode->next = temp->next;

if(temp->next != NULL) {

temp->next->prev = newNode;

temp->next = newNode;

printList();

void deleteFirst() {

if(head == NULL) return;


Node* temp = head;

head = head->next;

if(head != NULL) {

head->prev = NULL;

free(temp);

printList();

void deleteLast() {

if(head == NULL) return;

Node* temp = head;

while(temp->next != NULL) {

temp = temp->next;

if(temp->prev != NULL) {

temp->prev->next = NULL;

} else {

head = NULL;

free(temp);
printList();

void deleteAt(int pos) {

if(head == NULL) return;

Node* temp = head;

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

if(temp == NULL) return;

temp=temp->next;

if(temp==NULL || temp -> next==NULL){

return;

if(temp -> prev !=NULL){

temp -> prev -> next=temp -> next;

if(temp -> next !=NULL){

temp -> next -> prev=temp -> prev;

free(temp);

printList();

}
int main() {

int choice, pos, val;

while(1) {

printf("double linked list");

printf("\n1. Insert First\n2. Insert Last\n3. Insert at Position\n4. Delete First\n5. Delete Last\n6.
Delete at Position\n7. Print List\n8. Exit\n");

printf("enter your choice :\t");

scanf("%d", &choice);

switch(choice) {

case 1:

printf("Enter value: ");

scanf("%d", &val);

insertFirst(val);

break;

case 2:

printf("Enter value: ");

scanf("%d", &val);

insertLast(val);

break;

case 3:

printf("Enter position and value: ");

scanf("%d %d", &pos, &val);

insertAt(pos, val);

break;

case 4:

deleteFirst();
break;

case 5:

deleteLast();

break;

case 6:

printf("Enter position: ");

scanf("%d", &pos);

deleteAt(pos);

break;

case 7:

printList();

break;

case 8:

return 0;

return 0;

}
CSLL

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;

};

struct Node *head = NULL;

void createList() {

int n, data;

struct Node *newNode;

printf("Enter the number of nodes: ");

scanf("%d", &n);

if (n <= 0) {

printf("Invalid number of nodes.\n");

return;

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

newNode = (struct Node *)malloc(sizeof(struct Node));

printf("Enter data for node %d: ", i + 1);

scanf("%d", &data);

newNode->data = data;
newNode->next = NULL;

if (head == NULL) {

head = newNode;

newNode->next = head;

} else {

struct Node *temp = head;

while (temp->next != head)

temp = temp->next;

temp->next = newNode;

newNode->next = head;

void insertAtFirst(int data) {

struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = head;

struct Node *temp = head;

while (temp->next != head)

temp = temp->next;

temp->next = newNode;

head = newNode;

void insertAtLast(int data) {


struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = head;

if (head == NULL) {

head = newNode;

newNode->next = head;

} else {

struct Node *temp = head;

while (temp->next != head)

temp = temp->next;

temp->next = newNode;

void insertAtPosition(int data, int position) {

struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

if (position == 1) {

insertAtFirst(data);

return;

struct Node *temp = head;

for (int i = 1; i < position - 1; i++) {

temp = temp->next;
if (temp == head) {

printf("Invalid position for insertion.\n");

return;

newNode->next = temp->next;

temp->next = newNode;

void deleteAtFirst() {

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node *temp = head;

while (temp->next != head)

temp = temp->next;

if (temp == head) {

free(head);

head = NULL;

} else {

temp->next = head->next;

free(head);

head = temp->next;

}
void deleteAtLast() {

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node *temp = head;

struct Node *prev = NULL;

while (temp->next != head) {

prev = temp;

temp = temp->next;

if (prev == NULL) {

free(head);

head = NULL;

} else {

prev->next = head;

free(temp);

void deleteAtPosition(int position) {

if (head == NULL) {

printf("List is empty.\n");

return;

}
if (position == 1) {

deleteAtFirst();

return;

struct Node *temp = head;

struct Node *prev = NULL;

for (int i = 1; i < position; i++) {

prev = temp;

temp = temp->next;

if (temp == head) {

printf("Invalid position for deletion.\n");

return;

prev->next = temp->next;

free(temp);

void displayList() {

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node *temp = head;

do {
printf("%d -> ", temp->data);

temp = temp->next;

} while (temp != head);

printf(" (NULL)\n");

int main() {

int choice, data, position;

while (1) {

printf("\n***CSL list menu***");

printf("\n1.create list\n2.insert at first\n3.insert at last\n4.insert at "

"specfic position \n5.delete at first \n6.delete at last \n7.delete "

"at specific position\n8.Display list\n9.exit");

printf("\nEnter your choice:");

scanf("%d", &choice);

switch (choice) {

case 1:

createList();

break;

case 2:

printf("Enter data to insert at first: ");

scanf("%d", &data);

insertAtFirst(data);

break;

case 3:

printf("Enter data to insert at last: ");


scanf("%d", &data);

insertAtLast(data);

break;

case 4:

printf("Enter data to insert: ");

scanf("%d", &data);

printf("Enter position for insertion: ");

scanf("%d", &position);

insertAtPosition(data, position);

break;

case 5:

deleteAtFirst();

break;

case 6:

deleteAtLast();

break;

case 7:

printf("Enter position for deletion: ");

scanf("%d", &position);

deleteAtPosition(position);

break;

case 8:

displayList();

break;

case 9:

exit(0);

default:

printf("Invalid choice. Please try again.\n");

}
}

return 0;

}
CDLL

#include <stdio.h>

#include <stdlib.h>

Struct Node {

Int data;

Struct Node* next;

Struct Node* prev;

};

Struct Node* head = NULL;

Void createList() {

Int n, data;

Struct Node* newNode;

Printf(“Enter the number of nodes: “);

Scanf(“%d”, &n);

If (n <= 0) {

Printf(“Invalid number of nodes.\n”);

Return;

For (int I = 0; I < n; i++) {

newNode = (struct Node*)malloc(sizeof(struct Node));

printf(“Enter data for node %d: “, I + 1);

scanf(“%d”, &data);
newNode->data = data;

newNode->next = NULL;

newNode->prev = NULL;

if (head == NULL) {

head = newNode;

newNode->next = head;

newNode->prev = head;

} else {

Struct Node* temp = head;

While (temp->next != head)

Temp = temp->next;

Temp->next = newNode;

newNode->prev = temp;

newNode->next = head;

head->prev = newNode;

Void insertAtFirst(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

if (head == NULL) {

head = newNode;

newNode->next = head;

newNode->prev = head;

} else {
newNode->next = head;

newNode->prev = head->prev;

head->prev->next = newNode;

head->prev = newNode;

head = newNode;

Void insertAtLast(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

if (head == NULL) {

head = newNode;

newNode->next = head;

newNode->prev = head;

} else {

Struct Node* temp = head->prev;

Temp->next = newNode;

newNode->prev = temp;

newNode->next = head;

head->prev = newNode;

Void insertAtPosition(int data, int position) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;
if (position == 1) {

insertAtFirst(data);

return;

Struct Node* temp = head;

For (int I = 1; I < position – 1; i++) {

Temp = temp->next;

If (temp == head) {

Printf(“Invalid position for insertion.\n”);

Free(newNode);

Return;

newNode->next = temp->next;

newNode->prev = temp;

temp->next->prev = newNode;

temp->next = newNode;

Void deleteAtFirst() {

If (head == NULL) {

Printf(“List is empty.\n”);

Return;

Struct Node* temp = head;


If (temp->next == temp) { // Only one node in the list

Free(temp);

Head = NULL;

} else {

Head = temp->next;

Head->prev = temp->prev;

Temp->prev->next = head;

Free(temp);

Void deleteAtLast() {

If (head == NULL) {

Printf(“List is empty.\n”);

Return;

Struct Node* temp = head->prev;

If (temp == head) { // Only one node in the list

Free(temp);

Head = NULL;

} else {

Head->prev = temp->prev;

Temp->prev->next = head;

Free(temp);

}
Void deleteAtPosition(int position) {

If (head == NULL) {

Printf(“List is empty.\n”);

Return;

If (position == 1) {

deleteAtFirst();

return;

Struct Node* temp = head;

For (int I = 1; I < position; i++) {

Temp = temp->next;

If (temp == head) {

Printf(“Invalid position for deletion.\n”);

Return;

Temp->prev->next = temp->next;

Temp->next->prev = temp->prev;

Free(temp);

Void displayList() {

If (head == NULL) {

Printf(“List is empty.\n”);

Return;
}

Struct Node* temp = head;

Do {

Printf(“%d <-> “, temp->data);

Temp = temp->next;

} while (temp != head);

Printf(“ (NULL)\n”);

Int main() {

Int choice, data, position;

While (1) {

Printf(“\n***list menu***”);

Printf(“\n1.create list\n2.insert at first\n3.insert at last\n4.insert at specific position \n5.delete at


first \n6.delete at last \n7.delete at specific position\n8.Display list\n9.exit”);

Printf(“\nEnter your choice:”);

Scanf(“%d”, &choice);

Switch (choice) {

Case 1:

createList();

break;

case 2:

printf(“Enter data to insert at first: “);

scanf(“%d”, &data);

insertAtFirst(data);
break;

case 3:

printf(“Enter data to insert at last: “);

scanf(“%d”, &data);

insertAtLast(data);

break;

case 4:

printf(“Enter data to insert: “);

scanf(“%d”, &data);

printf(“Enter position for insertion: “);

scanf(“%d", &position);

insertAtPosition(data, position);

break;

case 5:

deleteAtFirst();

break;

case 6:

deleteAtLast();

break;

case 7:

printf("Enter position for deletion: ");

scanf("%d", &position);

deleteAtPosition(position);

break;

case 8:

displayList();

break;

case 9:

exit(0);
default:

printf("Invalid choice. Please try again.\n");

return 0;

}
STACK

#include <stdio.h>

int array[5], size = 0;

void print_stack() {

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

printf("%d ", array[i]); // Use %d to print the element itself, not its address

printf("\n");

void push(int val) {

if (size == 5) {

printf("Stack is full\n");

return;

array[size] = val;

size++;

print_stack();

void pop() {

if (size == 0) {

printf("Stack is empty\n");

return;

} else {
size--;

print_stack();

int main() {

int val, choice;

while (1) {

printf("Select function:\n 1. Push\n 2. Pop\n 3. Print stack\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value: ");

scanf("%d", &val);

push(val);

break;

case 2:

pop();

break;

case 3:

print_stack();

break;

default:

printf("Invalid choice\n");

return 0;

}
QUEUES

#include <stdio.h>

int array[5], size = 0;

void printQueue() {

if (size == 0) {

printf("Queue is empty\n");

return;

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

printf("%d ", array[i]);

printf("\n");

void enqueue(int val) {

if (size == 5) {

printf("Queue is full\n");

return;

array[size] = val;

size++;

printQueue();

}
void deQueue() {

if (size == 0) {

printf("Queue is empty\n");

return;

for (int i = 0; i < size - 1; i++) {

array[i] = array[i + 1];

size--;

printQueue();

int main() {

int val, choice;

while (1) {

printf("Select function:\n 1. Enqueue\n 2. Dequeue\n 3. Print Queue\n 4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value: ");

scanf("%d", &val);

enqueue(val);

break;

case 2:

deQueue();

break;

case 3:
printQueue();

break;

case 4:

return 0; // Exit the program

default:

printf("Invalid choice\n");

}
STACKS LL

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} Node;

Node* head = NULL;

void print_stack() {

Node* temp = head;

while(temp != NULL) {

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

temp = temp->next;

if(temp == NULL){

printf("(NULL)");

void push(int val) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = val;

newNode->next = NULL;
if(head == NULL) {

head = newNode;

} else {

Node* temp = head;

while(temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

print_stack();

void pop() {

if(head == NULL) return;

if(head->next == NULL) {

free(head);

head = NULL;

} else {

Node* secondLast = head;

while(secondLast->next->next != NULL) {

secondLast = secondLast->next;

free(secondLast->next);

secondLast->next = NULL;

print_stack();

int main() {
int val, choice;

while (1) {

printf("\nSelect function:\n 1. Push\n 2. Pop\n 3. Print stack\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value: ");

scanf("%d", &val);

push(val);

break;

case 2:

pop();

break;

case 3:

print_stack();

break;

default:

printf("Invalid choice\n");

2 return 0;

}
QUEUES LL

#include <stdio.h>

#include <stdlib.h>

Typedef struct Node {

Int data;

Struct Node* next;

} Node;

Node* head = NULL;

Void printQueue() {

Printf(“Elements are: “);

Node* temp = head;

While(temp != NULL) {

Printf(“%d -> “, temp->data);

Temp = temp->next;

If(temp == NULL){

Printf(“(NULL)”);

Void enqueue(int val) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = val;

newNode->next = NULL;
if(head == NULL) {

head = newNode;

} else {

Node* temp = head;

While(temp->next != NULL) {

Temp = temp->next;

Temp->next = newNode;

printQueue();

Void deQueue() {

If(head == NULL) return;

Node* temp = head;

Head = head->next;

Free(temp);

printQueue();

Int main() {

Int val, choice;

While (1) {

Printf(“\nSelect function:\n 1. Enqueue\n 2. Dequeue\n 3. Print Queue\n 4. Exit\n”);

Printf(“Enter your choice: “);

Scanf(“%d”, &choice);
Switch (choice) {

Case 1:

Printf(“Enter value: “);

Scanf(“%d”, &val);

Enqueue(val);

Break;

Case 2:

deQueue();

break;

case 3:

printQueue();

break;

case 4:

return 0; // Exit the program

default:

printf(“Invalid choice\n”);

}
SORTINGS

#include <stdio.h>

#include <stdlib.h>

// Function to swap the position of two elements

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

// Function to perform quicksort

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 (arr[j] < pivot) {

i++;

swap(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[high]);

return (i + 1);

}
void quickSort(int arr[], int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

// Function to heapify a subtree rooted at index i in the array arr of size n

void heapify(int arr[], int n, int i) {

int largest = i;

int left = 2 * i + 1;

int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])

largest = left;

if (right < n && arr[right] > arr[largest])

largest = right;

if (largest != i) {

swap(&arr[i], &arr[largest]);

heapify(arr, n, largest);

// Function to perform heap sort

void heapSort(int arr[], int n) {

for (int i = n / 2 - 1; i >= 0; i--)


heapify(arr, n, i);

for (int i = n - 1; i >= 0; i--) {

swap(&arr[0], &arr[i]);

heapify(arr, i, 0);

// Function to merge two subarrays of arr[]

void merge(int arr[], int l, int m, int r) {

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

for (i = 0; i < n1; i++)

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1 + j];

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;
} else {

arr[k] = R[j];

j++;

k++;

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {

arr[k] = R[j];

j++;

k++;

// Function to perform merge sort

void mergeSort(int arr[], int l, int r) {

if (l < r) {

int m = l + (r - l) / 2;

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

merge(arr, l, m, r);

}
// Function to print an array

void printArray(int arr[], int size) {

for (int i = 0; i < size; i++)

printf("%d ", arr[i]);

printf("\n");

int main() {

int x, choice;

printf("Enter the size of the list: ");

scanf("%d", &x);

int *arr = (int *)malloc(x * sizeof(int));

printf("Enter the elements of the list to be sorted: ");

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

scanf("%d", &arr[i]);

printf("Original array: ");

printArray(arr, x);

printf("\nChoose sorting algorithm:\n");

printf("1. Quick Sort\n");

printf("2. Merge Sort\n");

printf("3. Heap Sort\n");

printf("Enter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1:

quickSort(arr, 0, x - 1);

printf("Sorted array using Quick Sort: \n");

break;

case 2:

mergeSort(arr, 0, x - 1);

printf("Sorted array using Merge Sort: \n");

break;

case 3:

heapSort(arr, x);

printf("Sorted array using Heap Sort: \n");

break;

default:

printf("Invalid choice\n");

return 1; // Indicate an error

printArray(arr, x);

// Free the dynamically allocated memory

free(arr);

return 0;

}
GRAPH TRAVERSAL

#include <stdio.h>

#include <stdlib.h>

#define SIZE 40

// Structure for node

struct node {

int vertex;

struct node *next;

};

// Structure for graph

struct Graph {

int numVertices;

struct node **adjLists;

int *visited;

};

// Structure for queue

struct queue {

int items[SIZE];

int front;

int rear;

};

// Function prototypes

struct node *createNode(int);


struct Graph *createGraph(int);

void addEdge(struct Graph *, int, int);

void printGraph(struct Graph *);

void DFS(struct Graph *, int);

void BFS(struct Graph *, int);

struct queue *createQueue();

void enqueue(struct queue *, int);

int dequeue(struct queue *);

int isEmpty(struct queue *);

void printQueue(struct queue *);

// Function to create a node

struct node *createNode(int v) {

struct node *newNode = malloc(sizeof(struct node));

newNode->vertex = v;

newNode->next = NULL;

return newNode;

// Function to create a graph

struct Graph *createGraph(int vertices) {

struct Graph *graph = malloc(sizeof(struct Graph));

graph->numVertices = vertices;

graph->adjLists = malloc(vertices * sizeof(struct node *));

graph->visited = malloc(vertices * sizeof(int));

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

graph->adjLists[i] = NULL;
graph->visited[i] = 0;

return graph;

// Function to add an edge to the graph

void addEdge(struct Graph *graph, int src, int dest) {

struct node *newNode = createNode(dest);

newNode->next = graph->adjLists[src];

graph->adjLists[src] = newNode;

newNode = createNode(src);

newNode->next = graph->adjLists[dest];

graph->adjLists[dest] = newNode;

// Function to print the graph

void printGraph(struct Graph *graph) {

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

struct node *temp = graph->adjLists[i];

printf("\n Adjacency list of vertex %d\n ", i);

while (temp) {

printf("%d -> ", temp->vertex);

temp = temp->next;

printf("\n");

}
// Function for DFS

void DFS(struct Graph *graph, int vertex) {

struct node *adjList = graph->adjLists[vertex];

struct node *temp = adjList;

graph->visited[vertex] = 1;

printf("Visited %d \n", vertex);

while (temp != NULL) {

int connectedVertex = temp->vertex;

if (graph->visited[connectedVertex] == 0) {

DFS(graph, connectedVertex);

temp = temp->next;

// Function to create a queue

struct queue *createQueue() {

struct queue *q = malloc(sizeof(struct queue));

q->front = -1;

q->rear = -1;

return q;

// Function to check if the queue is empty

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


// Function to enqueue an item to the queue

void enqueue(struct queue *q, int value) {

if (q->rear == SIZE - 1)

printf("\nQueue is Full!!");

else {

if (q->front == -1)

q->front = 0;

q->rear++;

q->items[q->rear] = value;

// Function to dequeue an item from the queue

int dequeue(struct queue *q) {

int item;

if (isEmpty(q)) {

printf("Queue is empty");

item = -1;

} else {

item = q->items[q->front];

q->front++;

if (q->front > q->rear) {

printf("Resetting queue ");

q->front = q->rear = -1;

return item;

}
// Function to print the queue

void printQueue(struct queue *q) {

int i = q->front;

if (!isEmpty(q)) {

printf("\nQueue contains \n");

for (i = q->front; i < q->rear + 1; i++) {

printf("%d ", q->items[i]);

// Function for BFS

void BFS(struct Graph *graph, int startVertex) {

struct queue *q = createQueue();

graph->visited[startVertex] = 1;

enqueue(q, startVertex);

while (!isEmpty(q)) {

int currentVertex = dequeue(q);

printf("Visited %d\n", currentVertex);

struct node *temp = graph->adjLists[currentVertex];

while (temp) {

int adjVertex = temp->vertex;


if (graph->visited[adjVertex] == 0) {

graph->visited[adjVertex] = 1;

enqueue(q, adjVertex);

temp = temp->next;

int main() {

int choice, startVertex;

printf("\nEnter the starting vertex for traversal: ");

scanf("%d", &startVertex);

printf("\nChoose traversal algorithm:\n");

printf("1. DFS\n");

printf("2. BFS\n");

printf("Enter your choice: ");

scanf("%d", &choice);

struct Graph *graph;

switch (choice) {

case 1:

graph = createGraph(6);

addEdge(graph, 0, 1);

addEdge(graph, 0, 2);

addEdge(graph, 1, 2);
addEdge(graph, 2, 3);

addEdge(graph, 2, 4);

addEdge(graph, 3, 1);

addEdge(graph, 3, 5);

addEdge(graph, 5, 4);

printf("\nGraph:\n");

printGraph(graph);

printf("\nDFS Traversal:\n");

DFS(graph, startVertex);

break;

case 2:

graph = createGraph(4);

addEdge(graph, 0, 1);

addEdge(graph, 0, 2);

addEdge(graph, 1, 2);

addEdge(graph, 1, 3);

addEdge(graph, 1, 3);

addEdge(graph, 2, 3);

addEdge(graph, 3, 3);

printf("\nGraph:\n");

printGraph(graph);

printf("\nBFS Traversal:\n");

BFS(graph, startVertex);

break;

default:
printf("\nInvalid choice\n");

return 1; // Indicate an error

return 0;

}
PATTERN MATCHING

#include <limits.h>

#include <string.h>

#include <stdio.h>

#define NO_OF_CHARS 256

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

void badCharHeuristic(char *str, int size, int badchar[NO_OF_CHARS]);

void search(char *txt, char *pat);

void kmpSearch(char *txt, char *pat);

void computeLPSArray(char *pat, int m, int *lps);

int main()

int choice;

char txt[100];

char pat[100];

printf("Choose the algorithm:\n");

printf("1. Knuth-Morris-Pratt\n");

printf("2. Boyer-Moore\n");

printf("Enter your choice (1 or 2): ");

scanf("%d", &choice);

printf("Enter the text: ");


scanf("%s", txt);

printf("Enter the pattern: ");

scanf("%s", pat);

switch (choice)

case 1:

kmpSearch(txt, pat);

break;

case 2:

search(txt, pat);

break;

default:

printf("Invalid choice. Please enter 1 or 2.\n");

break;

return 0;

void badCharHeuristic(char *str, int size, int badchar[NO_OF_CHARS])

int i;

for (i = 0; i < NO_OF_CHARS; i++)

badchar[i] = -1;

for (i = 0; i < size; i++)

badchar[(int)str[i]] = i;

}
void search(char *txt, char *pat)

int m = strlen(pat);

int n = strlen(txt);

int badchar[NO_OF_CHARS];

badCharHeuristic(pat, m, badchar);

int s = 0;

while (s <= (n - m))

int j = m - 1;

while (j >= 0 && pat[j] == txt[s + j])

j--;

if (j < 0)

printf("\nPattern occurs at shift = %d", s);

s += (s + m < n) ? m - badchar[txt[s + m]] : 1;

else

s += max(1, j - badchar[txt[s + j]]);

void kmpSearch(char *txt, char *pat)

int m = strlen(pat);

int n = strlen(txt);
int lps[m];

computeLPSArray(pat, m, lps);

int i = 0;

int j = 0;

while (i < n)

if (pat[j] == txt[i])

j++;

i++;

if (j == m)

printf("\nPattern occurs at shift = %d", i - j);

j = lps[j - 1];

else if (i < n && pat[j] != txt[i])

if (j != 0)

j = lps[j - 1];

else

i = i + 1;

void computeLPSArray(char *pat, int m, int *lps)

int len = 0;
int i = 1;

lps[0] = 0;

while (i < m)

if (pat[i] == pat[len])

len++;

lps[i] = len;

i++;

else

if (len != 0)

len = lps[len - 1];

else

lps[i] = 0;

i++;

You might also like