DSC314 Data Structure Lab3
DSC314 Data Structure Lab3
#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");
13.Priority queue
#include <stdio.h>
int heap[40];
int size = -1;
// 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 (largest != i) {
int temp = heap[i];
heap[i] = heap[largest];
heap[largest] = temp;
moveDown(largest);
}
}
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;
}