Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- //дефиниране и инициализация на опашка
- struct elem
- {
- int key;
- elem *next;
- } *first = NULL, *last = NULL, *p;
- void push(int n); //prototype
- int pop(int &n); //prototype
- void push(elem *&f, elem *&l, int n); //prototype
- int pop(elem *&f, elem *&l, int &n); //prototype
- void push(int n) //добавяне на елемент
- {
- p = last;
- last = new elem;
- last->key = n;
- last->next = NULL;
- if(p != NULL)
- p->next = last;
- if (first == NULL) //добавяне на първи елемент
- {
- first = last;
- }
- }
- int pop(int &n) //извличане на елемент
- {
- if (first) //проверка за непразна опашка
- {
- n = first->key;
- p = first;
- first = first->next;
- if (first == NULL)
- last = first;
- delete p; //премахване на елемента
- return 1;
- }
- else
- return 0; //опашката е празна
- }
- void push(elem *&f, elem *&l, int n) //добавяне на елемент
- {
- elem *p = l;
- l = new elem;
- l->key = n;
- l->next = NULL;
- if(p != NULL)
- p->next = l;
- if (f == NULL) //добавяне на първи елемент
- {
- f = l;
- }
- }
- int pop(elem *&f, elem *&l, int &n) //извличане на елемент
- {
- elem *p;
- if (f) //проверка за непразна опашка
- {
- n = f->key;
- p = f;
- f = f->next;
- if (f == NULL)
- l = f;
- delete p; //премахване на елемента
- return 1;
- }
- else
- return 0; //опашката е празна
- }
- int main()
- {
- int num;
- cout<<"Input integers:\n";
- while (cin >> num)
- push(num);
- cout<<"\nBuffered data: ";
- while (pop(num))
- {
- cout << num << " ";
- }
- cout << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement