fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct pelement
  5. {
  6. pelement *nast;
  7. int w;
  8. };
  9.  
  10. struct kolejka
  11. {
  12. pelement *pierwszy;
  13. pelement *ostatni;
  14. };
  15.  
  16. void init(struct kolejka* k)
  17. {
  18. k->pierwszy=0;
  19. k->ostatni=0;
  20. }
  21.  
  22. bool empty(struct kolejka *k)
  23. {
  24. if(k->pierwszy == NULL)
  25. return true;
  26. return false;
  27. }
  28.  
  29. void insert(struct kolejka* &k, int x)
  30. {
  31. struct pelement *nowy;
  32. nowy = new pelement;
  33. nowy->w=x;
  34. nowy->nast=NULL;
  35. if(empty(k))
  36. {
  37. k->ostatni=nowy;
  38. k->pierwszy=nowy;
  39. }
  40. else
  41. {
  42. k->ostatni->nast=nowy;
  43. k->ostatni=nowy;
  44. }
  45.  
  46. }
  47. int pop(struct kolejka* &k)
  48. {
  49. int x;
  50. if(empty(k))
  51. return 0;
  52. pelement *old;
  53. old = new pelement;
  54. old=k->pierwszy;
  55. x=old->w;
  56. k->pierwszy=old->nast;
  57. delete(old);
  58. return x;
  59. }
  60.  
  61. int main()
  62. {
  63. kolejka *k1= new kolejka;
  64.  
  65. init(k1);
  66. insert(k1,3);
  67. insert(k1,4);
  68. insert(k1,5);
  69.  
  70. cout<<pop(k1);
  71. cout<<endl;
  72. cout<<pop(k1);
  73. cout<<endl;
  74. cout<<pop(k1);
  75. cout<<endl;
  76. cout<<pop(k1);
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0.02s 2856KB
stdin
Standard input is empty
stdout
3
4
5
0