Filage

Lab5_1

Mar 19th, 2024
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node {
  6.     node* next;
  7.     int info;
  8. };
  9.  
  10. void push(node*& Begin, node*& End) {
  11.     node* el = new node;
  12.     cout << "Значение: ";
  13.     cin >> el->info;
  14.     el->next = NULL;
  15.     if (End == nullptr) {
  16.         Begin = End = el;
  17.     }
  18.     else {
  19.         End = el;
  20.     }
  21. }
  22.  
  23.  
  24. bool isEmpty(node* Begin) {
  25.     if (Begin == nullptr)
  26.         cout << "Очередь пуста\n";
  27.     return Begin == nullptr;
  28. }
  29.  
  30. void queueList(node* Begin) {
  31.     if (isEmpty(Begin))
  32.         return;
  33.     node* temp = Begin;
  34.     while (temp != nullptr) {
  35.         cout << temp->info << " ";
  36.         temp = temp->next;
  37.     }
  38. }
  39.  
  40. void pop(node*& Begin) {
  41.     if (isEmpty(Begin))
  42.         return;
  43.     node* temp = Begin;
  44.     temp = Begin;
  45.     Begin = Begin->next;
  46.     delete temp;
  47. }
  48.  
  49. int peek(node* Begin) {
  50.     if (isEmpty(Begin))
  51.         return -1;
  52.     return Begin->info;
  53. }
  54.  
  55.  
  56. int main() {
  57.     setlocale(LC_ALL, "Rus");
  58.     node* Begin = nullptr, * End = nullptr;
  59.     push(Begin, End);
  60.     push(Begin, End);
  61.     push(Begin, End);
  62.     queueList(Begin);
  63.     pop(Begin);
  64.     cout << endl;
  65.     queueList(Begin);
  66.     cout << endl;
  67.     int num = peek(Begin);
  68.     cout << num;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment