Título: Lista Encadeada
Linguagem: C/C++
S.O.: DOS/Windows
Autor(es): Wenderson Teixeira


Lista Encadeada

linklist.h

#ifndef _LINKLIST_H 
#define _LINKLIST_H 

#define false 0
#define true 1

#define UP 0
#define DOWN 1

typedef int bool;

// Defnindo o tipo TList: 

typedef struct tagList
{
  void *Data;
  struct tagList *Next;
} TList;

TList *Add(TList *root, void *item);
TList *Find(TList *root, void *item, bool (*IsEqual)(void *, void *));
void BubbleSort(TList *root, int order, int (*Compare)(void *, void *));
TList *Remove(TList *root, void *item, bool (*IsEqual)(void *, void *),
             void (*Delete)(void*));

#endif 
Criando as funções si: linklist.c

#include <alloc.h> 
#include "linklist.h" 

// Criando a função Add: 
TList *Add(TList *root, void *item)
{
  TList *iter = root;
  if(iter)
  {
    while(iter->Next)
      iter = iter->Next;
    iter->Next = (TList *)malloc(sizeof(TList));
    iter = iter->Next;
  }
  else
    iter = (TList *)malloc(sizeof(TList));
  iter->Data = item;
  iter->Next = NULL;
  return root ? root : iter;
}


// Criando a função Find:
TList *Find(TList *root, void *item, bool (*IsEqual)(void *, void *))
{
  TList *iter = root;
  while(iter ? !IsEqual(iter->Data, item) : false)
    iter = iter->Next;
  return iter;
}


// Criando a função BubbleSort: 
void BubbleSort(TList *root, int order, int (*Compare)(void *, void *))
{
  TList *begin;
  begin = root;
  while(begin && begin->Next)
  {
    TList *iter = begin->Next;
    while(iter)
    {
      int result = Compare(begin->Data, iter->Data);
      bool bCanSwap = (order == UP) ?
                         (result > 0? true : false) : 
                         (result > 0? false : true);

      if(bCanSwap)
      {
        void *swap = begin->Data;
        begin->Data = iter->Data;
        iter->Data = swap;
      }

      iter = iter->Next;
    }

  begin = begin->Next;
  }
}


// Criando a função Remove: 
TList *Remove(TList *root, void *item, bool (*IsEqual)(void *, void *),
             void (*Delete)(void *))
{
  TList *iter1 = root, *iter2 = NULL;
  while(iter1)
  {
    if(IsEqual(iter1->Data, item))
    {
      if(iter2)
      {
        iter2->Next = iter1->Next;
        iter2 = root;
      }
      else
        iter2 = iter1->Next;

      Delete(iter1->Data);
      free(iter1);

      return iter2;
    }

    iter2 = iter1;
    iter1 = iter1->Next;
  }

  return root;
}
listtest.c

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <alloc.h> 
#include <string.h> 
#include <conio.h> 
#include "linklist.h" 


bool IsEqual(void *i1, void *i2)
{
  return stricmp(i1, i2) == 0;
}

int Compare(void *i1, void *i2)
{
  return stricmp(i1, i2); 
} 

void Delete(void *item) 
{ 
  free(item); 
} 

TList *FindItem(TList *root, void *item) 
{ 
  return Find(root, item, IsEqual); 
} 

void SortList(TList *root, int order) 
{ 
  BubbleSort(root, order, Compare); 
} 

TList *RemoveItem(TList *root, char *item) 
{ 
  return Remove(root, item, IsEqual, Delete); 
} 

void DisplayList(TList *root) 
{ 
  TList *iter = root; 
  while(iter) 
  { 
    printf(" %s", iter->Data); 
    iter = iter->Next; 
  } 
} 

const char *names[] = { 
   "Pera", "Manga", "Maça", 
   "Uva", "Laranja", "Goiaba", 
   "Abacaxi", "Morango", "Cajú", 
   "Melão", "Banana", NULL 
    }; 

///// Main Program /////////// 
int main() 
{ 
  TList *root = NULL, *pItem; 
  int c = 0, count; 

  // Adiciona itens com valores aleatorios na lista 
  randomize(); 
  for(count = 0; names[count]; count++) 
  { 
    char *item = (char *)malloc(sizeof(char) * 30); 
    strcpy(item, names[count]); 
    root = Add(root, item); 
  } 

  // Mostra lista preenchida 
  printf("\nNormal:\n");

  DisplayList(root); 

  // Ordena lista em ordem crescente 
  printf("\nOrdenada Crescente:\n"); 
  SortList(root, UP); 
  DisplayList(root); 

  // Ordena lista em ordem decrescente 
  printf("\nOrdenada Decrescente:\n"); 
  SortList(root, DOWN); 
  DisplayList(root); 

  c = random(count); 
  pItem = FindItem(root, names[c]); 
  if(pItem) 
    printf("\nItem %s encontrado\n", pItem->Data); 
  else 
    printf("\nItem %s nao encontrado\n", names[c]); 

  pItem = FindItem(root, "Abacate"); 
  if(pItem) 
    printf("\nItem %s encontrado\n", "Abacate"); 
  else 
    printf("\nItem %s nao encontrado\n", "Abacate"); 

  getch(); 

  // Apaga toda a lista 
  while(root) 
  { 
    printf("\nApagando Item %s\n", root->Data); 
    root = RemoveItem(root, root->Data); 
    DisplayList(root); 
  } 

  return 0; 
} 

1