[ create a new paste ] login | about

Link: http://codepad.org/ybm8Rm82    [ raw code | output | fork ]

tek6 - C++, pasted on Oct 3:
#include "stdio.h"
#include "stdlib.h"

#define SIZE_MAX 100

typedef int TListPos;
typedef int TListKey;

struct TList {
         int size;
         TListKey keys[SIZE_MAX];
       };

void createlist (TList *l);                     // Cria uma lista l vazia
bool emptylist (TList l);                       // Retorna V se l estiver vazia; e F, caso contrario
int listsize (TList l);                         // Retorna o tamanho da lista l
TListPos getpos (int i, TList l);               // Retorna a posição do iº elemento da lista l. Se i for maior que o tamanho da lista, retorna a posicao final da lista.
TListKey getkey (TListPos p, TList l);          // Retorna a chave da posição p
bool final (TListPos p, TList l);               // Retorna V se p é o final da lista; e F, caso contrario
TListPos first (TList l);                       // Retorna a posicao do primeiro elemento da lista
TListPos last (TList l);                        // Retorna a posicao do ultimo elemento da lista. Se a lista estiver vazia, retorna a primeira posicao
TListPos next (TListPos p, TList l);            // Retorna a posicao seguinte a p, na lista. Se p for final, retorna p
int indexof (TListKey k, TList l);              // Retorna o indice da chave k na lista l. Se k não estiver em l, retorna 0
void insert (TListKey k, TListPos p, TList *l); // Insere a chave k na posicao p da lista l
void remove (TListPos p, TList *l);             // Remove o elemento da posicao p na lista l
void printlist (TList l, char *s);              // Imprime os elementos da lista l, precedidos do texto em s
void insert(TListKey k, TListPos p, TList *l);
void sort(TList *l);
void remove (TListPos p, TList *l);		// Remove o item na posiçao

void createlist (TList *l) {
  (*l).size = 0;
}
     
bool emptylist (TList l) {
  return l.size == 0;
}

int listsize (TList l) {
  return l.size;
}
    
TListPos getpos (int i, TList l) {
  return (i >= 1) && (i <= l.size)? i - 1: l.size;
}

TListKey getkey (TListPos p, TList l) {
  return l.keys[p];
}

bool final (TListPos p, TList l) {
  return p == l.size;
}

TListPos first (TList l) {
  return 0;
}

TListPos last (TList l) {
  return emptylist(l)? 0: l.size - 1;
}

TListPos next (TListPos p, TList l) {
  return final(p , l)? p : p + 1;
}

int indexof (TListKey k, TList l) {        
  int i = 0;                                
  TListPos p;
  
  for ( p = first(l); !final(p, l); p = next(p, l) ) {
    i++;
    
    if ( getkey(p, l) == k)
      return i;
  }
  
  return 0;
}

void insert (TListKey k, TListPos p, TList *l) {
  if ((*l).size < SIZE_MAX) {    
    int i;
    
    for (i = (*l).size; i > p; i--)
      (*l).keys[i] = (*l).keys[i - 1];
    
    (*l).keys[p] = k;
    (*l).size++;
  }
}

void printlist (TList l, char *s) {
  printf("%s", s);
  
  if (emptylist(l))
    printf(" vazia!");
  else {
    TListPos p = first(l);
    
    while(!final(p, l)) {      
      printf(" %d", getkey(p, l));
      p = next(p, l);
    }
  }
  
  printf("\n");
}

void insertord (TListKey k, TList *l) {
  if (emptylist(*l))
    insert(k, first(*l), l);
  else {
    TListPos p = first(*l);
    
    while ((!final(p, *l)) && (getkey(p, *l) < k))
      p = next(p, *l);
      
    insert(k, p, l);
  }
}

void sort(TList *l) {
  TList tmp;
  TListKey k;
  
  createlist(&tmp);
  
  while (!emptylist(*l)) {
    k = getkey(first(*l), *l);
    remove(first(*l), l);
    insertord(k, &tmp);
  }          
  
  while (!emptylist(tmp)) {
    k = getkey(first(tmp), tmp);
    remove(first(tmp), &tmp);
    insert(k, next(last(*l), *l), l);  
  }    
}

void remove (TListPos p, TList *l) {
  int i;
  
  for (i = p; i < (*l).size - 1; i++)
    (*l).keys[i] = (*l).keys[i + 1];
  
  (*l).size--;
}


Output:
1
2
In function `_start':
undefined reference to `main'


Create a new paste based on this one


Comments: