[color=black]Faca uma função que conte os elementos de uma lista previamente criada e depois retire desta o quinto elemento se houver e a imprima.
[/color]
[color=green]Bom eu comecei assim.[/color]
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct lista{
char nome[51];
int ano;
struct lista* prox;
};
typedef struct lista Lista;
Lista* insere (Lista* l){
int v;
system ("cls");
Lista* novo = (Lista*) malloc(sizeof(Lista));
printf(" Entre com o nome do carro.: ");
scanf(" %50[^\n]",novo->nome);
printf(" Entre com o ano do carro.: ");
scanf("%d",&v);
novo->ano = v;
novo->prox = l;
system("cls");
return novo;
}
int imprime (Lista* l)
{
system("cls");
Lista* p;
if( l == NULL){
system("cls");
printf("\n\nLISTA ESTA VAZIA\n\n");
}
for (p = l; p != NULL; p=p->prox)
{
printf("NOME.: %s\n",p->nome);
printf("ANO.: %d\n\n",p->ano);
}
}
Lista* remove_quinto(Lista*l, int v)
{
int cont = 1;
Lista * ant = NULL;
Lista * p = l;
if( p == NULL){
system("cls");
printf("\nA LISTA ESTA VAZIA.\n\n");
return l;
}
while(p != NULL && cont != v ){
ant = p;
p = p->prox;
cont++;
}
if(p != NULL && cont == v){
ant->prox = p->prox;
}
else if(cont == v){
ant->prox = p->prox;
}
else {
system("cls");
printf("\nA LISTA NAO CONTEM [%d] ELEMENTOS.\nELA TEM [%d], ELEMENTOS.\n\n",v,cont);
}
free (p);
return l;
}
int main()
{
int v, loop = -1, num;
Lista *l;
l = NULL;
system("cls");
while (loop != 0){
printf("[1]-Inserir novo carro na lista.");
printf("\n[2]-Imprimir a lista de carros.");
printf("\n[3]-Remove o elemento. ");
printf("\n[0]-Sair do programa. \nOPCAO.: ");
scanf("%d",&num);
system("cls");
if (num == 1)
l= insere (l);
else if (num == 2)
imprime(l);
else if (num == 3){
printf("\nEntre com o numero da posicao do elemento que deseja remover.:");
scanf("%d",&v);
l = remove_quinto(l,v);
}
else if (num == 0)
{
printf("\n Opcao desejada foi sair do programa.");
loop = num;
}
}
getch();
}