Add to Favorites    Make Home Page 1763 Online  
 Language Categories  
 Our Services  

Home » C Home » Data Structures Home » Program to demonstrate the working of Priority Queue

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Program to demonstrate the working of Priority Queue
Author Dilip Savant
Author Email Dilip_SWAT_Officer [at] yahoo.com
Description
Category C » Data Structures
Hits 387402
Code Select and Copy the Code
Code : /*((((((((((((((((((((( Priority Queue Operations )))))))))))))))))))))))*/ #include<stdio.h> #include<conio.h> #include<alloc.h> #include<dos.h> void Insert_into_Priority_Queue(int choice1); void Delete_from_Priority_Queue(void); void Display_Priority_Queue(void); struct node { int Value; struct node *Next; } *Block,*Front,*Rear,*Travel,*Previous; void main() { int choice1,choice2,size,SizeCount=0; clrscr(); textcolor(10); cprintf("Specify the size for your Priority Queue : "); scanf("%d",&size); clrscr(); cprintf(" Allocating Memory........... Please Wait !!!"); sleep(2); clrscr(); if(size>0) { do { cprintf("*---------------------------* "); cprintf("* Main Menu * "); cprintf("*---------------------------* "); cprintf("* (1). Ascending Priority. * "); cprintf("* (2). Descending Priority. * "); cprintf("* (3). Quit. * "); cprintf("*---------------------------* "); cprintf(" Your Wish : "); scanf("%d",&choice1); if((choice1==1)||(choice1==2)) { do { clrscr(); cprintf("*--------------------------* "); cprintf("* Operations * "); cprintf("*--------------------------* "); cprintf("* (1). Insert * "); cprintf("* (2). Delete * "); cprintf("* (3). Display * "); cprintf("* (4). Back to Main Menu. * "); cprintf("*--------------------------* "); cprintf(" Your Wish : "); cscanf("%d",&choice2); switch(choice2) { case 1 : if(SizeCount<0) SizeCount=0; if(SizeCount<size) { Insert_into_Priority_Queue(choice1); SizeCount++; } else { getch(); cprintf(" Priority Queue is Full !!! "); getch(); } clrscr(); break; case 2 : Delete_from_Priority_Queue(); if(SizeCount<0) SizeCount=0; else SizeCount--; clrscr(); break; case 3 : Display_Priority_Queue(); break; case 4 : if((Front!=NULL)&&(Rear!=NULL)) { //Let's clear Memory Contents, first...... while(Front!=NULL) { Travel=Front; Front=Front->Next; Travel->Next=NULL; free(Travel); } Rear=NULL; SizeCount=0; } /* If you forget to clear previously utilized memory, Then above part will do the job for you. */ clrscr(); break; default : cprintf(" Wrong Selection !!!"); getch(); clrscr(); break; } } while(choice2!=4); } else { if(choice1==3) { if((Front!=NULL)&&(Rear!=NULL)) { //Let's clear Memory Contents, first...... while(Front!=NULL) { Travel=Front; Front=Front->Next; Travel->Next=NULL; free(Travel); } Rear=NULL; SizeCount=0; } /* If you forget to clear previously utilized memory, Then above part will do the job for you. */ sleep(1); cprintf(" Releasing Allocated Memory....... "); sleep(1); cprintf(" Testing Memory Space....... "); sleep(1); cprintf(" Memory Test....... O.K. "); break; } else { cprintf(" Wrong Selection !!!"); getch(); clrscr(); } } } while(choice1!=3); } else { cprintf(" Unable to allocate Memory !!!"); getch(); clrscr(); } getch(); textcolor(YELLOW+BLINK); cprintf(" Thank you for using my application !!!"); getch(); } /*======================================================================== * Procedure : Insert an item or Data into the Queue,either - By Ascending Priority or - By Descending Priority. *========================================================================* / void Insert_into_Priority_Queue(int choice1) { Block=(node *)malloc(sizeof(node *)); cprintf(" Give the Data to be inserted : "); scanf("%d",&Block->Value); Block->Next=NULL; if((Front==NULL)&&(Rear==NULL)) //No memory utilization for Priority Queue. { Front=Block; //Start Memory utilization for Priority Queue. Rear=Block; Front->Next=Rear->Next=NULL; cprintf(" %d has been inserted !!! ",Block->Value); sleep(1); cprintf(" It is first time you are inserting the Data !!!"); getch(); } else { switch(choice1) { /* Insert Data into the Queue by Ascending Priority. */ case 1 : if(Block->Value<Front->Value) { Block->Next=Front; cprintf(" %d has been inserted before %d ",Block->Value,Front->Value); sleep(1); cprintf(" Now, %d is your starting Data",Block->Value); getch(); Front=Block; } else { Travel=Front; while(Block->Value>=Travel->Value) { Previous=Travel; Travel=Travel->Next; if(Travel==NULL) break; } Previous->Next=Block; Block->Next=Travel; cprintf(" %d has been inserted after %d",Block->Value,Previous->Value); getch(); } //Now, adjust the position of the "Rear". Travel=Front; while(Travel!=NULL) { Previous=Travel; Travel=Travel->Next; } Rear=Previous; // Force "Rear" to point towards the end of Priority Queue. break; /* Insert Data into the Queue by Descending Priority. */ case 2 : if(Block->Value>Front->Value) { Block->Next=Front; cprintf(" %d has been inserted before %d ",Block->Value,Front->Value); sleep(1); cprintf(" Now, %d is your starting Data",Block->Value); getch(); Front=Block; } else { Travel=Front; while(Block->Value<=Travel->Value) { Previous=Travel; Travel=Travel->Next; if(Travel==NULL) break; } Previous->Next=Block; Block->Next=Travel; cprintf(" %d has been inserted after %d",Block->Value,Previous->Value); getch(); } //Now, adjust the position of the "Rear". Travel=Front; while(Travel!=NULL) { Previous=Travel; Travel=Travel->Next; } Rear=Previous; // Force "Rear" to point towards the end of Priority Queue. break; default : cprintf(" Wrong Selection !!!"); getch(); clrscr(); break; } } getch(); clrscr(); } /*======================================================================== * Procedure : Delete an item or Data from the Priority Queue. *========================================================================* / void Delete_from_Priority_Queue(void) { if((Front==NULL)&&(Rear==NULL)) //Nothing has been stored in the Queue. { cprintf(" These is nothing to delete from the Priority Queue !!!"); getch(); } else { Travel=Front; if(Travel==Rear) /* If "Front" & "Rear", both are pointing to the same memory block......... */ { Front=NULL; Rear=NULL; Travel->Next=NULL; cprintf(" %d has been erased from the Priority Queue !!!",Travel->Value); sleep(1); cprintf(" Now Queue is empty !!!"); getch(); } else { Front=Front->Next; Travel->Next=NULL; cprintf(" %d has been erased from the Queue !!!",Travel->Value); getch(); } free(Travel); //The block pointed by "Front" has been erased. } getch(); clrscr(); } /*======================================================================== * Procedure : Display an item or Data from the Priority Queue. *========================================================================* / void Display_Priority_Queue(void) { if((Front==NULL)&&(Rear==NULL)) { cprintf(" These is nothing to display from the Priority Queue !!!"); getch(); } else { Travel=Front; while(Travel!=NULL) { /* If Front & Rear, both are pointing to the same memory block. */ if((Travel==Front)&&(Travel==Rear)) { cprintf(" Front ==> | %d | <== Rear",Travel->Value); Travel=Travel->Next; } else { if(Travel==Front) //Memory Block, where Front points.... cprintf(" Front ==> | %d |",Travel->Value); else { if(Travel==Rear) //Memory Block, where Rear points.... cprintf(" %d | <== REAR",Travel->Value); else cprintf(" %d |",Travel->Value); } Travel=Travel->Next; } } getch(); } getch(); clrscr(); }

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi �դ ?�� ��??���
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1072


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review