domingo, 19 de setembro de 2021

Linguagem C: Trabalhando com nós / Lista encadeada

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

typedef struct No
{
    int dado;
    struct No *next;
    struct No *before;
}No;

No *createNode(int val)
{
    No *newNode = malloc(sizeof(No));
    newNode->dado = val;
    newNode->next = NULL;
    newNode->before = NULL;
    return newNode;
}

No *insertInitNode(int num, No *Node)
{
    No *novo = malloc(sizeof(No));
    novo->before = NULL;
    novo->next=Node;
    novo->dado=num;
    return novo;
}

No *insertEndNode(int num, No *Node)
{
    No *aux = Node;
    No *novo;
    novo = malloc(sizeof(No));
    novo->dado=num;
    novo->next=NULL;
    while(aux->next!=NULL)
    {
        aux=aux->next;
        if(aux->next==NULL)
        {
            novo->before=aux;
        }
    }
    aux->next=novo;
    return Node;
}

void findInNode(int busca, No *Node)
{
    int contador=1;
    No *aux;
    aux = Node;
    while (aux->next!=NULL && aux->dado!=busca)
    {
        aux = aux->next;
        contador=contador+1;
    }
    if(aux->dado==busca)
    {
        printf("O valor procurado está na posição %d\n",contador);
    }
    else
    {
        printf("O valor procurado não foi encontrado\n");
    }
}

void findAllInNodes(int busca, No *Node)
{
    int contador=1;
    No *aux;
    aux = Node;
    while (aux->next!=NULL)
    {
        aux = aux->next;
        contador=contador+1;
        if(aux->dado==busca)
        {
            printf("O valor procurado está na posição %d\n",contador);
        }
    }
}

No *EditValueNode(int target, int target2, No *Node)
{
    No *aux;
    aux = Node;
    while (aux->next!=NULL && aux->dado!=target)
    {
        aux =  aux->next;
        if (aux!=NULL && aux->dado==target)
        {
            aux->dado=target2;
        }
    }
    return Node;
}

No *EditNode(int target, int value, No *Node)
{
    int contador=1;
    No *aux;
    aux = Node;
    while (aux->next!=NULL && contador!=target)
    {
        aux = aux->next;
        contador=contador+1;
    }
    if(contador==target)
    {
        aux->dado=value;
    }
    else
    {
        printf("No não encontrado\n");
    }
    return Node;
}

No *deleteValueInNode(int target, No *Node)
{
    No *aux;
    aux=Node;
    if(Node->dado==target)
    {
        if(Node->next!=NULL)
        {
            aux=Node->next;
            free(Node);
            Node->dado=aux->dado;
            Node->next=aux->next;
        }
        else
        {
            aux=Node->before;
            free(Node);
            Node->dado=aux->dado;
            Node->next=NULL;
        }
    }
    if(Node->next!=NULL)
    {
        deleteValueInNode(target, Node->next);
    }    
    return Node;
}

No *deleteAllNodes(No *Node)
{
    No *aux;
    aux=Node;
    while(aux->next!=NULL)
    {
        aux = Node->next;
        free(Node);
        Node=aux;
    }
    free(Node);
    Node=NULL;
    return Node;
}

void imprimir(No *Node)
{
    if(Node!=NULL)
    {
        if (Node->next!=NULL);
        {
            No *novo;
            novo=Node->next;
            imprimir(novo);
        }
        printf("Dado = %d\n",Node->dado);
    }
}

int main()
{
    printf("------------------------ createNode\n");
    No *node = createNode(3);
    imprimir(node);
    printf("------------------------ insertInitNode\n");

    node = insertInitNode(2, node);
    node = insertInitNode(2, node);
    node = insertInitNode(1, node);
    imprimir(node);
    printf("------------------------ insertEndNode\n");

    node = insertEndNode(4, node);
    imprimir(node);
    printf("------------------------ findInNode\n");

    findInNode(3, node);
    findInNode(6, node);
    printf("------------------------ findAllInNodes\n");

    findAllInNodes(2, node);
    printf("------------------------ EditValueNode\n");

    node = EditValueNode(2, 3, node);
    imprimir(node);
    printf("------------------------ EditNode\n");

    node = EditNode(4, 6, node);
    imprimir(node);
    printf("------------------------ deleteValueInNode\n");

    node = deleteValueInNode(3, node);
    imprimir(node);
    printf("------------------------ deleteAllNodes\n");

    node = deleteAllNodes(node);
    imprimir(node);
    printf("------------------------\n");

    return 0;
}

Nenhum comentário: