nikitast

ekz_prep_2

Jan 20th, 2021
814
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. struct element {
  4.     element* prev;
  5.     element* next;
  6.     int data;
  7. };
  8.  
  9. element *first, *last, *cur;
  10.  
  11. void ElementInput(element*& first, element*& last)
  12. {
  13.     char snum[10];
  14.     first = new element;
  15.     puts("Enter the elements:");
  16.     gets_s(snum);
  17.     first->data = atoi(snum);
  18.     first->prev = NULL;
  19.     first->next = NULL;
  20.     last = first;
  21.     gets_s(snum);
  22.     while (strlen(snum) != 0)
  23.     {
  24.         element* cur = new element;
  25.         cur->data = atoi(snum);
  26.         cur->prev = last;
  27.         last->next = cur;
  28.         cur->next = NULL;
  29.         last = cur;
  30.         gets_s(snum);
  31.     }
  32. }
  33.  
  34. void ElementCompute(element* first, element* last)
  35. {
  36.     while (first != NULL)
  37.     {
  38.         printf("%d ", first->data * last->data);
  39.         first = first->next;
  40.         last = last->prev;
  41.     }
  42. }
  43.  
  44. void ElementOutput(bool reverse, element* first, element* last)
  45. {
  46.     element* cur;
  47.     if (reverse)
  48.         cur = last;
  49.     else
  50.         cur = first;
  51.     while (cur != NULL)
  52.     {
  53.         printf("%2d ", cur->data);
  54.         if (reverse)
  55.             cur = cur->prev;
  56.         else
  57.             cur = cur->next;
  58.     }
  59. }
  60.  
  61. void MemoryClean(element*& first)
  62. {
  63.     while (first != NULL)
  64.     {
  65.         cur = first;
  66.         first = first->next;
  67.         delete cur;
  68.     }
  69. }
  70.  
  71. int main()
  72. {
  73.     ElementInput(first, last);
  74.     puts("The entered list:");
  75.     ElementOutput(false, first, last);
  76.     puts("\n\nThe reversed list:");
  77.     ElementOutput(true, first, last);
  78.     puts("\n\nThe computed values x1*xn,...,xn*x1:");
  79.     ElementCompute(first, last);
  80.     //last = NULL;
  81.     MemoryClean(first);
  82.     puts("\n\n");
  83.     return 0;
  84. }
RAW Paste Data