0% found this document useful (0 votes)
19 views6 pages

DSC314 Data Structure Lab3

Uploaded by

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

DSC314 Data Structure Lab3

Uploaded by

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

12 Queue implementation using array

#include <stdio.h>
#define QUEUESIZE 100

struct queue {
int a[QUEUESIZE];
int front;
int back;
};
struct queue q;

void enqueue(void);
int dequeue(void);
void display(void);

void main() {
int choice;
int option = 1;
q.front = -1;
q.back = -1;

printf("QUEUE OPERATION\n");
while (option) {
printf("------------------------------------------\n");
printf(" 1 --> ENQUEUE \n");
printf(" 2 --> DEQUEUE \n");
printf(" 3 --> DISPLAY \n");
printf(" 4 --> EXIT \n");
printf("------------------------------------------\n");

printf("Enter your choice\n");


scanf("%d", &choice);
switch (choice) {
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
return;
}

printf("Do you want to continue(Type 0 (no) or 1 (yes))?\n");


scanf("%d", &option);
}
}

/* Function to add an element to the queue */


void enqueue() {
int num;
if (q.back == (QUEUESIZE - 1)) {
printf("Queue is Full\n");
return;
} else {
printf("Enter the element to be enqueued\n");
scanf("%d", &num);
if (q.front == -1) {
q.front = 0; // Set front to 0 if it's the first element
}
q.back = q.back + 1;
q.a[q.back] = num;
}
return;
}

/* Function to delete an element from the queue */


int dequeue() {
int num;
if (q.front == -1 || q.front > q.back) {
printf("Queue is Empty\n");
return -1;
} else {
num = q.a[q.front];
printf("Dequeued element is = %d\n", q.a[q.front]);
q.front = q.front + 1;
if (q.front > q.back) {
q.front = q.back = -1; // Reset the queue if it's empty
}
}
return num;
}

/* Function to display the status of the queue */


void display() {
int i;
if (q.front == -1) {
printf("Queue is empty\n");
return;
} else {
printf("\nThe status of the queue is \n");
for (i = q.front; i <= q.back; i++) {
printf("%d\n", q.a[i]);
}
}
printf("\n");
}

13.Priority queue

priority queue using max heap https://www.javatpoint.com/ds-priority-queue

#include <stdio.h>

int heap[40];
int size = -1;

// retrieving the parent node of the child node


int parent(int i) {
return (i - 1) / 2;
}

// retrieving the left child of the parent node


int left_child(int i) {
return 2 * i + 1;
}

// retrieving the right child of the parent


int right_child(int i) {
return 2 * i + 2;
}

// Returning the element having the highest priority


int get_Max() {
return heap[0];
}

// Returning the element having the minimum priority


int get_Min() {
return heap[size];
}

// function to move the node up the tree in order to restore the heap property
void moveUp(int i) {
while (i > 0 && heap[parent(i)] < heap[i]) {
int temp = heap[parent(i)];
heap[parent(i)] = heap[i];
heap[i] = temp;
i = parent(i);
}
}
// function to move the node down the tree in order to restore the heap property
void moveDown(int i) {
int largest = i;
int left = left_child(i);
int right = right_child(i);

if (left <= size && heap[left] > heap[largest]) {


largest = left;
}

if (right <= size && heap[right] > heap[largest]) {


largest = right;
}

if (largest != i) {
int temp = heap[i];
heap[i] = heap[largest];
heap[largest] = temp;
moveDown(largest);
}
}

// Removing the element of maximum priority


void removeMax() {
if (size >= 0) {
int r = heap[0];
heap[0] = heap[size];
size--;
moveDown(0);
}
}

// inserting the element in a priority queue


void insert(int p) {
size = size + 1;
heap[size] = p;

// move Up to maintain heap property


moveUp(size);
}

// Removing the element from the priority queue at a given index i


void delete(int i) {
heap[i] = heap[0] + 1;

// move the node stored at ith location to the root node


moveUp(i);

// Removing the node having maximum priority


removeMax();
}
// Display the elements in the priority queue
void display() {
printf("Elements in the priority queue are: ");
for (int i = 0; i <= size; i++) {
printf("%d ", heap[i]);
}
printf("\n");
}

int main() {
int choice, element, index;

do {
printf("\nPriority Queue Menu\n");
printf("1. Insert an element\n");
printf("2. Delete an element by index\n");
printf("3. Get maximum element\n");
printf("4. Get minimum element\n");
printf("5. Display priority queue\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to insert: ");
scanf("%d", &element);
insert(element);
break;
case 2:
printf("Enter the index of the element to delete: ");
scanf("%d", &index);
if (index >= 0 && index <= size) {
printf("%dth element deleted",index);
delete(index);
} else {
printf("Invalid index!\n");
}
break;
case 3:
printf("The element with the highest priority is: %d\n", get_Max());
break;
case 4:
printf("The element with the lowest priority is: %d\n", get_Min());
break;
case 5:
display();
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 6);

return 0;
}

You might also like