Advertisement
Guest User

Untitled

a guest
May 29th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. struct List {
  7.     int value;
  8.     int line;
  9.     int column;
  10.     List* Next;
  11.     List* Prev;
  12. };
  13.  
  14.  
  15. void MakeList(int N, int M, int n, int m, List** Head, List* Prev){
  16.     if (m==M){
  17.         m = 0;
  18.         n++;
  19.     }
  20.     if (n==N)
  21.         return;
  22.     int tmp;
  23.  
  24.     cin >> tmp;
  25.     if (tmp){
  26.         (*Head) = (List*)malloc(sizeof(List));
  27.         (*Head)->value = tmp;
  28.         (*Head)->line = n;
  29.         (*Head)->column = m;
  30.         (*Head)->Prev = Prev;
  31.         (*Head)->Next = NULL;
  32.         MakeList(N, M, n, m+1, &((*Head)->Next), (*Head));
  33.     } else
  34.         MakeList(N, M, n, m+1, Head, Prev);
  35. }
  36.  
  37. void PrintList (List *Head){
  38.     if (Head!=NULL){
  39.         if (Head->Prev){
  40.             if (Head->Prev->column > Head->column)
  41.                 cout << endl;
  42.         }
  43.         cout << Head->value << ' ';
  44.         PrintList(Head->Next);
  45.     } else
  46.         cout << endl;
  47. }
  48.  
  49. void LAL (int *arr, List** Head){
  50.     if (!(*Head))
  51.         return;
  52.     if (!arr[(*Head)->line])
  53.         arr[(*Head)->line] = 1;
  54.     arr[(*Head)->line] *= (*Head)->value;
  55.     LAL (arr, &((*Head)->Next));
  56. }
  57.  
  58. int main()
  59. {
  60.     int N, M, i, *arr;
  61.     cin >> N >> M;
  62.     List* Lal;
  63.     MakeList(N, M, 0, 0, &Lal, NULL);
  64.     cout << "Your List:\n" ;
  65.     PrintList(Lal);
  66.  
  67.     arr = (int*)calloc(N, sizeof(int));
  68.     LAL(arr, &Lal);
  69.  
  70.     cout << "Product:\n";
  71.     for (i = 0; i < N; i++)
  72.         cout << arr[i] << ' ' ;
  73.     cout << endl;
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement