gashink_t

саод лаб 5 часть 2

Mar 11th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #define N 10
  4.  
  5. struct Deque {
  6.     int data[N];
  7.     int last;
  8. };
  9. typedef struct Deque Deque;
  10. void Creation(Deque *D);
  11. void AddL(Deque *D);
  12. void AddR(Deque *D);
  13. void DeleteL(Deque *D);
  14. void DeleteR(Deque *D);
  15. int OutputL(Deque *D);
  16. int OutputR(Deque *D);
  17. int Size(Deque *D);
  18.  
  19. int main() {
  20.     Deque d;
  21.     int a=1;
  22.     Creation(&d);
  23.     while (a==1) {
  24.         cout << "adding to the beginning: " << endl;
  25.         AddL(&d);
  26.         cout << "Add more? 1(yes)/2(no)" << endl;
  27.         cin >> a;
  28.     }
  29.     a=1;
  30.     while (a==1) {
  31.         cout << "adding to the end: " << endl;
  32.         AddR(&d);
  33.         cout << "Add more? 1(yes)/2(no)" << endl;
  34.         cin >> a;
  35.     }
  36.     cout << "output of the first element: " << endl;
  37.     a = OutputL(&d);
  38.     cout << a << endl;
  39.     cout << "output of the last element: " << endl;
  40.     a = OutputR(&d);
  41.     cout << a << endl;
  42.     cout << "deleting the first element: " << endl;
  43.     DeleteL(&d);
  44.     cout << "output of the first element: " << endl;
  45.     a = OutputL(&d);
  46.     cout << a << endl;
  47.     cout << "deleting the last element: " << endl;
  48.     DeleteR(&d);
  49.     cout << "output of the last element: " << endl;
  50.     a = OutputR(&d);
  51.     cout << a << endl;
  52.     int s=Size(&d);
  53.     cout << "the size of the deck: " << s << endl;
  54. }
  55.  
  56. void Creation(Deque *D) {
  57.     D->last=0; }
  58.     bool Full(Deque *D) {
  59.     if (D->last==0)
  60.         return true;
  61.     else return false;
  62. }
  63. void AddL(Deque *D) { //добавление в начало
  64.     if (D->last==N)
  65.         cout<<"\nДек заполнен\n\n"; return;
  66.     int value;
  67.     cout<<"\nЗначение > ";
  68.     cin>>value;
  69.     for (int i=D->last; i>0; i--)
  70.         D->data[i]=D->data[i-1];
  71.     D->data[0]=value;
  72.     D->last++;
  73.     cout<<endl<<"Элемент добавлен\n\n";
  74. }
  75.  
  76. void AddR(Deque *D) {//добавление элемента в конец
  77.     if (D->last==N) {
  78.         cout<<"\nДек заполнен\n\n"; return; }
  79.     int value;
  80.     cout<<"\nЗначение > "; cin>>value;
  81.     D->data[D->last++]=value;
  82.     cout<<endl<<"Элемент добавлен\n\n";
  83. }
  84. void DeleteL(Deque *D) {//удаление первого элемента
  85.     for (int i=0; i<D->last; i++)
  86.         D->data[i]=D->data[i+1]; D->last--;
  87. }
  88. void DeleteR(Deque *D) {//удаление последнего элемента
  89.     D->last--;
  90. }
  91. int OutputL(Deque *D) {//вывод первого элемента
  92.     return D->data[0];
  93. }
  94. int OutputR(Deque *D) {//вывод последнего элемента
  95.     return D->data[D->last-1];
  96. }
  97. int Size(Deque *D) {//размер дека
  98.     return D->last;
  99. }
Add Comment
Please, Sign In to add comment