Guest User

Untitled

a guest
Jun 25th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. const int MAX = 10;
  6.  
  7. // Tipo base dos elementos da lista
  8. typedef struct elementos {
  9.   char nome[50];
  10.   // Outros elementos
  11. } t_elemento;
  12. // Estrutura da lista
  13. typedef struct lista {
  14.         t_elemento vetor[MAX];//vet que armazena elem. da pilha
  15.         int n; // posicao (indice) do ultimo elemento da lista
  16. } t_lista; // tipo lista
  17.  
  18.  
  19. t_lista criar(){
  20.         t_lista lista;
  21.         lista.n = -1;        
  22.        
  23.         return lista;
  24. }
  25.  
  26. int isVazia(t_lista * lista){
  27.     return (lista->n == -1);
  28. }
  29.  
  30. int isCheia(t_lista * lista){
  31.     return (lista->n == MAX-1);
  32. }
  33.  
  34. int getTamanho(t_lista * lista) {
  35.     return lista->n + 1;
  36. }
  37.  
  38. t_elemento * getElemento(t_lista * lista, int pos) {
  39.     if ((pos > lista->n) || (pos < 0))
  40.        return 0;
  41.        
  42.     return &(lista->vetor[pos]);
  43. }
  44.  
  45. int compara(t_elemento dado1, t_elemento dado2){
  46.     strcmp(dado1.nome, dado2.nome);
  47. }
  48.  
  49. int getPosicao(t_lista * lista, t_elemento dado){
  50.     int i;
  51.     for (i = 0; i <= lista->n; i++)
  52.         if (compara(lista->vetor[i], dado) == 0)
  53.            return i;
  54.            
  55.     return -1;
  56. }
  57.  
  58. int deslocaEsquerda(t_lista * lista, int pos) {
  59.     int i;
  60.     for (i=pos; i<=(lista->n); i++)
  61.         lista->vetor[i] = lista->vetor[i+1];
  62.        
  63.     return 1;
  64. }
  65.  
  66. int deslocaDireita(t_lista * lista, int pos) {
  67.     int i;
  68.     for (i=lista->n + 1; i>pos; i--)
  69.         lista->vetor[i] = lista->vetor[i-1];
  70.        
  71.     return 1;
  72. }
  73.  
  74.  
  75. int inserir (t_lista * lista, int pos, t_elemento dado) {
  76.     if ( isCheia(lista) || (pos > lista->n + 1) || (pos < 0) )
  77.        return 0;
  78.        
  79.     deslocaDireita(lista, pos);
  80.     lista->vetor[pos] = dado;
  81.     (lista->n)++;
  82.    
  83.     return 1;
  84. }
  85.  
  86. int remover (t_lista *lista, int pos) {
  87.     if ((pos > lista->n) || (pos < 0))
  88.        return 0;
  89.    
  90.     deslocaEsquerda(lista, pos);
  91.     (lista->n)--;
  92.    
  93.     return 1;
  94. }
  95.  
  96. int main(){
  97.     printf("Programa de teste para lista sequencial\n\n");
  98.    
  99.     system("pause");
  100.     return 0;    
  101. }
Add Comment
Please, Sign In to add comment