0% found this document useful (0 votes)
27 views35 pages

DSFILE

The document describes the implementation of various queue data structures using arrays in C, including: 1) Linear queue using an array 2) Circular queue using an array 3) Priority queue using an array where elements are inserted in priority order 4) Double-ended queue (deque) using an array where elements can be inserted/removed from both ends Functions are defined for initialization, checking empty/full status, insertion, removal and display of elements for each implementation. Main functions test the queue operations and display output.

Uploaded by

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

DSFILE

The document describes the implementation of various queue data structures using arrays in C, including: 1) Linear queue using an array 2) Circular queue using an array 3) Priority queue using an array where elements are inserted in priority order 4) Double-ended queue (deque) using an array where elements can be inserted/removed from both ends Functions are defined for initialization, checking empty/full status, insertion, removal and display of elements for each implementation. Main functions test the queue operations and display output.

Uploaded by

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

// 3.

(a) ARRAY IMPLEMENTAION OF LINEAR QUEUE


#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
struct Queue {
int front, rear;
int array[MAX_SIZE];
};
void initializeQueue(struct Queue *queue) {
queue->front = -1;
queue->rear = -1;
}
int isEmpty(struct Queue *queue) {
return (queue->front == -1 && queue->rear == -1);
}
int isFull(struct Queue *queue) {
return (queue->rear == MAX_SIZE - 1);
}
void enqueue(struct Queue *queue, int value) {
if (isFull(queue)) {
printf("Queue is full. Cannot enqueue.\n");
return;
}
if (isEmpty(queue)) {
queue->front = 0;
}
queue->rear++;
queue->array[queue->rear] = value;
printf("Enqueued: %d\n", value);
}
void dequeue(struct Queue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty. Cannot dequeue.\n");
return;
}
printf("Dequeued: %d\n", queue->array[queue->front]);
if (queue->front == queue->rear) {
initializeQueue(queue);
} else {
queue->front++;
}
}
void display(struct Queue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
for (int i = queue->front; i <= queue->rear; i++) {
printf("%d ", queue->array[i]);
}
printf("\n");
}
int main() {
struct Queue queue;
initializeQueue(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
display(&queue);
dequeue(&queue);
display(&queue);
enqueue(&queue, 4);
display(&queue);
return 0;
}

OUTPUT:

Enqueued: 1

Enqueued: 2

Enqueued: 3

Queue elements: 1 2 3

Dequeued: 1

Queue elements: 2 3

Enqueued: 4

Queue elements: 2 3 4
3.(b) ARRAY IMPLEMENTAION OF CIRCULAR QUEUE
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
typedef struct {
int data[MAX_SIZE];
int front, rear;
} CircularQueue;
void initializeQueue(CircularQueue *queue) {
queue->front = -1;
queue->rear = -1;
}
int isFull(CircularQueue *queue) {
return (queue->front == (queue->rear + 1) % MAX_SIZE);
}
int isEmpty(CircularQueue *queue) {
return (queue->front == -1 && queue->rear == -1);
}
void enqueue(CircularQueue *queue, int element) {
if (isFull(queue)) {
printf("Queue is full. Cannot enqueue.\n");
return;
}
if (isEmpty(queue)) {
queue->front = 0;
queue->rear = 0;
} else {
queue->rear = (queue->rear + 1) % MAX_SIZE;
}
queue->data[queue->rear] = element;
printf("%d enqueued to the queue.\n", element);
}
void dequeue(CircularQueue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty. Cannot dequeue.\n");
return;
}
printf("%d dequeued from the queue.\n", queue->data[queue->front]);

if (queue->front == queue->rear) {
initializeQueue(queue);
} else {
queue->front = (queue->front + 1) % MAX_SIZE;
}
}
void displayQueue(CircularQueue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
int i = queue->front;
do {
printf("%d ", queue->data[i]);
i = (i + 1) % MAX_SIZE;
} while (i != (queue->rear + 1) % MAX_SIZE);
printf("\n");
}
int main() {
CircularQueue queue;
initializeQueue(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
displayQueue(&queue);
dequeue(&queue);
displayQueue(&queue);
enqueue(&queue, 4);
displayQueue(&queue);
enqueue(&queue, 5);
displayQueue(&queue);
enqueue(&queue, 6);
dequeue(&queue);
displayQueue(&queue);
return 0;
}

OUTPUT:

1 enqueued to the queue.


2 enqueued to the queue.
3 enqueued to the queue.
Queue elements: 1 2 3
1 dequeued from the queue.
Queue elements: 2 3
4 enqueued to the queue.
Queue elements: 2 3 4
5 enqueued to the queue.
Queue elements: 2 3 4 5
6 enqueued to the queue.
2 dequeued from the queue.
Queue elements: 3 4 5 6
3. (c) ARRAY IMPLEMENTAION OF PRIORITY QUEUE
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
struct PriorityQueue {
int array[MAX_SIZE];
int front;
int rear;
};
void initializeQueue(struct PriorityQueue *pq) {
pq->front = pq->rear = -1;
}
int isFull(struct PriorityQueue *pq) {
return pq->rear == MAX_SIZE - 1;
}
int isEmpty(struct PriorityQueue *pq) {
return pq->front == -1;
}
void enqueue(struct PriorityQueue *pq, int data) {
if (isFull(pq)) {
printf("Queue is full. Cannot enqueue.\n");
return;
}
if (isEmpty(pq)) {
pq->front = pq->rear = 0;
pq->array[0] = data;
} else {
int i;
for (i = pq->rear; i >= pq->front; i--) {
if (data > pq->array[i]) {
pq->array[i + 1] = pq->array[i];
} else {
break;
}
}
pq->array[i + 1] = data;
pq->rear++;
}
}
void dequeue(struct PriorityQueue *pq) {
if (isEmpty(pq)) {
printf("Queue is empty. Cannot dequeue.\n");
return;
}
printf("Dequeued element: %d\n", pq->array[pq->front]);
if (pq->front == pq->rear) {
initializeQueue(pq);
} else {
pq->front++;
}
}
void display(struct PriorityQueue *pq) {
if (isEmpty(pq)) {
printf("Queue is empty.\n");
return;
}

printf("Priority Queue elements: ");


for (int i = pq->front; i <= pq->rear; i++) {
printf("%d ", pq->array[i]);
}
printf("\n");
}
int main() {
struct PriorityQueue pq;
initializeQueue(&pq);
enqueue(&pq, 30);
enqueue(&pq, 50);
enqueue(&pq, 10);
enqueue(&pq, 40);
display(&pq);
dequeue(&pq);
display(&pq);
return 0;
}

OUTPUT :

Priority Queue elements: 50 40 30 10


Dequeued element: 50
Priority Queue elements: 40 30 10
3.(d) ARRAY IMPLEMENTAION OF DOUBLE ENDED QUEUE
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
struct Deque {
int arr[MAX_SIZE];
int front, rear;
};
void initializeDeque(struct Deque *dq) {
dq->front = -1;
dq->rear = -1;
}
int isEmpty(struct Deque *dq) {
return (dq->front == -1 && dq->rear == -1);
}
int isFull(struct Deque *dq) {
return ((dq->rear + 1) % MAX_SIZE == dq->front);
}
void insertFront(struct Deque *dq, int data) {
if (isFull(dq)) {
printf("Deque is full. Cannot insert.\n");
return;
}
if (isEmpty(dq)) {
dq->front = 0;
dq->rear = 0;
} else {
dq->front = (dq->front - 1 + MAX_SIZE) % MAX_SIZE;
}
dq->arr[dq->front] = data;
printf("Inserted %d at the front.\n", data);
}
void insertRear(struct Deque *dq, int data) {
if (isFull(dq)) {
printf("Deque is full. Cannot insert.\n");
return;
}
if (isEmpty(dq)) {
dq->front = 0;
dq->rear = 0;
} else {
dq->rear = (dq->rear + 1) % MAX_SIZE;
}
dq->arr[dq->rear] = data;
printf("Inserted %d at the rear.\n", data);
}
int deleteFront(struct Deque *dq) {
int deletedItem;
if (isEmpty(dq)) {
printf("Deque is empty. Cannot delete.\n");
return -1;
}
deletedItem = dq->arr[dq->front];
if (dq->front == dq->rear) {
initializeDeque(dq);
} else {
dq->front = (dq->front + 1) % MAX_SIZE;
}
printf("Deleted %d from the front.\n", deletedItem);
return deletedItem;
}
int deleteRear(struct Deque *dq) {
int deletedItem;
if (isEmpty(dq)) {
printf("Deque is empty. Cannot delete.\n");
return -1;
}
deletedItem = dq->arr[dq->rear];
if (dq->front == dq->rear) {
initializeDeque(dq);
} else {
dq->rear = (dq->rear - 1 + MAX_SIZE) % MAX_SIZE;
}
printf("Deleted %d from the rear.\n", deletedItem);
return deletedItem;
}
void displayDeque(struct Deque *dq) {
if (isEmpty(dq)) {
printf("Deque is empty.\n");
return;
}
printf("Deque elements: ");
int i = dq->front;
while (1) {
printf("%d ", dq->arr[i]);
if (i == dq->rear)
break;
i = (i + 1) % MAX_SIZE;
}
printf("\n");
}
int main() {
struct Deque dq;
initializeDeque(&dq);
insertRear(&dq, 1);
insertRear(&dq, 2);
insertFront(&dq, 3);
insertFront(&dq, 4);
displayDeque(&dq);
deleteFront(&dq);
deleteRear(&dq);
displayDeque(&dq);
return 0;
}

OUTPUT :

Inserted 1 at the rear.


Inserted 2 at the rear.
Inserted 3 at the front.
Inserted 4 at the front.
Deque elements: 4 3 1 2
Deleted 4 from the front.
Deleted 2 from the rear.
Deque elements: 3 1
4.(a) LINEAR LINKED LIST PRIMITIVE OPERATION
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
struct Node* insertAtBeginning(struct Node* head, int value) {
struct Node* newNode = createNode(value);
newNode->next = head;
return newNode;
}
struct Node* insertAtEnd(struct Node* head, int value) {
struct Node* newNode = createNode(value);
if (head == NULL)
return newNode;
struct Node* current = head;
while (current->next != NULL)
current = current->next;
current->next = newNode;
return head;
}
void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
struct Node* deleteNode(struct Node* head, int value) {
if (head == NULL) {
printf("List is empty\n");
return NULL;
}
struct Node* current = head;
struct Node* previous = NULL;
while (current != NULL && current->data != value) {
previous = current;
current = current->next;
}
if (current == NULL) {
printf("Node with value %d not found\n", value);
return head;
}
if (previous == NULL)
head = current->next;
else
previous->next = current->next;
free(current);
return head;
}
void freeList(struct Node* head) {
struct Node* current = head;
struct Node* nextNode;
while (current != NULL) {
nextNode = current->next;
free(current);
current = nextNode;
}
}
int main() {
struct Node* head = NULL;
head = insertAtBeginning(head, 2);
head = insertAtBeginning(head, 1);
printf("Linked List: ");
displayList(head);
head = insertAtEnd(head, 4);
printf("Linked List after insertion at the end: ");
displayList(head);
head = deleteNode(head, 2);
printf("Linked List after deletion: ");
displayList(head);
freeList(head);
return 0;
}

OUTPUT :

Linked List: 1 -> 2 -> NULL


Linked List after insertion at the end: 1 -> 2 -> 4 -> NULL
Linked List after deletion: 1 -> 4 -> NULL
4.(b) SEARCHING THE LINEAR LINKED LIST
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
struct Node* insertAtBeginning(struct Node* head, int value) {
struct Node* newNode = createNode(value);
newNode->next = head;
return newNode;
}
void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int searchLinkedList(struct Node* head, int target) {
struct Node* current = head;
int position = 1;
while (current != NULL) {
if (current->data == target) {
return position;
}
current = current->next;
position++;
}
return -1;
}
int main() {
struct Node* head = NULL;
head = insertAtBeginning(head, 3);
head = insertAtBeginning(head, 2);
head = insertAtBeginning(head, 1);
printf("Linked List: ");
displayList(head);
int target = 2;
int position = searchLinkedList(head, target);
if (position != -1) {
printf("%d found at position %d\n", target, position);
} else {
printf("%d not found in the linked list\n", target);
}
free(head);
return 0;
}

OUTPUT :

Linked List: 1 -> 2 -> 3 -> NULL


2 found at position 2
4.(c) COUNT OF NODES IN LINEAR LINKED LIST
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
int countNodes(struct Node* head) {
int count = 0;
struct Node* current = head;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
void freeList(struct Node* head) {
struct Node* current = head;
struct Node* nextNode;
while (current != NULL) {
nextNode = current->next;
free(current);
current = nextNode;
}
}
int main() {
struct Node* head = NULL;
head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
printf("Linked List: ");
displayList(head);
int nodeCount = countNodes(head);
printf("Number of nodes in the linked list: %d\n", nodeCount);
freeList(head);
return 0;
}

OUTPUT :

Linked List: 1 -> 2 -> 3 -> 4 -> NULL


Number of nodes in the linked list: 4
4.(d) REVERSE THE LINEAR LINKED LIST
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
struct Node* insertAtBeginning(struct Node* head, int value) {
struct Node* newNode = createNode(value);
newNode->next = head;
return newNode;
}
void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
struct Node* reverseList(struct Node* head) {
struct Node *prev, *current, *next;
prev = NULL;
current = head;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
void freeList(struct Node* head) {
struct Node* current = head;
struct Node* nextNode;
while (current != NULL) {
nextNode = current->next;
free(current);
current = nextNode;
}
}
int main() {
struct Node* head = NULL;
head = insertAtBeginning(head, 3);
head = insertAtBeginning(head, 2);
head = insertAtBeginning(head, 1);
printf("Original Linked List: ");
displayList(head);
head = reverseList(head);
printf("Reversed Linked List: ");
displayList(head);
freeList(head);
return 0;
}

OUTPUT :

Original Linked List: 1 -> 2 -> 3 -> NULL


Reversed Linked List: 3 -> 2 -> 1 -> NULL
4.(e) POLYNOMIAL ARITHMETIC
#include <stdio.h>
#include <stdlib.h>
struct Term {
int coefficient;
int exponent;
struct Term* next;
};
struct Term* createTerm(int coefficient, int exponent) {
struct Term* newTerm = (struct Term*)malloc(sizeof(struct Term));
if (newTerm == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newTerm->coefficient = coefficient;
newTerm->exponent = exponent;
newTerm->next = NULL;
return newTerm;
}
struct Term* insertTerm(struct Term* head, int coefficient, int exponent) {
struct Term* newTerm = createTerm(coefficient, exponent);
if (head == NULL)
return newTerm;
struct Term* current = head;
while (current->next != NULL)
current = current->next;
current->next = newTerm;
return head;
}
void displayPolynomial(struct Term* head) {
struct Term* current = head;
while (current != NULL) {
printf("%dx^%d ", current->coefficient, current->exponent);
if (current->next != NULL)
printf("+ ");
current = current->next;
}
printf("\n");
}
struct Term* addPolynomials(struct Term* poly1, struct Term* poly2) {
struct Term* result = NULL;
while (poly1 != NULL || poly2 != NULL) {
int coefficient = 0, exponent = 0;
if (poly1 != NULL && poly2 != NULL) {
if (poly1->exponent > poly2->exponent) {
coefficient = poly1->coefficient;
exponent = poly1->exponent; poly1 = poly1->next;
} else if (poly1->exponent < poly2->exponent) {
coefficient = poly2->coefficient;
exponent = poly2->exponent; poly2 = poly2->next;
} else {
coefficient = poly1->coefficient + poly2->coefficient;
exponent = poly1->exponent;
poly1 = poly1->next; poly2 = poly2->next;
}
} else if (poly1 != NULL) {
coefficient = poly1->coefficient;
exponent = poly1->exponent; poly1 = poly1->next;
} else if (poly2 != NULL) {
coefficient = poly2->coefficient;
exponent = poly2->exponent; poly2 = poly2->next;
}
result = insertTerm(result, coefficient, exponent);
}
return result;
}
void freePolynomial(struct Term* head) {
struct Term* current = head;
struct Term* nextTerm;
while (current != NULL) {
nextTerm = current->next;
free(current);
current = nextTerm;
}
}
int main() {
struct Term* poly1 = NULL;
struct Term* poly2 = NULL;
poly1 = insertTerm(poly1, 5, 2);
poly1 = insertTerm(poly1, -3, 1);
poly1 = insertTerm(poly1, 2, 0);
poly2 = insertTerm(poly2, 4, 3);
poly2 = insertTerm(poly2, 1, 1);
poly2 = insertTerm(poly2, -7, 0);
struct Term* result = addPolynomials(poly1, poly2);
printf("Resultant Polynomial: ");
displayPolynomial(result);
freePolynomial(poly1);
freePolynomial(poly2);
freePolynomial(result);
return 0;
}
OUTPUT:
Polynomial 1: 5x^2 + -3x^1 + 2x^0
Polynomial 2: 4x^3 + 1x^1 + -7x^0
Resultant Polynomial: 4x^3 + 5x^2 + -2x^1 + -5x^0
4.(f) CIRCULAR LINKED LIST PRIMITIVE OPERATION
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
struct Node* insertAtBeginning(struct Node* head, int value) {
struct Node* newNode = createNode(value);
if (head == NULL) {
newNode->next = newNode;
return newNode;
}
newNode->next = head->next;
head->next = newNode;
return head;
}
struct Node* insertAtEnd(struct Node* head, int value) {
struct Node* newNode = createNode(value);
if (head == NULL) {
newNode->next = newNode;
return newNode;
}
newNode->next = head->next;
head->next = newNode;
return newNode;
}
void displayList(struct Node* head) {
if (head == NULL) {
printf("List is empty\n"); return;
}
struct Node* current = head->next;
do {
printf("%d -> ", current->data);
current = current->next;
} while (current != head->next);
printf("(head)\n");
}
struct Node* deleteNode(struct Node* head, int value) {
if (head == NULL) {
printf("List is empty\n");
return NULL;
}
struct Node* current = head->next;
struct Node* previous = head;
do {
if (current->data == value) {
previous->next = current->next;
free(current);
return head; }
previous = current;
current = current->next;
} while (current != head->next);
return head;
}
void freeList(struct Node* head) {
if (head == NULL)
return;
struct Node* current = head->next;
struct Node* nextNode;
do {
nextNode = current->next;
current = nextNode;
} while (current != head->next);
free(head);
}
int main() {
struct Node* head = NULL;

head = insertAtBeginning(head, 2);


head = insertAtBeginning(head, 1);
printf("Circular Linked List: ");
displayList(head);
head = insertAtEnd(head, 3);
printf("Circular Linked List after insertion at the end: ");
displayList(head);
head = deleteNode(head, 2);
printf("Circular Linked List after deletion: ");
displayList(head);
freeList(head);
return 0;
}
OUTPUT:
Circular Linked List: 1 -> 2 -> (head)
Circular Linked List after insertion at the end: 1 -> 2 -> 3 -> (head)
Circular Linked List after deletion: 1 -> 3 -> (head)
4.(g) DOUBLY LINKED LIST PRIMITIVE OPERATION
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->data = value;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
struct Node* insertAtBeginning(struct Node* head, int value) {
struct Node* newNode = createNode(value);
if (head == NULL) {
return newNode;
}
newNode->next = head;
head->prev = newNode;
return newNode;
}
struct Node* insertAtEnd(struct Node* head, int value) {
struct Node* newNode = createNode(value);
if (head == NULL) {
return newNode;
}
struct Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
return head;
}
void displayForward(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d <-> ", current->data);
current = current->next;
}
printf("NULL\n");
}
void displayBackward(struct Node* head) {
struct Node* current = head;
while (current->next != NULL) {
current = current->next;
}
while (current != NULL) {
printf("%d <-> ", current->data);
current = current->prev;
}
printf("NULL\n");
}
struct Node* deleteNode(struct Node* head, int value) {
if (head == NULL) {
printf("List is empty\n");
return NULL;
}
struct Node* current = head;
while (current != NULL && current->data != value) {
current = current->next;
}
if (current == NULL) {
printf("Node with value %d not found\n", value);
return head;
}
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
head = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
return head;
}
void freeList(struct Node* head) {
struct Node* current = head;
struct Node* nextNode;
while (current != NULL) {
nextNode = current->next;
free(current);
current = nextNode;
}
}
int main() {
struct Node* head = NULL;
head = insertAtBeginning(head, 3);
head = insertAtBeginning(head, 2);
head = insertAtBeginning(head, 1);
printf("Doubly Linked List (Forward): ");
displayForward(head);
printf("Doubly Linked List (Backward): ");
displayBackward(head);
head = insertAtEnd(head, 4);
head = insertAtEnd(head, 5);
printf("Doubly Linked List after insertion at the end (Forward): ");
displayForward(head);
head = deleteNode(head, 3);
printf("Doubly Linked List after deletion (Forward): ");
displayForward(head);
freeList(head);
return 0;
}

OUTPUT:

Doubly Linked List (Forward): 1 <-> 2 <-> 3 <-> NULL


Doubly Linked List (Backward): 3 <-> 2 <-> 1 <-> NULL
Doubly Linked List after insertion at the end (Forward): 1 <-> 2 <-> 3 <-> 4
<-> 5 <-> NULL
Doubly Linked List after deletion (Forward): 1 <-> 2 <-> 4 <-> 5 <-> NULL
5.(a) LINEAR SEARCH
#include <stdio.h>
int linearSearch(int arr[], int size, int key){
for (int i = 0; i < size; i++){
if (arr[i] == key){
return i;
}
}
return -1;
}
int main(){
int arr[] = {10, 20, 30, 40, 50, 60, 70};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 30;
int index = linearSearch(arr, size, key);
if (index != -1)
printf("Element %d found at index %d\n", key, index);
else
printf("Element %d not found in the array\n", key);
return 0;
}

OUTPUT:

Element 30 found at index 2


5.(b) BINARY SEARCH
#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x){
if (r >= l){
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int main(){
int arr[] = {20, 30, 40, 50, 60};
int size = sizeof(arr) / sizeof(arr[0]);
int x = 30;
int index = binarySearch(arr, 0, size - 1, x);
if (index == -1)
printf("Element is not present in array");
else
printf("Element is present at index %d", index);
return 0;
}

OUTPUT:

Element is present at index 1


6.(a) BASIC HASH FUNCTIONS
#include <stdio.h>
int main(){
// DIVISION METHOD
int size, i, indexes[5];
int keys[5] = {10, 22, 33, 44, 555};
printf("Enter the size of the Hash Table: ");
scanf("%d", &size);
int M = size;
for (i = 0; i < 5; i++){
indexes[i] = (keys[i] % M);
}
printf("\nThe indexes of the values in the Hash Table: ");
for (i = 0; i < 5; i++){
printf("%d ", indexes[i]);
}
return 0;
}

OUTPUT:
Enter the size of the Hash Table:
6.(b) COLLISION RESOLUTION USING LINEAR PROBING
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
struct HashTable {
int keys[SIZE];
int values[SIZE];
};
int hash(int key) {
return key % SIZE;
}
void initializeHashTable(struct HashTable* table) {
for (int i = 0; i < SIZE; i++) {
table->keys[i] = -1;
table->values[i] = -1;
}
}
void displayHashTable(struct HashTable table) {
printf("Hash Table:\n");
for (int i = 0; i < SIZE; i++) {
if (table.keys[i] != -1) {
printf("Key: %d, Value: %d\n", table.keys[i], table.values[i]);
} else {
printf("Slot %d: Empty\n", i);
}
}
printf("\n");
}
int insert(struct HashTable* table, int key, int value) {
int index = hash(key);
while (table->keys[index] != -1) {
index = (index + 1) % SIZE;
}
table->keys[index] = key;
table->values[index] = value;
return index;
}
int search(struct HashTable table, int key) {
int index = hash(key);
while (table.keys[index] != key) {
if (table.keys[index] == -1) {
return -1;
}
index = (index + 1) % SIZE;
}
return index;
}
int main() {
struct HashTable table;
initializeHashTable(&table);
insert(&table, 5, 50);
insert(&table, 25, 250);
insert(&table, 15, 150);
insert(&table, 35, 350);
displayHashTable(table);
int searchKey = 15;
int result = search(table, searchKey);
if (result != -1) {
printf("Key %d found at index %d\n", searchKey, result);
} else {
printf("Key %d not found\n", searchKey);
}
return 0;
}
OUTPUT:

The indexes of the values in the Hash Table: 10 22 33 44 555


7.(a) BUBBLE SORT
#include <stdio.h>
void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(arr, j, j+1);
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}

OUTPUT:
Sorted array:
11 12 22 25 34 64 90
7.(b) SELECTION SORT
#include <stdio.h>
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n) {
int i, j, min_idx;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}

OUTPUT:
Sorted array:
11 12 22 25 64
7.(c) INSERTION SORT
#include <stdio.h>
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);

return 0;
}

OUTPUT:

Sorted array:
5 6 11 12 13
7.(d) RADIX SORT
#include <stdio.h>
void countingSort(int array[], int size, int place) {
const int max = 10;
int output[size];
int count[max];
for (int i = 0; i < max; ++i)
count[i] = 0;
for (int i = 0; i < size; i++)
count[(array[i] / place) % 10]++;
for (int i = 1; i < max; i++)
count[i] += count[i - 1];
for (int i = size - 1; i >= 0; i--) {
output[count[(array[i] / place) % 10] - 1] = array[i];
count[(array[i] / place) % 10]--;
}
for (int i = 0; i < size; i++)
array[i] = output[i];
}
int getMax(int array[], int size) {
int max = array[0];
for (int i = 1; i < size; i++)
if (array[i] > max)
max = array[i];
return max;
}
void radixSort(int array[], int size) {
int max = getMax(array, size);
for (int place = 1; max / place > 0; place *= 10)
countingSort(array, size, place);
}
int main() {
int array[] = {170, 45, 75, 90, 802, 24, 2, 66};
int size = sizeof(array) / sizeof(array[0]);
radixSort(array, size);
printf("Sorted array: ");
for (int i = 0; i < size; i++)
printf("%d ", array[i]);

return 0;
}

OUTPUT: Sorted array: 2 24 45 66 75 90 170 802


7.(e) MERGE SORT
#include <stdio.h>
void merge(int arr[], int l, int m, int r) {
int i, j, k, n1 = m - l + 1, n2 = r – m, 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++;
}
}
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);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, arr_size - 1);
printf("Sorted array is : ");
for (int i = 0; i < arr_size; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
OUTPUT:Sorted array is : 5 6 7 11 12 13
7.(f) QUICK SORT
#include <stdio.h>
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
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);
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}

OUTPUT:

Sorted array:

11 12 22 25 64

You might also like