Advertisement
Guest User

Working conversion from list to matrix

a guest
Oct 17th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <cmath>
  2. using namespace std;
  3.  
  4. struct node {
  5.     int data;
  6.     node* next;
  7. };
  8.  
  9. struct list {
  10.     node* head;
  11.     node* tail;
  12. };
  13.  
  14. void add_node(int value, list* listx) {
  15.     node *temp = new node;
  16.     temp->data = value;
  17.     temp->next = NULL;
  18.     if(listx->head == NULL) {
  19.         listx->head = temp;
  20.         listx->tail = temp;
  21.         temp = NULL;
  22.     }
  23.     else { 
  24.         listx->tail->next = temp;
  25.         listx-> tail = temp;
  26.     }
  27. }
  28.  
  29. void add_first(int value, list* listx) {
  30.    
  31.     node *temp = new node;
  32.    
  33.     temp->data = value;
  34.    
  35.     temp->next = listx->head;
  36.    
  37.     listx->head = temp;
  38.    
  39. }
  40.  
  41. void display(list* listx) {
  42.     node *temp = new node;
  43.     temp = listx->head;
  44.     while(temp != NULL) {
  45.         cout << "(" << (temp->data + 1) << ")";
  46.         if(temp->next != NULL) {
  47.             cout << ", ";
  48.         }
  49.         temp = temp->next;
  50.     }
  51. }
  52.  
  53.  
  54. int main() {
  55.    
  56.     int n;
  57.     cin >> n;
  58.    
  59.     list listar[n];
  60.    
  61.    
  62.    
  63.     for(int i = 0; i < n; i++) {
  64.        
  65.         list *listx = new list;
  66.         listx->head = NULL;
  67.         listx->tail = NULL;
  68.        
  69.         int inserted;
  70.         for(cin >> inserted; inserted != 777; cin >> inserted) {
  71.             if(inserted < 0) {
  72.                 add_first(abs(inserted - 1), listx);
  73.             } else {
  74.                 add_node(inserted - 1, listx);
  75.             }
  76.         }
  77.         listar[i] = *listx;
  78.        
  79.     }
  80.    
  81.     cout << "Inserted Graph: " << endl;
  82.     cout << "----------------" << endl;
  83.    
  84.     for(int i = 0; i < n; i++) {
  85.         cout << "(" << i + 1 << "): ";
  86.         display(&listar[i]);
  87.         cout << endl;
  88.     }
  89.    
  90.     cout << "---------------" << endl;
  91.     cout << "Converting to matrix..." << endl;
  92.    
  93.     bool matrix[n][n];
  94.    
  95.     for(int i = 0; i < n; i++) {
  96.        
  97.         bool row[n];
  98.        
  99.         for(int j = 0; j < n; j++) {
  100.            
  101.             row[i] = false;
  102.            
  103.         }
  104.        
  105.         list *list_i = &listar[i];
  106.        
  107.         for(node *nodex = list_i->head; nodex != NULL; nodex = nodex->next) {
  108.            
  109.             row[nodex->data] = true;
  110.            
  111.         }
  112.        
  113.         memcpy(matrix + i, row, sizeof(row));
  114.        
  115.     }
  116.    
  117.     cout << "-----------" << endl;
  118.     cout << "Converted matrix: " << endl;
  119.    
  120.     cout << "  |";
  121.     for(int i = 0; i < n; i++) {
  122.        
  123.         cout << " " << i + 1 << " |";
  124.        
  125.     }
  126.     cout << endl;
  127.     for(int i = 0; i < n; i++) {
  128.        
  129.         cout << i + 1 << " |";
  130.         for(int j = 0; j < n; j++) {
  131.            
  132.             cout << " " << (matrix[i][j] ? 1 : 0) << " |";
  133.            
  134.         }
  135.         cout << endl;
  136.        
  137.     }
  138.    
  139.    
  140.    
  141.    
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement