Skip to content

Commit

Permalink
criando o remover
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaian-k committed Jun 23, 2022
1 parent fb54692 commit 8937d65
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 50 deletions.
92 changes: 52 additions & 40 deletions abb.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,84 @@
#include <stdlib.h>


typedef struct vertice{
typedef struct node_abb {
//Dados iniciais da encomenda de um livro
int id; //identificador
char *nome_aluno;
int matricula;
char *descricao;

//mecanismo p/ unir nos!
struct vertice *esq;
struct vertice *dir;
} Vertice;
Vertice *raiz = NULL;
struct node_abb *left; struct node_abb *right;
} Node_abb; Node_abb *root = NULL;


Vertice *buscar(int id, Vertice *aux){
Node_abb *buscar(int id, Node_abb *aux){
if (aux != NULL){
if (aux->id == id){
return aux;}

else if (id < aux->id){
if (aux->esq != NULL){
return buscar(id, aux->esq);}
else {
return aux;}}

else if (id > aux->id){
if (aux->dir != NULL){
return buscar(id, aux->dir);}
else {
return aux;}}}
else {
return NULL;}}
if (aux->id == id){return aux;}

else if (aux->left != NULL && id < aux->id){return buscar(id, aux->left);}
else if (aux->right != NULL && id > aux->id){return buscar(id, aux->right);}

else {return aux;}}

else {return NULL;}}


void add_abb(int id, char *nome_aluno, int matricula, char *descricao){
Vertice *aux = buscar(id, raiz);

if (aux != NULL && aux->id == id){
printf("Insercao invalida!\n");}
Node_abb *aux = buscar(id, root);

if (aux != NULL && aux->id == id){printf("Insercao invalida!\n");}

else {
Vertice *novo = malloc(sizeof(Vertice));
Node_abb *novo = malloc(sizeof(Node_abb));
novo->id = id;
novo->nome_aluno = nome_aluno;
novo->matricula = matricula;
novo->descricao = descricao;
novo->esq = NULL; novo->dir = NULL;

if (aux == NULL){//arvore esta vazia
raiz = novo;}
novo->left = NULL; novo->right = NULL;

if (aux == NULL){root = novo;} //arvore esta vazia

else {
if (id < aux->id){
aux->esq = novo;}
else {
aux->dir = novo;}}}}
if (id < aux->id){aux->left = novo;}

else {aux->right = novo;}}}}


Node_abb *remover_abb(Node_abb *root, int id){
if (root == NULL){return NULL;}

else {
if (root->left, root->right == NULL){ //remove nós, caso nao tenha filhos
free(root); printf("Elemento removido!\n"); return NULL;}

else { //remove nós, caso tenha 1 ou 2 filhos
if (root->left, root->right != NULL){ //com 2 filhos
Node_abb *aux = root->left;
while (aux->right != NULL){
aux = aux->right;}

root->id = aux->id;
aux->id = id;

root->left = remover_abb(root->left, id); return root;}

else { //com 1 filho
Node_abb *aux;

if (root->left != NULL){aux = root->left;}
else {aux = root->right;}

free(root); return aux;}}}}


void in_ordem(Vertice *aux){
if (aux->esq != NULL){
in_ordem(aux->esq);}
void in_ordem(Node_abb *aux){
if (aux->left != NULL){in_ordem(aux->left);}

printf("%d\n", aux->id);
printf("%s\n", aux->nome_aluno);
printf("%d\n", aux->matricula);
printf("%s\n", aux->descricao);

if (aux->dir != NULL){
in_ordem(aux->dir);}}
if (aux->right != NULL){in_ordem(aux->right);}}
19 changes: 9 additions & 10 deletions fila.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
#include <stdlib.h>


typedef struct pedido{
typedef struct pedido {
int identificador;
char *nome_aluno;
//... demais dados
int prioridade;
} Pedido;


typedef struct node{
typedef struct node_fifo {
Pedido *p;
//mecanismo p/ unir nos!
struct node *prox;
} Node;
Node * inicio = NULL; Node * fim = NULL; int tam = 0;
struct node_fifo *prox;
} Node_fifo;
Node_fifo * inicio = NULL; Node_fifo *fim = NULL; int tam = 0;


void add_fila(int identificador, char *nome_aluno, int prioridade){
Expand All @@ -24,7 +24,7 @@ void add_fila(int identificador, char *nome_aluno, int prioridade){
p->nome_aluno = nome_aluno;
p->prioridade = prioridade;

Node *novo = malloc(sizeof(Node));
Node_fifo *novo = malloc(sizeof(Node_fifo));
novo->p = p;
novo->prox = NULL;

Expand All @@ -38,7 +38,7 @@ void add_fila(int identificador, char *nome_aluno, int prioridade){


void imprimir(){
Node *aux = inicio;
Node_fifo *aux = inicio;
for (int i=0; i<tam; i++){
printf("Identificador: %d\n", aux->p->identificador);
aux = aux->prox;}}
Expand All @@ -48,12 +48,11 @@ Pedido remover_fila(){
Pedido pedido;
//remover!
if (inicio != NULL){ //remover do antigo inicio da lista!
Node *lixo = inicio;
Node_fifo *lixo = inicio;
inicio = inicio->prox;
pedido.identificador = lixo->p->identificador;
//...
free(lixo); tam--;

if(tam == 1){
fim = NULL;}}
return pedido;}
fim = NULL;}} return pedido;}
Binary file added principal
Binary file not shown.

0 comments on commit 8937d65

Please sign in to comment.