next up previous contents
suivant: Tri fusion monter: Corrigés des programmes précédent: Complexes   Table des matières

Listes chaînées

linkedList.c


#include"linkedList.h"
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

/*----------------------------------------*/

void error()
{
  printf("error");
  exit(0);
}

/*----------------------------------------*/

int linkedListGetSize(linkedList* l)
{
  return l->size;
}

/*----------------------------------------*/


linkedList* linkedListCreate()
{
  linkedList* l;
  l = (linkedList*)malloc(sizeof(linkedList));
  if(l == NULL) 
    error();
  l->first = NULL;
  l->last = NULL;
  l->size = 0;
  return l;
}

/*----------------------------------------*/


link* linkedListCreateLink(void* x)
{
  link* lk = (link*)malloc(sizeof(link));
  if (lk == NULL) error();
  lk->data = x;
  lk->next = NULL;
  return lk;
}

/*----------------------------------------*/


void linkedListAppendLink(linkedList* l, link* lk)
{
  if (l->last == NULL)
    {
      l->first = lk;
      l->last = l->first;
    }
  else
    {
      l->last->next = lk;
      l->last = lk;
    }
  lk->next = NULL;
  l->size ++;
}

/*----------------------------------------*/


void linkedListAppend(linkedList* l, void* x)
{
  link* lk = linkedListCreateLink(x);
  linkedListAppendLink(l, lk);
}

/*----------------------------------------*/


void linkedListPushLink(linkedList* l, link* lk)
{
  if (l->first == NULL)
    {
      l->first = lk;
      l->last = l->first;
    }
  else
    {
      lk->next = l->first;
      l->first = lk;
    }
  l->size ++;
}

/*----------------------------------------*/


void linkedListPush(linkedList* l, void* x)
{
  link* lk = linkedListCreateLink(x);
  linkedListPushLink(l, lk);
}

/*----------------------------------------*/


link* linkedListGetFirst(linkedList* l)
{
  return l->first;
}

/*----------------------------------------*/


link* linkedListUnlinkFirst(linkedList* l)
{
  link* f = linkedListGetFirst(l);
  if (f != NULL)
    {
      l->first = l->first->next;
      if (l->last == f)
	l->last = NULL;
      l->size--;
    }
  return f;
}

/*----------------------------------------*/

void linkedListConcat(linkedList* begin, linkedList* end)
{
  if (begin->size != 0)
      begin->last->next = end->first;
  else
      begin->first = end->first;
  if(end->size != 0)
    begin->last = end->last;
  end->first = NULL;
  end->last = NULL;
  begin->size += end->size;
  end->size = 0;
}

/*----------------------------------------*/

linkedList* linkedListMap(linkedList* l, void* (*f)(void*))
{
  linkedList* lBis = linkedListCreate();
  link* lk = linkedListGetFirst(l);
  while(lk!=NULL)
    {
      linkedListAppend(lBis, f(lk->data));
      lk = lk->next; 
    }
  return lBis;  
}

/*----------------------------------------*/

void linkedListApply(linkedList* l, void (*f)(void*))
{
  link* lk = linkedListGetFirst(l);
  while(lk!=NULL)
    {
      f(lk->data);
      lk = lk->next; 
    }
}

/*----------------------------------------*/

 
void linkDestroy(link* l, void (*fr)(void*))
{
  if (l != NULL)
    {
      linkDestroy(l->next, fr);
      fr(l->data);
      free(l);
    }
}

/*----------------------------------------*/


void linkedListDestroy(linkedList* l, void (*fr)(void*))
{
  linkDestroy(l->first, fr);
  free(l);  
}

/*----------------------------------------*/



/*
void destroyInt(void* i)
{
  free((int*)i);
}

void printInt(void* i)
{
  printf("%d -> ", *((int*)i));
}

void* copyInt(void* i)
{
  void* v = malloc(sizeof(int));
  if (v == NULL)
    error();
  *((int*)v) = *((int*)i);
  return v;
}

int main()
{
  int i = 1;
  int* k;
  linkedList* l = linkedListCreate();
  linkedList* lBis;
  for(i = 0 ; i < 100 ; i++) 
    {
      k = (int*)malloc(sizeof(int));
      *k = i;
      linkedListAppend(l, k);
    }
  //linkedListApply(l, printInt);
  lBis = linkedListMap(l, copyInt);
  //linkedListApply(lBis, printInt);
  linkedListConcat(l, lBis);
  linkedListApply(l, printInt);
  linkedListDestroy(l, destroyInt);
  linkedListDestroy(lBis, destroyInt);
  return 0;
}

*/



klaus 2010-08-05