Concatenate two sequential static lists

Asked

Viewed 275 times

-1

I’m having trouble making a function that receives two lists and returns a third with the elements of the first concatenated with the elements of the second. I made the code but it returns me the third list with the elements of the first and with memory garbage instead of the elements of the second.

Lista concatenar(Lista lst1, Lista lst2) {

  Lista lst3 = (Lista) malloc(sizeof(Lista));
  int i,j;

  for (i = 0; i < lst1->fim; i++) {
    lst3->no[i] = lst1->no[i];
  }

  j = lst3->fim;

  for (i = 0; i < lst2->fim; i++) {
    lst3->no[j+1] = lst2->no[i];
  }

  return lst3;
}

Call the inserts(this function is working normal):

insere_elem(lst1, 4);
insere_elem(lst1, 12);
insere_elem(lst1, 0);

insere_elem(lst2, 11);
insere_elem(lst2, 18);
insere_elem(lst2, 25);

Struct lista:

struct lista {
  int no[max];
  int fim;
};
  • Post the declaration of your List variable. You say it’s a sequential static list but the treatment gives the impression of being a dynamic chained list.

  • I did that, added struct to my code

  • first you need to make this program compile...because the way you put it just doesn’t compile...

  • You are not using linked list, but rather an array. Why do you call this contiguous region of list memory?

1 answer

0

Since you were using pointers I needed to make some changes, where without such I do not see how the code would work... You have a "TL", controller, embedded in the struct, the "END"...use it. It’ll look something like this:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define max 100

struct lista {
  int no[max];
  int fim;
};typedef struct lista Lista;

void init(Lista ** lst)
{
    *lst = (Lista*) malloc (sizeof(Lista));     
    (*lst) -> fim = 0;
}
Lista* concatenar(Lista * lst1, Lista * lst2) {
  Lista * lst;
  init(&lst);

  int i;

  for (i = 0; i < lst1->fim; i++) {
    lst->no[lst -> fim] = lst1->no[i];
    lst -> fim++;
  }

  for (i = 0; i < lst2->fim; i++) {
    lst->no[lst -> fim] = lst2->no[i];
    lst -> fim++;
  }

  return lst;
}


void insere_elem(Lista ** lst, int elem)
{
    if((*lst) -> fim <= max)
    {
        (*lst) -> no[(*lst) -> fim] = elem;
        (*lst) -> fim++;
    }   
}



void exibeLista(Lista * lst)
{
    for(int i=0; i < lst -> fim; i++)
        printf("%d ", lst -> no[i]);        
    printf("\n\n\n\n");

}

int main(void)
{
    Lista * lst1;
    init(&lst1);
    Lista * lst2;
    init(&lst2);

    Lista * lst3;


    insere_elem(&lst1, 4);
    insere_elem(&lst1, 12);
    insere_elem(&lst1, 0);

    insere_elem(&lst2, 11);
    insere_elem(&lst2, 18);
    insere_elem(&lst2, 25);

    exibeLista(lst1);
    exibeLista(lst2);

    lst3 = concatenar(lst1, lst2);
    exibeLista(lst3);
    getch();


}
  • insere_elem should not receive a pointer from a list pointer, since there is no interest in exchanging the object passed by pointer for another object. Not to mention that the code gets cleaner without losing in any processing/abstraction

  • By your code, too, it would be leaner if you, instead of doing the for of the two lists making the steps of the function insere_elem, call soon insere_elem. There is a much better scheme in terms of efficiency, but it is not by making the "insertion" a-a-one of the elements

Browser other questions tagged

You are not signed in. Login or sign up in order to post.