Guest User

Untitled

a guest
Jun 23rd, 2016
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct vector{
  5.     int lmd;
  6.     int val;
  7.     struct vector *next;
  8.  
  9. }vector;
  10.  
  11. vector* add_new_value (vector *vc, int el, int lmd)
  12. {
  13.     if (vc == NULL) {
  14.         vc = malloc(sizeof(vector*));
  15.         vc->val = el;
  16.         vc->lmd = lmd;
  17.         printf("val = %d lmd = %d\n", vc->val, vc->lmd);
  18.         return vc;
  19.     }
  20.     else {
  21.         add_new_value(vc->next, el, lmd);
  22.         }
  23. }
  24.  
  25. vector* getLast(vector *head) {
  26.     if (head == NULL) {
  27.         return NULL;
  28.     }
  29.     while (head->next) {
  30.         head = head->next;
  31.     }
  32.     return head;
  33. }
  34.  
  35. void pushBack(vector *head, int value, int lmd) {
  36.     vector *last = getLast(head);
  37.     vector *tmp = (vector*) malloc(sizeof(vector));
  38.     tmp->val = value;
  39.     tmp->lmd = lmd;
  40.     tmp->next = NULL;
  41.     last->next = tmp;
  42. }
  43.  
  44. /*vec add_to_end(vec v1,int el, int lmd){
  45.  
  46.     vec temp = v1;
  47.     while(temp->next != NULL){
  48.         temp = temp->next;
  49.     }
  50.     vec vector=malloc(sizeof(vector));
  51.  
  52.     vector->val=el;
  53.     vector->lmd=lmd;
  54.  
  55.     temp->next=vector;
  56.     return temp;
  57. }*/
  58.  
  59.  
  60. void print_vector_lmd (vector* v) {
  61.     if (v == NULL) return;
  62.     else {
  63.         printf("%3d ",  v->lmd);
  64.         print_vector_lmd(v->next);
  65.     }
  66. }
  67. void print_vector_val(vector* v)
  68. {
  69.     if (v == NULL) return;
  70.     else {
  71.         printf("%3d ",  v->val);
  72.         print_vector_val(v->next);
  73.     }
  74. }
  75.  
  76. int main()
  77. {
  78.     int m,n,t;
  79.     int lmd = 0;
  80.     vector *v = NULL;
  81.     scanf("%d %d", &m, &n);
  82.     for (int i = 1; i <= m; i++) {
  83.         for (int j = 1; j <= n; j++) {
  84.             scanf("%d", &t);
  85.             if (t != 0) {
  86.                 lmd = (i-1)*n+j-1;
  87.                 pushBack(v,t,lmd);
  88.  
  89.             }
  90.         }
  91.     }
  92.  
  93.     print_vector_lmd(v);
  94.     printf("\n");
  95.     print_vector_val(v);
  96.     printf("\n");
  97. }
Advertisement
Add Comment
Please, Sign In to add comment