Computer Science, asked by roshu2002, 1 year ago

c program of priority queue using linked list

Answers

Answered by vikashvarun22pdx5m5
2
#include <stdio.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 form 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

int main()

{

    // 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);

    }

  

    return 0;

}


vikashvarun22pdx5m5: Algorithm :
PUSH(HEAD, DATA, PRIORITY)
Step 1: Create new node with DATA and PRIORITY
Step 2: Check if HEAD has lower priority. If true follow Steps 3-4 and end. Else goto Step 5.
Step 3: NEW -> NEXT = HEAD
Step 4: HEAD = NEW
Step 5: Set TEMP to head of the list
Step 6: While TEMP -> NEXT != NULL and TEMP -> NEXT -> PRIORITY > PRIORITY
vikashvarun22pdx5m5: Step 7: TEMP = TEMP -> NEXT
[END OF LOOP]
Step 8: NEW -> NEXT = TEMP -> NEXT
Step 9: TEMP -> NEXT = NEW
Step 10: End
vikashvarun22pdx5m5: POP(HEAD)
Step 2: Set the head of the list to the next node in the list. HEAD = HEAD -> NEXT.
Step 3: Free the node at the head of the list
Step 4: End
vikashvarun22pdx5m5: PEEK(HEAD):
Step 1: Return HEAD -> DATA
Step 2: End
roshu2002: mla only 1 algorithm pahije ...tyat sarv aale pahije ....ashe different nako
vikashvarun22pdx5m5: the first 10 is algorithm
roshu2002: hello nd what about flowchart
vikashvarun22pdx5m5: pop and peek are the actions performed you should also know that
vikashvarun22pdx5m5: can you message me
roshu2002: yes.
Similar questions