Showing posts with label Queue. Show all posts
Showing posts with label Queue. Show all posts

Write a program to Implement a Queue in C++

User is asked to Insert into.. Delete from or Display the entire queue..

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<stdio.h> 

  2. #include<conio.h> 

  3. #include<process.h> 

  4. int queue[5]; 

  5. long front,rear; 

  6. void initqueue(); 

  7. void display(); 

  8. void main() 


  9. int choice,info; 

  10. clrscr(); 

  11. while(1) 


  12. clrscr(); 

  13. printf(" MENU \n"); 

  14. printf("1.Insert an element in queue\n"); 

  15. printf("2.Delete an element from queue\n"); 

  16. printf("3.Display the queue\n"); 

  17. printf("4.Exit!\n"); 

  18. printf("Your choice: "); 

  19. scanf("%i",&choice); 

  20. //Coding by: Snehil Khanor  

  21. //http://WapCPP.blogspot.com  

  22. switch(choice) 


  23. case 1:if(rear<4) 


  24. printf("enter the number: "); 

  25. scanf("%d",&info); 

  26. if (front==-1) 


  27. front=0; 

  28. rear=0; 


  29. else 

  30. rear=rear+1; 

  31. queue[rear]=info; 


  32. else 

  33. printf("queue is full"); 

  34. getch(); 

  35. break; 

  36. case 2: int info; 

  37. if(front!=-1) 


  38. info=queue[front]; 

  39. if(front==rear) 


  40. front=-1; 

  41. rear=-1; 


  42. else 

  43. front=front+1; 

  44. printf("no deleted is = %d",info); 


  45. else 

  46. printf("queue is empty"); 

  47. getch(); 

  48. break; 

  49. case 3: display(); 

  50. getch(); 

  51. break; 

  52. case 4: exit(1); 

  53. break; 

  54. default:printf("You entered wrong choice!"); 

  55. getch(); 

  56. break; 




  57. void initqueue() 


  58. front=rear=-1; 


  59. void display() 


  60. int i; 

  61. for(i=front;i<=rear;i++) 

  62. printf("%i\n",queue[i]); 


Simple Function Pointers example

This example shows how function pointers work. There is also an example of how priority queues work.



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This program intents to show the use of function pointers
//This program also uses priority queues
#include<iostream>
#include<queue>

using namespace
std;

typedef
int (*fpRegCb)(int* i_p, int index);

int
funcA(int *val, int index)
{

cout<<"Func A: index = "<<index<<" val = "<<*val<<endl;
return
(*val + 10);
}


int
funcB(int *val, int index)
{

cout<<"Func B: index = "<<index<<" val = "<<*val<<endl;
return
(*val + 15);
}


class
someSimpleListType
{

private
:
someSimpleListType(); //This cant be used
public:
int
index;
int
*value;
fpRegCb callback;
someSimpleListType(fpRegCb cb, int *val, int idx):value(val),index(idx)
{

callback = cb;
};

//< operator is overloaded for use with priority queues. This will
//decide the priority of the new element, where it should be placed
const bool operator <(const someSimpleListType& sLT) const
{

return
(index < sLT.index);
}
};


priority_queue <someSimpleListType> someSimpleList;

int
main()
{

int
index=0;
int
*a=new int(22);
int
*b=new int(37);
int
*c=new int(62);

//The higher the priority, higher it is in the queue
someSimpleList.push(someSimpleListType(funcA, a, index++));
someSimpleList.push(someSimpleListType(funcB, b, index++));
someSimpleList.push(someSimpleListType(funcB, c, index++));
//The last push will end up top of queue as index(priority) is greatest

//Lets get all the elements from the list and process them
cout<<"Test 1"<<endl;
while
(!someSimpleList.empty())
{

someSimpleListType &sLT = someSimpleList.top();
cout<<"Callback["<<sLT.index<<"] = "<<sLT.callback(sLT.value,sLT.index)<<endl;
someSimpleList.pop();
}


//Since the priority queue is now empty, push some more elements
someSimpleList.push(someSimpleListType(funcA, a, index++));
someSimpleList.push(someSimpleListType(funcB, b, index++));
someSimpleList.push(someSimpleListType(funcB, c, index++));

//This time we will only process funcB and ignore funcA
cout<<endl<<"Test 2"<<endl;
while
(!someSimpleList.empty())
{

someSimpleListType &sLT = someSimpleList.top();
if
(sLT.callback == funcB)
{

cout<<"Callback["<<sLT.index<<"] = "<<sLT.callback(sLT.value,sLT.index)<<endl;
}

someSimpleList.pop();
}


return
0;
}


Check out this stream