March 12, 2014

Priority Queue Implementation in C using Arrays

#include <stdio.h>

#include <conio.h>

#define size 5

int queue[5][2] = {0};

int top = -1;

int bottom;

void push(int value, int pr)

{

int i,j,k;

if(top < size-1)

{

if(queue[top][1] > pr)

{

for(i=0;i<top;i++)

{

if(queue[i][1] > pr)

{

break;

}

}

for(j=top;j>=i;j--)

{

queue[j+1][0] = queue[j][0];

queue[j+1][1] = queue[j][1];

}

top++;

queue[i][0] = value;

queue[i][1] = pr;

}

else

{

top++;

queue[top][0] = value;

queue[top][1] = pr;

}

}

else

{

printf("queue overflow \n");

}

}

void pop()

{

int i;

if(queue[0][0] == 0)

{

printf("\n The queue is empty  \n");

}

else

{

printf("After , dequeue the following value is erased \n  %d \n", queue[0][0]);

for(i=0;i<top;i++)

{

queue[i][0] = queue[i+1][0];

queue[i][1] = queue[i+1][1];

}

queue[top][0] = 0;

queue[top][1] = 0;

top--;

}

}

void display()

{ int i,j;

printf("Element\tPriority \n");

for(i=size - 1;i>=0;i--)

{

for(j=0;j<2;j++)

{

printf(" %d\t",queue[i][j]);

}

printf("\n");

}

}

int main()

{

int i,j, ch=0 ,value = 0,pr=0;

while(1)

{

printf("\n Please Enter the choice. \n");

printf("1 for Enqueue \n 2 for Dequeue \n 3 for display\n  5 for exit: \t \n");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("\n Please Enter the number to be inserted: \t ");

scanf("%d", &value);

printf("\n Please Enter the priority: \t ");

scanf("%d", &pr);

push(value,pr);

break;

case 2:

pop();

break;

case 3:

display();

break;

case 5:

exit(0);

default:

printf("You entered wrong choice\n");

}

}

}

13 comments:

  1. this was good :) www.eshopori.com thank you

    ReplyDelete
  2. Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.

    Hadoop Training in Chennai

    ReplyDelete
  3. excellent ,very deeply explained, i enjoyed reading this articles keep updating more...

    Oracle Apps Finance training in chennai

    ReplyDelete

  4. नोटबंदी के 50वें दिन, मोदी सरकार के फैसले से निराश हैं जनता
    Readmore todaynews18.com https://goo.gl/NJUQY9

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. this post is very help full all for students...
    Date Sheet

    ReplyDelete

  7. Wow that was really a great read indeed. Expecting more such blogs in near future. Thanks for sharing.
    Please help me suggest
    The Best School in Perambur
    The Best School in Vyasarpadi

    ReplyDelete
  8. We at COEPD provides the best web Designing course in Hyderabad. Your search to learn web designing technology ends here at COEPD. Here, we are an established training institute who have trained more than 10,000 participants in all streams. We will help you to convert your passion to learn into an enriched learning process. At COEPD, we provide finest web designing technology covering Photoshop, HTML, CSS, HTML5, CSS3, JavaScript, Boot Strap, and JQuery. We train participants to be solution providers and creative engineers.

    http://www.coepd.com/WebDesigning.html

    ReplyDelete
  9. Locuciones Baratas a un nivel profesional solo en Locuciones baratos. Las mejores locuciones baratas a la mejor calidad precio. No lo dudes. Solo en locucionesbaratos.com

    ReplyDelete
  10. this is great opportunity to start most profitable business and get extra income get Small Business Ideas



    ReplyDelete
  11. ฝาก20รับ100 pg ล่าสุด การฝากถอนในเกมสล็อต PG SLOT เป็นกระบวนการที่ง่ายสะดวกสบายทำให้ผู้เล่นสามารถเข้ามาเล่นเกมได้โดยไม่มีความยุ่งยากมากมาย นี้คือขั้นตอนที่ทำให้การฝากถอนใน PG Slot

    ReplyDelete

C program to Read From a File

#include <stdio.h> #include <stdlib.h> void main() {     FILE *fptr;     char filename[15];     char ch;   ...