Facebook
From Stained Hamster, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 241
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. struct node
  7. {
  8.         int val;
  9.         node *next;
  10. };
  11.  
  12. void enqueue(node *&Q, node *&t, int x)           //dodawanie
  13. {
  14.     node *p=new node;
  15.     p->val=x;
  16.     p->next=NULL;
  17.     if (Q==NULL)
  18.       {
  19.         Q=p;
  20.       }
  21.     else
  22.       {
  23.         t->next=p;  
  24.       }
  25.     t=p;
  26. }
  27.  
  28.  
  29. void dequeue(node *&Q, node *&t)          //usuwanie
  30. {
  31.     if (Q !=NULL)
  32.     {
  33.         node *p=Q;
  34.         Q=Q->next;   //alt wersja: Q=p->next
  35.         delete p;
  36.        
  37.     }
  38.     if (Q==NULL)
  39.     {
  40.         t=NULL;
  41.     }
  42. }
  43.  
  44.  
  45. bool isEmpty (node *Q)
  46. {
  47.     if(Q==NULL)return true;
  48.     else return false;
  49. }
  50.  
  51. void showfront(node *Q)
  52. {
  53.    
  54.     if (Q !=NULL)
  55.     {
  56.         node *p=Q;
  57.         cout<<"->";
  58.      cout<<p->val;
  59.     }    
  60.    
  61. }
  62.  
  63.  
  64. void showback(node *t)
  65. {
  66.     if (t !=NULL)
  67.     {
  68.         node *p=t;
  69.         cout<<"->";
  70.      cout<<p->val;
  71.     }    
  72. }
  73.  
  74. int main()
  75. {
  76.    
  77.     node *Q=NULL;
  78.     node *t=NULL;
  79.  
  80.         enqueue(Q,t, 5);       
  81.         enqueue(Q,t, 22);
  82.         showfront(Q);
  83.         dequeue(Q,t);
  84.         showfront(Q);
  85.  
  86.  
  87.     return 0;
  88. }
  89.  
  90.  
  91. ///ZAD DOM 1: co drugi element ze stosu przerzuć na kolejkę;
  92. ///ZAD DOM 2; co drugi element z kolejki przerzuć na stos;