Show Values greater than an X value in a C list

Asked

Viewed 49 times

0

Suppose I declare a chained list in C and that list contains 7 integers, and I declare a value X = 20.

List values:

3,9,11,18,20,22,25

Knowing that my X is equal to 20 which command I would use to show on the screen how many values greater than X exist and which are they

1 answer

0

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

//Estrutura principal da lista.
struct listaE
{
    int vl;
    struct listaE *pl;
};


//funcoes da lista:
void displayLL(struct listaE *p)
{
    printf("Mostrar Lista:\n");
    if(p)
    {
        do
        {
            printf(" %d", p->vl);
            p=p->pl;
        }
        while(p);
    }
    else
        printf("Lista Vazia.");
}
//Q 1
int comprimento (struct listaE *p)
{

printf("\n");

    int i=0;
    if(p)
    {
        do
        {
           p=p->pl;
           i++;

        }



        while(p);
         printf(" %d",i);
    }
    else
        printf("Lista vazia.");
}

//Q 2

int maiores(struct listaE *p)
{
    int x=48;
    int i=5;
    printf("\n");
    printf("Valor(es) maior(es) que o de X:");
    printf("\n");

    if(p)
    {
        do
        {

           p=p->pl;
           i--;

        }
        while(x > p->vl);

         printf(" %d",i);
    }
    else
        printf("Lista vazia.");

}

// Q 3
int ultimo (struct listaE *p)
{
    printf("\n");
 printf("O ultimo valor inteiro e:\n");


    if(p)
    {
        do
        {
         p=p->pl;

        }
        while(p && p->pl != NULL );
             printf(" %d", p->vl);

    }

    else
        printf("Lista vazia.");
}





int main(void)
{
    struct listaE *Lista1 = NULL;
    struct listaE *Lista2 = NULL;
    struct listaE *Lista3 = NULL;
    struct listaE *Lista4 = NULL;
    struct listaE *Lista5 = NULL;

    //Criando os nos e associando os dados.

    Lista1 = (struct listaE*) malloc(sizeof(struct listaE*));
    Lista1->vl = 18;

    Lista2 = (struct listaE*) malloc(sizeof(struct listaE*));
    Lista2->vl = 27;

    Lista3 = (struct listaE*) malloc(sizeof(struct listaE*));
    Lista3->vl = 33;

    Lista4 = (struct listaE*) malloc(sizeof(struct listaE*));
    Lista4->vl = 45;

    Lista5 = (struct listaE*) malloc(sizeof(struct listaE*));
    Lista5->vl = 57;

    //Conectando os nos
    Lista1->pl = Lista2;
    Lista2->pl = Lista3;
    Lista3->pl = Lista4;
    Lista4->pl = Lista5;
    Lista5->pl = NULL;

    //Mostrando a lista.
    if(Lista1)
        displayLL(Lista1);
        comprimento(Lista1);
        maiores(Lista1);
        ultimo(Lista1);
  return 0;
}

Browser other questions tagged

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