Advertisement
Guest User

lab2

a guest
Apr 27th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. // lab2_1.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "stdio.h"
  6. #include "string"
  7. #include <conio.h>
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12. struct List
  13. {
  14.     int data;
  15.     List *next;
  16. };
  17.  
  18. void maxElement(List* spis)  //нахождение адреса максимального элемента
  19. {
  20.  
  21.     int max = spis->data;
  22.     List *adress = NULL;
  23.     while (spis)
  24.     {
  25.         if (max < spis->data)
  26.         {
  27.             max = spis->data;
  28.             adress = spis;
  29.         }
  30.         spis = spis->next;
  31.     }
  32.     cout << "\nmax element: " << max <<"\n";
  33.     cout << "max adress: " << adress;
  34.     //return adress;
  35. }
  36.  
  37. void newList(List* spis, List* start, int count)
  38. {
  39.  
  40.  
  41.     cout << "element: ";
  42.     cin >> spis->data; // инициализируем его данными
  43.  
  44.     spis->next = NULL; // следующий элемент - NULL
  45.     start = spis; // запоминаем начало
  46.  
  47.     int chislo;
  48.     for (int i = 1; i<count; i++) // добавление элементов
  49.     {
  50.         spis->next = new List; // создаем элемент (и сразу устанавливаем ссылку на него из текущего)
  51.         cout << "element: ";
  52.         cin >> chislo;
  53.         spis->next->data = chislo; // добавляем данные
  54.         spis->next->next = NULL; // следующий элемент - NULL
  55.         spis = spis->next; // делаем новый элемент текущим
  56.     }
  57.  
  58.     // выводим (не обязательно)
  59.     cout << "\n\n";
  60.     spis = start;
  61.  
  62.     while (spis)
  63.     {
  64.         cout << "\n" << spis->data;
  65.         spis = spis->next;
  66.     }
  67. }
  68.  
  69. int main()
  70. {
  71.     List *spis = new List; // создаем список
  72.     List *start = spis; // это будет указатель на начало списка
  73.  
  74.     int count;
  75.     cout << "enter number of elements: ";
  76.     cin >> count;
  77.     cout << "\n\n";
  78.     newList(spis, start, count);
  79.  
  80.     //cout << "\nmax adress: ";
  81.     maxElement(spis);
  82.  
  83.     getchar();
  84.     getchar();
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement