Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct vector{
- int lmd;
- int val;
- struct vector *next;
- }vector;
- vector* add_new_value (vector *vc, int el, int lmd)
- {
- if (vc == NULL) {
- vc = malloc(sizeof(vector*));
- vc->val = el;
- vc->lmd = lmd;
- printf("val = %d lmd = %d\n", vc->val, vc->lmd);
- return vc;
- }
- else {
- add_new_value(vc->next, el, lmd);
- }
- }
- vector* getLast(vector *head) {
- if (head == NULL) {
- return NULL;
- }
- while (head->next) {
- head = head->next;
- }
- return head;
- }
- void pushBack(vector *head, int value, int lmd) {
- vector *last = getLast(head);
- vector *tmp = (vector*) malloc(sizeof(vector));
- tmp->val = value;
- tmp->lmd = lmd;
- tmp->next = NULL;
- last->next = tmp;
- }
- /*vec add_to_end(vec v1,int el, int lmd){
- vec temp = v1;
- while(temp->next != NULL){
- temp = temp->next;
- }
- vec vector=malloc(sizeof(vector));
- vector->val=el;
- vector->lmd=lmd;
- temp->next=vector;
- return temp;
- }*/
- void print_vector_lmd (vector* v) {
- if (v == NULL) return;
- else {
- printf("%3d ", v->lmd);
- print_vector_lmd(v->next);
- }
- }
- void print_vector_val(vector* v)
- {
- if (v == NULL) return;
- else {
- printf("%3d ", v->val);
- print_vector_val(v->next);
- }
- }
- int main()
- {
- int m,n,t;
- int lmd = 0;
- vector *v = NULL;
- scanf("%d %d", &m, &n);
- for (int i = 1; i <= m; i++) {
- for (int j = 1; j <= n; j++) {
- scanf("%d", &t);
- if (t != 0) {
- lmd = (i-1)*n+j-1;
- pushBack(v,t,lmd);
- }
- }
- }
- print_vector_lmd(v);
- printf("\n");
- print_vector_val(v);
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment