quarta-feira, 6 de novembro de 2013

Lista Encadeada Simples


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// Titulo: Lista Encadeada Simples.
// Autor: Sinval Júnior.
// Inicio: 25/10/2013.
// Fim: 06/11/2013.

typedef struct node_type {
char name[254];
    int id;
    struct node_type *next;
}ArrayList;

// Lista de

ArrayList *MemoryAlloc(); // 100%
void Insert(ArrayList *headList, int id, char name[]); // 100%
int Remove(ArrayList *headList, int id); // 100%
void ShowList(ArrayList *headList); // 100%
int Search(ArrayList *headList, int id); // 100%
ArrayList *StartList(); // 100%
void PainelPrompt(); // 100%

// Função principal.
int main(int argc, char *argv[], double fly){

srand(time(NULL));

ArrayList *user = StartList();

PainelPrompt(user);

}

// Cria um endereço de memoria.
ArrayList *MemoryAlloc(){

    return malloc(sizeof(ArrayList));

}

// Função para inserir elemento na lista.
void Insert(ArrayList *headList, int id, char name[]){


printf("CHEGANDO: %d || %s", id, name);

// Percorre a lista ate o final.
while(headList->next != NULL){
headList = headList->next;
}

// Criação espaço para novo elemento.
headList->next = MemoryAlloc();
headList = headList->next;

// Preparando o final.
strcpy(headList->name, name);
headList->id = id;
headList->next = NULL;

}

// Imprime toda a lista na tela.
void ShowList(ArrayList *headList){

while(headList->next != NULL){

headList = headList->next;
printf("\n\nUser Name: %s", headList->name);
printf("\nUser ID: %d", headList->id);

}

}

// Função para remover elemento contido na lista.
int Remove(ArrayList *headList, int id){

ArrayList *tempList = headList; // Não é preciso usar a função MemoryAlloc().

while(headList->next != NULL){ // Percorre a Lista

headList = headList->next;

if(headList->id == id){ // Compara cada elemento da lista com o que se deseja remover.

if(headList->next != NULL){ // Verifica se o elemento é o ultimo.

tempList->next = headList->next;

free(headList);

return 1;

}else{ // Se for o ultimo tempList->next recebe NULL.

tempList->next = NULL;

free(headList);

return 1;

}
}else{
tempList = tempList->next;
}
}

return 0;

}

// Função para buscar um elemento na lista.
int Search(ArrayList *headList, int id){

while(headList->next != NULL){

headList = headList->next;

if(headList->id == id){

printf("\n\n------------------------------");
printf("\nUser Name: %s", headList->name);
printf("\nUser ID: %d", headList->id);
printf("\n------------------------------");

return 1;
}
}
return 0;

}

// Prepara para o proximo.
ArrayList *StartList(){

ArrayList *headList = MemoryAlloc();
headList->next = NULL;

return headList;
}

void PainelPrompt(ArrayList *headList){

int userCounter = 0, randomID = 0;
int userOption = -1;
char userName[254];

while(userOption != 0){
printf("\t\t[Insert Program Elements List]");
printf("\n\nPress the option to continue.\n1 - Insert.\n2 - Remove\n3 - Search\n4 - Print list.\n\n>:");

scanf("%d", &userOption);

switch(userOption){

case 1:

printf("Enter your name: ");

gets(userName);
gets(userName);

randomID = rand();

if(!Search(headList, randomID)){

userCounter++;

printf("\n\n\nMANDADO: %d, %s", randomID, userName);

Insert(headList, randomID, userName);

}else {

printf("ID existing name in the list.");

}

break;
case 2:

printf("\nEnter the ID to remove: ");

scanf("%d", &userOption);

Remove(headList, userOption);

break;
case 3:

printf("\nEnter the ID to search: ");
scanf("%d", &userOption);
Search(headList, userOption);

break;
case 4:
ShowList(headList);
default: printf("Enter a number in the existing panel.");

}
}

}

Nenhum comentário:

Postar um comentário