2
I have a problem with the Divide function, where I am getting a list and want to return to the right and left part of the list. Can someone give me a hand with logic.
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
struct celula {
    int num;
    struct celula * seg;
    struct celula * ant;
};
typedef struct celula Numero;
int gerarNumeros(int n);
int numeroDigitos(Numero * lst);
void geraSeed();
void geraLista(Numero *lst, int numeros);
void insereLista(Numero *lst, int num);
void imprimeLista(Numero * lst);
Numero * criaLista(int n);
Numero * DivideDireita(Numero *a, int n);
int main() {
    Numero * head_n1, * head_n2, * resposta ,* head1 ;
    int d;
    printf("Insira a quantidade de DIGITOS do seu numero binario\n");
    scanf("%i", &d);
    head_n1 = criaLista(1); 
    head_n2 = criaLista(1);
    head1 = criaLista(1);
    head2 = criaLista(1);
    printf("Criando os dois numeros... \n\n");
    geraLista(head_n1, d);
    geraLista(head_n2, d);
    imprimeLista(head_n1);
    printf("\n\n\n");
    head1 = DivideDireita(head_n1, d/2);
  //head2 = DivideEsquerda(head_n1,d/2);
    printf("\n Parte 1");
    printf("\n");
    imprimeLista(head1);  
    printf("\n Parte 2");
    printf("\n");
    //imprimeLista(head2);     
    printf("\n");
    getchar();
    return 0;
}
Numero * DivideDireita(Numero *a, int n)
 {
   Numero * novo;
   Numero * aux;
   aux= a->ant;
   novo = criaLista(1);
for(int i=0; i<n; i++)
{
    insereLista(novo,aux->num);
    aux = aux->ant;
}
return novo;
}
/*Retorna um numero com outro endereço*/
Numero * copia(Numero * a)
{
    Numero * nova, * aux;
    aux = a->ant;
    nova = criaLista(a->num); // cria nova cabeça igual a cabeça de a
    while(aux != a) { // percorre do final até o começo
        insereLista(nova, aux->num);
        aux = aux->ant;
    }
    return aux; // devolve a cabeça do numero copiado
}
int numeroDigitos(Numero * lst)
{
    Numero * aux;
    int i;
    i = 0;
    aux = lst->seg;
    while(aux != lst) {
        aux = aux->seg;
        i++;
    }
    return i;
}
/*Retorna um ponteiro para uma copia do numero*/
Numero *copiaNum(Numero *head) 
{
    Numero *aux = head->ant;
    Numero *copia = criaLista(0);
    while(aux != head){
        insereLista(copia, aux->num);
        aux = aux->ant;
    }
    return copia;
}
/*Gerar numeros entre 0 ou 1 se n=2*/
int gerarNumeros(int n){
    return rand() % n ;
}
void geraSeed() { /* gera uma seed para nova sequencia de numeros aleatorios */
    srand((unsigned)time(NULL));
}
void removeNumero(Numero * rm) { // desaloca a memória da célula indicada
    Numero * aux;
    if(rm->seg == rm) { // se a celula for a cabeça
        free(rm); // desaloca rm
    } else { 
        aux = rm->ant;
        aux->ant->seg = rm;
        rm->ant = aux->ant;
        free(aux); // desaloca aux
    }
}
void insereLista(Numero *lst, int num)
{
    Numero *novo;
    novo = (Numero*) malloc(sizeof(Numero));
    novo->num = num; //acessa nova e bota o endereço da pessoa1 lá dentro;
    if(lst->seg == lst) {
        novo->seg = lst;
        lst->seg = novo;
        novo->ant = lst;
        lst->ant = novo;
        return;
    }
    novo->seg = lst->seg;
    lst->seg = novo;
    novo->seg->ant = novo;
    novo->ant = lst;
}
void geraLista(Numero *numero, int numeros) 
{
    int i;
    i=0;
    while (i != numeros) { // deve ser colocado 256 digitos
        insereLista(numero, gerarNumeros(2)); //GERANDO NUMEROS DE 0-1 E GUARDANDO NA LISTA
        i++;
    }
}
/*Cria uma lista*/
Numero * criaLista(int n)
{
    Numero * head;
    head = (Numero *) malloc(sizeof(Numero));
    head->num = n;
    head->seg = head;
    return head;
}
/*Imprime a lista caso o primeiro numero tenha um -1 é porque*/
/*o numero é negativo                                        */
void  imprimeLista(Numero * lst)
{
    //int i;
    Numero * aux;
    aux = lst->seg;
    //i=0;
    /*Imprimir o sinal*/
    if(lst->num == -1)  {
        printf("-");
    }
    while(aux != lst)
    {
        printf("%i", aux->num);
        aux = aux->seg;
        //i++;
    }
}
EDIT CODE List to the left this returning to the contrary, I thought to make an insert at the end of the list because the function I implemented is inserting at the beginning, I want to optimize this without having to create a new function.
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
struct celula {
    int num;
    struct celula * seg;
    struct celula * ant;
};
typedef struct celula Numero;
int gerarNumeros(int n);
int numeroDigitos(Numero * lst);
void geraSeed();
void geraLista(Numero *lst, int numeros);
void insereLista(Numero *lst, int num);
void imprimeLista(Numero * lst);
Numero * criaLista(int n);
Numero * DivideEsquerda(Numero *a, int n);
Numero * DivideDireita(Numero *a, int n);
int main() {
    Numero * head_n1, * head_n2, * resposta , *head1, *head2 ;
    int d;
    printf("Insira a quantidade de DIGITOS do seu numero binario\n");
    scanf("%i", &d);
    head_n1 = criaLista(1); 
    head_n2 = criaLista(1);
    head1 = criaLista(1);
    head2 = criaLista(1);
    printf("Criando os dois numeros... \n\n");
    geraLista(head_n1, d);
    geraLista(head_n2, d);
    imprimeLista(head_n1);
    printf("\n\n\n");
    head1 = DivideEsquerda(head_n1, d/2);
    head2 = DivideDireita(head_n1, d/2);
    printf("\n Parte 1");
    printf("\n");
    imprimeLista(head1);  
    printf("\n Parte 2");
    printf("\n");
    imprimeLista(head2);     
    printf("\n");
    getchar();
    return 0;
}
Numero * DivideEsquerda(Numero *a, int n)
{
Numero * esq;
Numero * aux;
aux= a->seg;
esq = criaLista(1);
for(int i=n; i>0; i--)
{
    insereListaFim(esq,aux->num);
    aux = aux->seg;
}
return esq;
}
Numero * DivideDireita(Numero *a, int n){
Numero * dir;
Numero * aux;
aux= a->ant;
dir = criaLista(1);
for(int i=0; i<n; i++)
{
    insereLista(dir,aux->num);
    aux = aux->ant;
}
return dir;
}
/*Retorna um numero com outro endereço*/
Numero * copia(Numero * a)
{
    Numero * nova, * aux;
    aux = a->ant;
    nova = criaLista(a->num); // cria nova cabeça igual a cabeça de a
    while(aux != a) { // percorre do final até o começo
        insereLista(nova, aux->num);
        aux = aux->ant;
    }
    return aux; // devolve a cabeça do numero copiado
}
int numeroDigitos(Numero * lst)
{
    Numero * aux;
    int i;
    i = 0;
    aux = lst->seg;
    while(aux != lst) {
        aux = aux->seg;
        i++;
    }
    return i;
}
/*Retorna um ponteiro para uma copia do numero*/
Numero *copiaNum(Numero *head) 
{
    Numero *aux = head->ant;
    Numero *copia = criaLista(0);
    while(aux != head){
        insereLista(copia, aux->num);
        aux = aux->ant;
    }
    return copia;
}
/*Gerar numeros entre 0 ou 1 se n=2*/
int gerarNumeros(int n){
    return rand() % n ;
}
void geraSeed() { /* gera uma seed para nova sequencia de numeros aleatorios */
    srand((unsigned)time(NULL));
}
void removeNumero(Numero * rm) { // desaloca a memória da célula indicada
    Numero * aux;
    if(rm->seg == rm) { // se a celula for a cabeça
        free(rm); // desaloca rm
    } else { 
        aux = rm->ant;
        aux->ant->seg = rm;
        rm->ant = aux->ant;
        free(aux); // desaloca aux
    }
}
void insereLista(Numero *lst, int num)
{
    Numero *novo;
    novo = (Numero*) malloc(sizeof(Numero));
    novo->num = num; //acessa nova e bota o endereço da pessoa1 lá dentro;
    if(lst->seg == lst) {
        novo->seg = lst;
        lst->seg = novo;
        novo->ant = lst;
        lst->ant = novo;
        return;
    }
    novo->seg = lst->seg;
    lst->seg = novo;
    novo->seg->ant = novo;
    novo->ant = lst;
}
void geraLista(Numero *numero, int numeros) 
{
    int i;
    i=0;
    while (i != numeros) { // deve ser colocado 256 digitos
        insereLista(numero, gerarNumeros(2)); //GERANDO NUMEROS DE 0-1 E GUARDANDO NA LISTA
        i++;
    }
}
/*Cria uma lista*/
Numero * criaLista(int n)
{
    Numero * head;
    head = (Numero *) malloc(sizeof(Numero));
    head->num = n;
    head->seg = head;
    return head;
}
/*Imprime a lista caso o primeiro numero tenha um -1 é porque*/
/*o numero é negativo                                        */
void  imprimeLista(Numero * lst)
{
    //int i;
    Numero * aux;
    aux = lst->seg;
    //i=0;
    /*Imprimir o sinal*/
    if(lst->num == -1)  {
        printf("-");
    }
    while(aux != lst)
    {
        printf("%i", aux->num);
        aux = aux->seg;
        //i++;
    }
}
						
The
nofdividewould be the bit Where would you split it into two lists ? But to have two lists the signature would have to be different and include the lists for the division, or return a structure consisting of 2 pointers of lists. These parts are not very clear– Isac
this n would be the size of digits that is d divided by 2 and the function that I’m trying to do is to return to half of the right and half of the left. List example end<->1<->1<->0<->1<->start return end<->0<->1<->start list and left list
– Matheus Francisco
void * Divide(Numero *a, int n)where these two halves will be in this function ?– Isac
I made an edit on the function I would like to return the two in the same function, I think return a pointer to the right part and one to the left part
– Matheus Francisco
This is C, there can only be one return value
– Isac
Yes I forgot to update.
– Matheus Francisco