DS-Record-mca-06) write a program for implementing the operations of a priority queue using dynamic allocation.

 1)AIM:-

write a program for implementing the operations of a priority queue using dynamic allocation.
code:-
// C code to implement Priority Queue 
// using Linked List 
#include <stdio.h> 
#include <conio.h>
#include <stdlib.h> 
// Node 
typedef struct node { 
    int data; 
    // Lower values indicate higher priority 
    int priority; 
    struct node* next; 
} Node; 
// Function to Create A New Node 
Node* newNode(int d, int p) 
{ 
    Node* temp = (Node*)malloc(sizeof(Node)); 
    temp->data = d; 
    temp->priority = p; 
    temp->next = NULL; 
    return temp; 
} 
// Return the value at head 
int peek(Node** head) 
{ 
    return (*head)->data; 
} 
// Removes the element with the 
// highest priority from the list 
void pop(Node** head) 
{ 
    Node* temp = *head; 
    (*head) = (*head)->next; 
    free(temp); 
} 
// Function to push according to priority 
void push(Node** head, int d, int p) 
{ 
    Node* start = (*head); 
    // Create new Node 
    Node* temp = newNode(d, p); 
    // Special Case: The head of list has lesser 
    // priority than new node. So insert new 
    // node before head node and change head node. 
    if ((*head)->priority > p) { 
        // Insert New Node before head 
        temp->next = *head; 
        (*head) = temp; 
    } 
    else { 
        // Traverse the list and find a 
        // position to insert new node 
        while (start->next != NULL && 
            start->next->priority < p) { 
            start = start->next; 
        } 
        // Either at the ends of the list 
        // or at required position 
        temp->next = start->next; 
        start->next = temp; 
    } 
} 
// Function to check is list is empty 
int isEmpty(Node** head) 
{ 
    return (*head) == NULL; 
} 
// Driver code 
void main() 
{ 
    clrscr();
    // Create a Priority Queue 
    // 7->4->5->6 
    Node* pq = newNode(4, 1); 
    push(&pq, 5, 2); 
    push(&pq, 6, 3); 
    push(&pq, 7, 0); 
    while (!isEmpty(&pq)) { 
        printf("%d ", peek(&pq)); 
        pop(&pq); 
    } 
    getch(); 
}

Output:-


Comments

Popular posts from this blog

digital marketing ppt-u1

SOFTWARE

cn lab

Computer Operations and Performing - D L Unit-1-1

DS-Record-mca-04) write a program for evaluating a given postfix expression using stack.

DBMS Degree Lab Records

Unit 2: Foundations of Ownership, Security Related Concepts in Blockchain

Unit-1 Foundations of Software Systems and Blockchain

Access the Internet to Browse Infromation & E-Mail Operation- D L Unit-2-1

6)what are the various service of internet and protocols ICT-unit-1