™[]Sumber Pembuat,Author,Created,dll sudah ada Di dalam Rarnya[]™

™[]Klik Judul postingan untuk melihat Isi selengkapnya!![]™

Kamis, 17 November 2011

0 Program Queue C++

  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<process.h>
  6. #define maxsize 5
  7.  
  8. struct que
  9. {
  10. int items[maxsize];
  11. int rear , front;
  12. };
  13.  
  14. void insert(struct que *,int);
  15. int remove(struct que *) ;
  16. int empty (struct que *);
  17. void makenull(struct que *);
  18. void front(struct que *);
  19.  
  20.  
  21. /****************** insert function **********************/
  22.  
  23. void insert(struct que *q,int a)
  24.  
  25. {
  26.  
  27. if(q->rear==maxsize-1)
  28.  
  29. q->rear=0;
  30.  
  31. else
  32.  
  33. (q->rear)++ ;
  34.  
  35. if(q->rear==q->front)
  36. {
  37.  
  38. cout<<endl<<"OVERFLOW" <<endl ;
  39. //exit(1);
  40. }
  41.  
  42. q->items[q->rear]=a ;
  43.  
  44.  
  45. }
  46.  
  47. /*************** remove function ********************/
  48.  
  49. int remove(struct que *q)
  50. {
  51.  
  52. if(empty(q))
  53.  
  54. {
  55. cout<<endl<<" UNDERFLOW"<<endl;
  56.  
  57. }
  58.  
  59. if(q->front==maxsize-1)
  60.  
  61. q->front =0;
  62.  
  63. else
  64. {
  65. (q->front)++;
  66. }
  67. return (q->items[q->front]);
  68.  
  69. q->rear=q->front=maxsize-1;
  70.  
  71. }
  72.  
  73.  
  74. /********************** empty function ************************/
  75.  
  76. int empty(struct que *q)
  77. {
  78.  
  79.  
  80. if(q->front==q->rear)
  81.  
  82. return (1);
  83.  
  84. else
  85.  
  86. return (0);
  87.  
  88. }
  89. /********************* front function ******************************/
  90. //returns the 1st element in queue
  91. void front(struct que *q)
  92.  
  93. {
  94. int a;
  95.  
  96. int first_val;
  97.  
  98.  
  99. first_val=q->items[q->front];
  100.  
  101. cout<<endl<<"FIRST ELEMENT IS :" <<first_val;
  102. }
  103.  
  104. /******************** makenull function ***************************/
  105.  
  106. void makenull(struct que *q)
  107.  
  108. {
  109. if(empty(q))
  110.  
  111. {
  112.  
  113. cout<<"THE QUEUE IS EMPTY";
  114. }
  115.  
  116. else
  117.  
  118. {
  119.  
  120. q->rear=q->front=maxsize-1;
  121.  
  122. cout<<endl<<"QUEUE Is EMPTY "<<endl ;
  123. }
  124.  
  125. }
  126.  
  127.  
  128. /****************** main function *****************************/
  129.  
  130. void main ()
  131.  
  132. {
  133.  
  134. int a;
  135. clrscr();
  136. struct que *q;
  137.  
  138.  
  139. for(int i=0;i<maxsize;i++)
  140. q->items[i]=0;
  141.  
  142. char choice;
  143. q->rear=q->front=maxsize-1;
  144. cout<<endl<<"******************* IMPLIMENTATION OF QUEUES******************" <<endl<<endl;
  145.  
  146. do
  147.  
  148.  
  149. { //clrscr();
  150.  
  151.  
  152. cout<<endl<<endl<<" 1 : Insert "<<endl<<" 2 : Remove "<<endl<<" 3 : check empty : 0 shows not empty & 1 shows empty " <<endl<<" 4 : Makenull"<<endl<<" 5 : Front"<<endl;
  153. cout<< "enter ur choice : " ;
  154.  
  155. choice=getche();
  156.  
  157. switch(choice)
  158.  
  159. {
  160.  
  161. case '1' :
  162.  
  163. {
  164.  
  165. cout<<endl<<"ENTER THE VALUE : ";
  166. cin>>a;
  167.  
  168. insert(q,a);
  169.  
  170. for(int i=0; i<=maxsize-2;i++)
  171.  
  172. cout<<q->items[i]<<" ";
  173.  
  174. break;
  175.  
  176. }
  177.  
  178. case '2' :
  179.  
  180. {
  181. cout<<endl<<"THE VALUE IS: ";
  182.  
  183. cout<<remove(q)<<endl;
  184.  
  185. for(int i=maxsize-1;i>0;i--)
  186.  
  187. cout<<q->items[i]<<" ";
  188.  
  189. break;
  190.  
  191. }
  192.  
  193.  
  194.  
  195. case '3' :
  196.  
  197. {
  198.  
  199. cout<<endl<<empty(q);
  200.  
  201. break;
  202.  
  203. }
  204.  
  205. case '4' :
  206.  
  207. {
  208.  
  209. makenull(q);
  210.  
  211. for(int i=0;i<maxsize;i++)
  212. {
  213.  
  214. q->items[i]='\0';
  215.  
  216. cout<<q->items[i]<<endl;
  217.  
  218. }
  219.  
  220. break;
  221.  
  222. }
  223.  
  224. case '5':
  225.  
  226. {
  227.  
  228. front(q);
  229.  
  230. break;
  231. }
  232.  
  233.  
  234. default :
  235.  
  236.  
  237.  
  238. cout<<" wrong choice ";
  239. break;
  240.  
  241. }//end of switch
  242.  
  243. cout<<endl<<"Do u want to exit y/n :" ;
  244.  
  245. } //end do while
  246.  
  247. while((choice=getche())!='y');
  248.  
  249. }//end main

0 komentar:

Posting Komentar