0% acharam este documento útil (0 voto)
1 visualizações9 páginas

Struct Base: Typedef

O documento apresenta implementações em C de listas simplesmente encadeadas, incluindo funções para alocar, inserir e buscar elementos. Ele também contém uma estrutura para gerenciar livros, com funcionalidades para exibir informações e manipular a lista, como inserção e exclusão de nós. Além disso, o código demonstra a manipulação de dados como título, autor, ano e quantidade de livros.

Enviado por

Md. Cheff
Direitos autorais
© All Rights Reserved
Levamos muito a sério os direitos de conteúdo. Se você suspeita que este conteúdo é seu, reivindique-o aqui.
Formatos disponíveis
Baixe no formato PDF, TXT ou leia on-line no Scribd
0% acharam este documento útil (0 voto)
1 visualizações9 páginas

Struct Base: Typedef

O documento apresenta implementações em C de listas simplesmente encadeadas, incluindo funções para alocar, inserir e buscar elementos. Ele também contém uma estrutura para gerenciar livros, com funcionalidades para exibir informações e manipular a lista, como inserção e exclusão de nós. Além disso, o código demonstra a manipulação de dados como título, autor, ano e quantidade de livros.

Enviado por

Md. Cheff
Direitos autorais
© All Rights Reserved
Levamos muito a sério os direitos de conteúdo. Se você suspeita que este conteúdo é seu, reivindique-o aqui.
Formatos disponíveis
Baixe no formato PDF, TXT ou leia on-line no Scribd

STRUCT BASE

#include <stdio.h>
#include <stdalign.h>

typedef struct elemento virgoline;


struct elemento{
int valor;
struct elemento *prox;
};

virgoline *inicio;
virgoline *fim;
int tamanho = 0;

virgoline *alocar(int valor)


{
virgoline *botao;
botao = (virgoline*) malloc(sizeof(virgoline));
botao->valor = valor;
botao->prox = NULL;

return botao;
}

void inserir(int valor)


{
virgoline *novoElemento = alocar(valor);
if(inicio == NULL)
{
inicio = novoElemento;
}
else
{
virgoline *aux = inicio;

novoElemento->prox = aux;
inicio = novoElemento;
}
tamanho ++;
}

//inserção fim da linha


void inserirFim(int valor)
{
//alocaçao dinamica
virgoline *novoElemento = alocar(valor);

if(inicio == NULL)
{
inicio = novoElemento;
fim = novoElemento;
}
else
{

😿
//Aponta o void/ nada da proxima lista, se n tiver é 0 na
prova Movimento para alocar novo elemento.
fim->prox = novoElemento;
fim = novoElemento;
}
tamanho ++;
}
//retorna posição elemento
int buscaValor(int valor)
{
int i;
//variavel para percorrer a lista
virgoline *aux = inicio;

for(i = 0; i < tamanho; i++)


{
if(aux->valor == valor)
{
break;
}
aux = aux->prox;
}

return i;
}
// apresenta o valr alocado na posicao em específico
int BuscaPorPosicao(int pos)
{
int i;

//variavel aux pra percorrer a lista


virgoline *aux = inicio;

for(i = 0; i <= pos; i++)


{
if(i == pos)
{
break;
}
aux = aux->prox;
}
return aux->valor;
}

STRUCT NO INICIO
#include <stdio.h>
#include <stdlib.h>
//começo do código
typedef struct elemento powy;
struct elemento{
int valor;
struct elemento *prox;
};
//apontando pro inicio
powy *inicio;
//powy *inicio = null; pois nao aponta pra nada pois não tem uma cabeça
powy *fim;
int tamanho = 0;

powy *alocar(int valor)


{
powy *botao;
botao = (powy*) malloc(sizeof(powy));
botao->valor = valor;
botao->prox = NULL;

return botao;
}

void inserir(int valor)


{
powy *novoElemento = alocar(valor);
if(inicio == NULL)
{
inicio = novoElemento;
}

else
{
powy *aux = inicio;
novoElemento ->prox = aux;
inicio = novoElemento;

}
}

//2 - Escreva um programa em C que irá inserir um novo nó no início de


uma lista simplesmente encadeada.

STRUCT NO FIM
#include <stdio.h>
#include <stdlib.h>

typedef struct elemento bola;


struct elemento{
int valor;
struct elemento *prox;
};

bola *inicio = NULL;


bola *fim = NULL;
int tamanho = 0;

bola *alocarEspacoDeMemoria(int valor){


bola *botao;
botao = (bola*) malloc (sizeof(bola));
botao->valor = valor;
botao->prox = NULL;

return botao;
}

void inserir(int valor){


bola *novoElemento = alocarEspacoDeMemoria (valor);
if (inicio == NULL){
inicio = novoElemento;
fim = novoElemento;

}
else{
bola *aux = fim;
aux->prox = novoElemento;
fim = novoElemento;
}
}
//3 - Escreva um programa em C para inserir um novo nó no final de uma
lista simplesmente encadeada.

STRUCT STRING
#include <stdio.h>
#include <stdlib.h>
#include<string.h>

typedef struct elemento livro;


struct elemento {
char titulo[100];
char autor[100];
int ano;
int quantidade;
struct elemento *prox;
};

livro *inicio = NULL;


livro *fim = NULL;
int quantidade = 0;

livro *alocaItem (char *titulo, char *autor, int ano, int quantidade)
{
livro *novoItem = (livro*) malloc(sizeof(livro));

strcpy(novoItem->titulo, titulo);
strcpy(novoItem->autor, autor);
novoItem->ano = ano;
novoItem->quantidade = quantidade;

novoItem->prox = NULL;

return novoItem;
}

void inserirItem (char *titulo, char *autor, int ano, int quantidade)
{
livro *novoItem = alocaItem(titulo, autor, ano, quantidade);

if(inicio == NULL){
inicio = novoItem;
}else {
livro *aux = inicio;

novoItem->prox = aux;
inicio = novoItem;
}
quantidade ++;
}

void exibeLista()
{
for(livro *aux = inicio; aux != NULL; aux = aux->prox)
{
printf("=========================================\n");
printf("Titulo: %s\n", aux->titulo);
printf("Autor: %s\n", aux->autor);
printf("Ano: %d\n", aux->ano);
printf("Quantidade: %d\n", aux->quantidade);
printf("=========================================\n");
}
printf("\n");
}

void exibeQuantidadeLivros()
{
int qtdLivros = 0;
for(livro *aux = inicio; aux != NULL; aux = aux->prox)
{
qtdLivros += aux->quantidade;
}
printf("A quantidade de livros e: %d\n", qtdLivros);
}

void exibeLivroPorAno(int ano)


{
for(livro *aux = inicio; aux != NULL; aux = aux->prox)
{
if(aux->ano == ano){
printf("=========================================\n");
printf("Titulo: %s\n", aux->titulo);
printf("Autor: %s\n", aux->autor);
printf("Ano: %d\n", aux->ano);
printf("Quantidade: %d\n", aux->quantidade);
printf("=========================================\n");
}
}
printf("\n");
}

// eXCLUIR NO INICIO

void excluirNoInicio(){

if(inicio = NULL){
printf("Lista vazia");
}

//Verificando se tem apenas 1 elemento


if(inicio -> prox == NULL){
inicio = NULL;
}

else{

livro *aux = inicio;

//Enviando
inicio = aux -> prox;
free(aux);
}

quantidade++;
}
//exclusão no fim
livro *buscaPenultimoElemento()
{
livro *aux = inicio;
while(aux->prox != fim)
{
aux = aux->prox;
}
return aux;

}
void ExcluirNoFim(){
if(inicio == NULL)
{
printf ("Lista vazia!\n");
return;
}
if(inicio->prox == NULL)
{
inicio = NULL;
fim = NULL;
}
else
{
livro *aux = fim;
livro *penultimo = buscaPenultimoElemento();
fim = penultimo;
fim->prox = NULL;
printf("elemento %d liberado da memorio \n", aux->titulo);
free(aux);
}
quantidade --;
}

int main () {
inserirItem("Memorias postumas de Bras Cubas", "Machado de Assis",
1881, 10);
inserirItem("A moreninha", "Joaquim Manuel de Macedo", 1844, 30);

inserirItem("A moreninha", "Joaquim Manoel de macedo", 1844, 30);


inserirItem("A moreninha", "Joaquim Manuel de Macedo", 1844, 30);

exibeLista();

exibeQuantidadeLivros();
exibeLivroPorAno(1881);
}

Você também pode gostar