Você está na página 1de 5

#include <stdio.

h>
#include <stdlib.h>

struct node
{
int data;
struct node *next;
};

struct node *start = NULL;

struct node *CriarListaLigada(struct node *);


struct node *ImprimirListaLigada(struct node *);
struct node *InsereInicioLista(struct node *);
struct node *InsereFimLista(struct node *);
struct node *InsereAntes(struct node *);
struct node *InsereDepois(struct node *);
struct node *DeletaNoInicio(struct node *);
struct node *DeletaNoFim(struct node *);
struct node *DeletaNoEspecifico(struct node *);
struct node *DeletaDepois(struct node *);
struct node *DeletaTudo(struct node *);
struct node *OrdenaLista(struct node *);

int main()
{
int option;
do
{
printf("\n\n *****MAIN MENU *****");
printf("\n 1: Cria elemento e insere na lista");
printf("\n 2: Imprimi a Lista");
printf("\n 3: Adiciona no no inicio da lista");
printf("\n 4: Adiciona no no fim da lista");
printf("\n 5: Adiciona no antes de um dado no");
printf("\n 6: Adiciona no depois de um dado no");
printf("\n 7: Deleta um no no inicio da lista");
printf("\n 8: Deleta um no no fim da lista");
printf("\n 9: Deleta um no especifico");
printf("\n 10: Deleta um no depois de um no especifico");
printf("\n 11: Deleta toda a Lista");
printf("\n 12: Ordena a lista");
printf("\n 13: Sair");
printf("\n\n Informe uma opcao: ");
scanf("%d", &option);

if (option > 1 && option < 14 && start == NULL)


{
if (start == NULL)
{
printf("Primeiro crie a lista com algum no, opcao 1.\n");
continue;
}
}

switch (option)
{
case 1:
start = CriarListaLigada(start);
printf("\n Lista Ligada Criada");
break;
case 2:
start = ImprimirListaLigada(start);
break;
case 3:
start = InsereInicioLista(start);
break;
case 4:
start = InsereFimLista(start);
break;
case 5:
start = InsereAntes(start);
break;
case 6:
start = InsereDepois(start);
break;
case 7:
start = DeletaNoInicio(start);
break;
case 8:
start = DeletaNoFim(start);
break;
case 9:
start = DeletaNoEspecifico(start);
break;
case 10:
start = DeletaDepois(start);
break;
case 11:
start = DeletaTudo(start);
printf("\n Lista Ligada Deletada");
break;
case 12:
start = OrdenaLista(start);
break;
}
} while (option != 13);

return 0;
}

struct node *CriarListaLigada(struct node *start)


{
struct node *new_node, *ptr;
int num;

printf("\n Enter -1 to end");


printf("\n Enter the data : ");
scanf("%d", &num);

while (num != -1)


{
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = num;

if (start == NULL)
{
new_node->next = NULL;
start = new_node;
}
else
{
ptr = start;

while (ptr->next != NULL)


ptr = ptr->next;

ptr->next = new_node;
new_node->next = NULL;
}

printf("\n Enter the data : ");


scanf("%d", &num);
}

return start;
}

struct node *ImprimirListaLigada(struct node *start)


{
struct node *ptr;
ptr = start;

while (ptr != NULL)


{
printf("\t %d", ptr->data);
ptr = ptr->next;
}

return start;
}

struct node *InsereInicioLista(struct node *start)


{
struct node *new_node;
int num;

printf("\n Enter the data : ");


scanf("%d", &num);

new_node = (struct node *)malloc(sizeof(struct node));


new_node->data = num;
new_node->next = start;
start = new_node;

return start;
}

struct node *InsereFimLista(struct node *start)


{
struct node *ptr, *new_node;
int num;

printf("\n Enter the data : ");


scanf("%d", &num);

new_node = (struct node *)malloc(sizeof(struct node));


new_node->data = num;
new_node->next = NULL;

ptr = start;

while (ptr->next != NULL)


ptr = ptr->next;

ptr->next = new_node;

return start;
}

struct node *InsereAntes(struct node *start)


{
struct node *new_node, *ptr, *preptr;
int num, val;

printf("\n Enter the data : ");


scanf("%d", &num);

printf("\n Enter the value before which the data has to be inserted : ");
scanf("%d", &val);

new_node = (struct node *)malloc(sizeof(struct node));


new_node->data = num;

ptr = start;

while (ptr->data != val)


{
preptr = ptr;
ptr = ptr->next;
}

preptr->next = new_node;
new_node->next = ptr;

return start;
}

struct node *InsereDepois(struct node *start)


{
struct node *new_node, *ptr, *preptr;
int num, val;

printf("\n Enter the data : ");


scanf("%d", &num);

printf("\n Enter the value after which the data has to be inserted : ");
scanf("%d", &val);

new_node = (struct node *)malloc(sizeof(struct node));


new_node->data = num;

ptr = start;
preptr = ptr;

while (preptr->data != val)


{
preptr = ptr;
ptr = ptr->next;
}

preptr->next = new_node;
new_node->next = ptr;

return start;
}

struct node *DeletaNoInicio(struct node *start)


{
struct node *ptr;
ptr = start;
start = start->next;
free(ptr);

return start;
}

struct node *DeletaNoFim(struct node *start)


{
struct node *ptr, *preptr;
ptr = start;

while (ptr->next != NULL)


{

Você também pode gostar