Doubt how to reverse values in a statistic list in C

Asked

Viewed 54 times

-4

Hi, I’m having second thoughts about a university paper on how to reverse a sequence of numbers into a static list in C. The specific part I’m doubting is :

**int SList_reverse(SList* lista, int dado){
      if(!SList_cheia(lista)){
      int i;
      for(i=0; i< lista->qty; i++)
        printf("<-[%d] ", lista->data[i]);
      putchar('\n');
      }
      return 0;
    }**

Complete code:

#include "StaticList.h"
#include <stdlib.h>
#include <stdio.h>

#define MAX 10
typedef struct list{
  int qty;
  int data[MAX];
} TList;

TList* SList_create(){
  TList* nova = (TList*) malloc(sizeof(TList));
  if(nova!=NULL){
    nova->qty = 0;
  }
  return nova;
}

int SList_insert(SList* lista, int dado){
  if(!SList_cheia(lista)){
    lista->data[(*lista).qty++] = dado;
    return 1;
  }
  return 0;
}

void SList_print(SList* lista){
  int i;
  for(i=0; i< lista->qty; i++)
    printf("[%d]-> ", lista->data[i]);
  putchar('\n');
}

int SList_search(SList* lista, int dado){
  int i;
  for(i=lista->qty-1; i>=0; i--)
    if(lista->data[i] == dado)
      break;
  return i;
}

int SList_remove(SList* lista, int dado){
  if(!SList_vazia(lista)){
    int i = SList_search(lista, dado);
    if(i>=0){
      int j;
      for(j=i; j<lista->qty-1; j++){
        lista->data[j] = lista->data[j+1];
      }
      lista->qty--;
      return 1;
    }
  
  }
  return 0;
}

int SList_reverse(SList* lista, int dado){
  if(!SList_cheia(lista)){
  int i;
  for(i=0; i< lista->qty; i++)
    printf("<-[%d] ", lista->data[i]);
  putchar('\n');
  }
  return 0;
}

void SList_destroy(SList* lista){
  if(lista!= NULL)
    free(lista);
}

int SList_getQty(SList* lista){
  if(lista!= NULL)
    return lista->qty;
  return 0;
}



int SList_cheia(SList* lista){
  return (lista->qty == MAX);
} 

int SList_vazia(SList* lista){
  return (lista->qty == 0);
}
  • When you say reverter uma sequencia de números, do you want to reverse the order of the numbers? Exmeplo: 123456789 in 987654321.

  • Exactly that.

1 answer

1


In this case you don’t even need to check if the list is full, just invert the items according to the code below.

int SList_reverse(SList* lista, int dado){
  int i;
  int aux;

  for(i=0; i< lista->qty /2; i++) {
    aux = lista->data[i];
    lista->data[i] = lista->data[(lista->qty - 1) - i];
    lista->data[lista->qty - i] = aux;
    printf("<-[%d] ", lista->data[(lista->qty - 1) - i]);
  }
  putchar('\n');
  return 0;
}
  • 1

    Note that in your code there will be an access to a memory location outside the array of the list. When i = 0, then lista->data[lista->qty - 0], whereas q the last element of the vector is lista->data[lista->qty - 1], soon lista->data[lista->qty - 0] is not part of the vector. That is, if the size is 10, then the vector boundary is [9], soon [10 - 0] == [10], which is not a valid position in the vector. Another error is that the for need to repeat only half of the times you put it, because when it gets in the middle it will deflect what has been reversed.

  • 1

    It would have to be like this lista->data[(lista->qty - 1) - i] to fix the first problem. The second just put i < (lista->qty / 2) within the for

  • 1

    True, posted running, the two corrections you quoted are necessary.

Browser other questions tagged

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