-
Notifications
You must be signed in to change notification settings - Fork 0
/
LISTA.C
756 lines (568 loc) · 16.2 KB
/
LISTA.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/***************************************************************************
* $MCI Módulo de implementação: LIS Lista duplamente encadeada
*
* Arquivo gerado: LISTA.c
* Letras identificadoras: LIS
*
* Nome da base de software: Arcabouço para a automação de testes de programas redigidos em C
* Arquivo da base de software: D:\AUTOTEST\PROJETOS\LISTA.BSW
*
* Projeto: INF 1301 / 1628 Automatização dos testes de módulos C
* Gestor: LES/DI/PUC-Rio
* Autores: avs
*
* $HA Histórico de evolução:
* Versão Autor Data Observações
* 4 avs 01/fev/2006 criar linguagem script simbólica
* 3 avs 08/dez/2004 uniformização dos exemplos
* 2 avs 07/jul/2003 unificação de todos os módulos em um só projeto
* 1 avs 16/abr/2003 início desenvolvimento
*
***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <malloc.h>
#include <assert.h>
#ifdef _DEBUG
#include "CESPDIN.H"
#endif
#define LISTA_OWN
#include "LISTA.h"
#undef LISTA_OWN
/***********************************************************************
*
* $TC Tipo de dados: LIS Elemento da lista
*
*
***********************************************************************/
typedef struct tagElemLista {
void * pValor ;
/* Ponteiro para o valor contido no elemento */
struct tagElemLista * pAnt ;
/* Ponteiro para o elemento predecessor */
struct tagElemLista * pProx ;
/* Ponteiro para o elemento sucessor */
#ifdef _DEBUG
char tipo[50];
#endif
} tpElemLista ;
/***********************************************************************
*
* $TC Tipo de dados: LIS Descritor da cabeça de lista
*
*
***********************************************************************/
typedef struct LIS_tagLista {
tpElemLista * pOrigemLista ;
/* Ponteiro para a origem da lista */
tpElemLista * pFimLista ;
/* Ponteiro para o final da lista */
tpElemLista * pElemCorr ;
/* Ponteiro para o elemento corrente da lista */
int numElem ;
/* Número de elementos da lista */
void ( * ExcluirValor ) ( void * pValor ) ;
/* Ponteiro para a função de destruição do valor contido em um elemento */
} LIS_tpLista ;
/***** Protótipos das funções encapuladas no módulo *****/
static void LiberarElemento( LIS_tppLista pLista ,
tpElemLista * pElem ) ;
static tpElemLista * CriarElemento( LIS_tppLista pLista ,
void * pValor ,
char * tipo ) ;
static void LimparCabeca( LIS_tppLista pLista ) ;
/***** Código das funções exportadas pelo módulo *****/
#ifdef _DEBUG
static char lixo[256] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
//ação 1: libera elemento corrente da estrutura escolhida
int LIS_LiberaElemCorrente(LIS_tppLista pLista)
{
free( pLista->pElemCorr ) ;
return 1;
}
//ação 2: atribui null para o sucesso
int LIS_SucesNULL(LIS_tppLista pLista)
{
pLista->pElemCorr->pProx = NULL ;
return 1;
}
//ação 3: atribui null para o antecessor
int LIS_PreNULL(LIS_tppLista pLista)
{
pLista->pElemCorr->pAnt = NULL ;
return 1;
}
//ação 4: atribui lixo para o sucesso
int LIS_LixoSucessor(LIS_tppLista pLista)
{
pLista->pElemCorr->pProx = (struct tagElemLista *) lixo;
return 1;
}
//ação 5: atribui lixo para o precessor
int LIS_LixoPre(LIS_tppLista pLista)
{
pLista->pElemCorr->pProx = (struct tagElemLista *) lixo;
return 1;
}
//ação 6: atribui NULL para o conteudo do nó
int LIS_NULLConteudo(LIS_tppLista pLista)
{
pLista->pElemCorr->pValor = NULL ;
return 1;
}
//ação 7: altera o tipo do nó
int LIS_AlteraTipoDoConteudo(LIS_tppLista pLista)
{
strcpy(pLista->pElemCorr->tipo, "tipo_errado");
return 1;
}
//ação 8: destaca o conteudo
int LIS_DestacaConteudo(LIS_tppLista pLista)
{
pLista->pElemCorr->pAnt->pProx = pLista->pElemCorr->pProx ;
pLista->numElem--;
pLista->pElemCorr->pProx->pAnt = pLista->pElemCorr->pAnt ;
return 1;
}
//ação 9: corrente nulo
int LIS_NULLCorrente(LIS_tppLista pLista)
{
pLista->pElemCorr = NULL ;
return 1;
}
//ação 10: origim nulo
int LIS_NULLOrigem(LIS_tppLista pLista)
{
pLista->pOrigemLista = NULL ;
return 1;
}
#endif
#ifdef _DEBUG
LIS_tpCondRet LIS_VerificaLiberaElemCorrente(LIS_tppLista pLista){
if(pLista->pElemCorr == NULL)
return LIS_CondRetListaNaoExiste;
}
LIS_tpCondRet LIS_VerificaSucessorNULL(LIS_tppLista pLista){
LIS_tppLista lis;
IrFinalLista(pLista);
for(lis = pLista; lis->pElemCorr->pAnt != NULL; lis->pElemCorr = lis->pElemCorr->pAnt)
if(lis->pElemCorr->pAnt->pProx == NULL)
return LIS_CondRetSucessorNulo;
if(lis->pElemCorr->pProx == NULL)
return LIS_CondRetSucessorNulo;
return LIS_CondRetOK;
}
LIS_tpCondRet LIS_VerificaPredecessorNULL(LIS_tppLista pLista){
LIS_tppLista lis;
IrInicioLista(pLista);
for(lis = pLista; lis->pElemCorr->pProx != NULL; lis->pElemCorr = lis->pElemCorr->pProx)
if(lis->pElemCorr->pProx->pAnt == NULL)
return LIS_CondRetPredecessor;
if(lis->pElemCorr->pAnt == NULL)
return LIS_CondRetPredecessor;
return LIS_CondRetOK;
}
LIS_tpCondRet LIS_VerificaLixoSucessor(LIS_tppLista pLista){
LIS_tppLista lis;
IrFinalLista(pLista);
for(lis = pLista; lis->pElemCorr->pAnt != NULL; lis->pElemCorr = lis->pElemCorr->pAnt)
if(lis->pElemCorr->pAnt->pProx != NULL && lis->pElemCorr->pAnt->pProx != lis->pElemCorr)
return LIS_CondRetSucessorLixo;
if(lis->pElemCorr->pProx != NULL && lis->pElemCorr->pProx->pAnt->pProx != lis->pElemCorr->pProx)
return LIS_CondRetSucessorLixo;
return LIS_CondRetOK;
}
LIS_tpCondRet LIS_VerificaLixoPredecessor(LIS_tppLista pLista){
LIS_tppLista lis;
IrInicioLista(pLista);
for(lis = pLista; lis->pElemCorr->pProx != NULL && lis->pElemCorr->pProx->pAnt == lis->pElemCorr; lis->pElemCorr = lis->pElemCorr->pProx)
if(lis->pElemCorr->pProx->pAnt != NULL && lis->pElemCorr->pProx->pAnt != lis->pElemCorr)
return LIS_CondRetPredecessorLixo;
if(lis->pElemCorr->pAnt != NULL && lis->pElemCorr->pAnt->pProx != lis->pElemCorr)
return LIS_CondRetPredecessorLixo;
return LIS_CondRetOK;
}
LIS_tpCondRet LIS_VerificaConteudoNulo(LIS_tppLista pLista){
return (pLista->pElemCorr->pValor == NULL) ? LIS_CondRetConteudoNulo : LIS_CondRetOK;
}
LIS_tpCondRet LIS_VerificaTipoConteudo(LIS_tppLista pLista, char * tipo){
return (strcmp(pLista->pElemCorr->tipo,tipo) == 0 ? LIS_CondRetOK : LIS_CondRetTipoInvalido);
}
LIS_tpCondRet LIS_VerificaCorrenteNulo(LIS_tppLista pLista){
return (pLista->pElemCorr == NULL) ? LIS_CondRetCorrenteNull : LIS_CondRetOK;
}
LIS_tpCondRet LIS_pOrigemNulo(LIS_tppLista pLista){
return (pLista->pOrigemLista == NULL) ? LIS_CondRetCorrenteNull : LIS_CondRetOK;
}
//confere numero da quantidade com o numero de movimentos possiveis dentro da lista
LIS_tpCondRet LIS_confereNumero(LIS_tppLista pLista){
int i, j;
LIS_tppLista lis;
IrInicioLista(pLista);
for(lis = pLista, i = 0; lis->pElemCorr->pProx != NULL; lis->pElemCorr = lis->pElemCorr->pProx, i++);
IrFinalLista(pLista);
for(lis = pLista, j = 0; lis->pElemCorr->pAnt != NULL; lis->pElemCorr = lis->pElemCorr->pAnt, j++);
return ((i > j ? i : j < pLista->numElem) ? LIS_CondRetOK : LIS_CondRetNaoAchou);
}
#endif
int LIS_NumeroDeElementos(LIS_tppLista pLista){
return pLista->numElem;
}
/***************************************************************************
*
* Função: LIS &Criar lista
* ****/
LIS_tppLista LIS_CriarLista(
void ( * ExcluirValor ) ( void * pDado ) )
{
LIS_tpLista * pLista = NULL ;
pLista = ( LIS_tpLista * ) malloc( sizeof( LIS_tpLista )) ;
if ( pLista == NULL )
{
return NULL ;
} /* if */
#ifdef _DEBUG
CED_MarcarEspacoAtivo(pLista);
#endif
LimparCabeca( pLista ) ;
pLista->ExcluirValor = ExcluirValor ;
return pLista ;
} /* Fim função: LIS &Criar lista */
/***************************************************************************
*
* Função: LIS &Destruir lista
* ****/
void LIS_DestruirLista( LIS_tppLista pLista )
{
#ifdef _DEBUG
CED_MarcarEspacoNaoAtivo(pLista);
#endif
#ifdef _DEBUG
assert( pLista != NULL ) ;
#endif
LIS_EsvaziarLista( pLista ) ;
free( pLista ) ;
} /* Fim função: LIS &Destruir lista */
/***************************************************************************
*
* Função: LIS &Esvaziar lista
* ****/
void LIS_EsvaziarLista( LIS_tppLista pLista )
{
tpElemLista * pElem ;
tpElemLista * pProx ;
#ifdef _DEBUG
assert( pLista != NULL ) ;
#endif
pElem = pLista->pOrigemLista ;
while ( pElem != NULL )
{
pProx = pElem->pProx ;
LiberarElemento( pLista , pElem ) ;
pElem = pProx ;
} /* while */
LimparCabeca( pLista ) ;
} /* Fim função: LIS &Esvaziar lista */
/***************************************************************************
*
* Função: LIS &Inserir elemento antes
* ****/
LIS_tpCondRet LIS_InserirElementoAntes( LIS_tppLista pLista ,
void * pValor,
char * tipo)
{
tpElemLista * pElem ;
#ifdef _DEBUG
assert( pLista != NULL ) ;
#endif
/* Criar elemento a inerir antes */
pElem = CriarElemento( pLista , pValor, tipo ) ;
if ( pElem == NULL )
{
return LIS_CondRetFaltouMemoria ;
} /* if */
/* Encadear o elemento antes do elemento corrente */
if ( pLista->pElemCorr == NULL )
{
pLista->pOrigemLista = pElem ;
pLista->pFimLista = pElem ;
} else
{
if ( pLista->pElemCorr->pAnt != NULL )
{
pElem->pAnt = pLista->pElemCorr->pAnt ;
pLista->pElemCorr->pAnt->pProx = pElem ;
} else
{
pLista->pOrigemLista = pElem ;
} /* if */
pElem->pProx = pLista->pElemCorr ;
pLista->pElemCorr->pAnt = pElem ;
} /* if */
pLista->pElemCorr = pElem ;
return LIS_CondRetOK ;
} /* Fim função: LIS &Inserir elemento antes */
/***************************************************************************
*
* Função: LIS &Inserir elemento após
* ****/
LIS_tpCondRet LIS_InserirElementoApos( LIS_tppLista pLista ,
void * pValor ,
char * tipo)
{
tpElemLista * pElem ;
#ifdef _DEBUG
assert( pLista != NULL ) ;
#endif
/* Criar elemento a inerir após */
pElem = CriarElemento( pLista , pValor , tipo) ;
if ( pElem == NULL )
{
return LIS_CondRetFaltouMemoria ;
} /* if */
/* Encadear o elemento após o elemento */
if ( pLista->pElemCorr == NULL )
{
pLista->pOrigemLista = pElem ;
pLista->pFimLista = pElem ;
} else
{
if ( pLista->pElemCorr->pProx != NULL )
{
pElem->pProx = pLista->pElemCorr->pProx ;
pLista->pElemCorr->pProx->pAnt = pElem ;
} else
{
pLista->pFimLista = pElem ;
} /* if */
pElem->pAnt = pLista->pElemCorr ;
pLista->pElemCorr->pProx = pElem ;
} /* if */
pLista->pElemCorr = pElem ;
return LIS_CondRetOK ;
} /* Fim função: LIS &Inserir elemento após */
/***************************************************************************
*
* Função: LIS &Excluir elemento
* ****/
LIS_tpCondRet LIS_ExcluirElemento( LIS_tppLista pLista )
{
tpElemLista * pElem ;
#ifdef _DEBUG
CED_MarcarEspacoNaoAtivo(pLista->pElemCorr);
#endif
#ifdef _DEBUG
assert( pLista != NULL ) ;
#endif
if ( pLista->pElemCorr == NULL )
{
return LIS_CondRetListaVazia ;
} /* if */
pElem = pLista->pElemCorr ;
/* Desencadeia à esquerda */
if ( pElem->pAnt != NULL )
{
pElem->pAnt->pProx = pElem->pProx ;
pLista->pElemCorr = pElem->pAnt ;
} else {
pLista->pElemCorr = pElem->pProx ;
pLista->pOrigemLista = pLista->pElemCorr ;
} /* if */
/* Desencadeia à direita */
if ( pElem->pProx != NULL )
{
pElem->pProx->pAnt = pElem->pAnt ;
} else
{
pLista->pFimLista = pElem->pAnt ;
} /* if */
LiberarElemento( pLista , pElem ) ;
return LIS_CondRetOK ;
} /* Fim função: LIS &Excluir elemento */
/***************************************************************************
*
* Função: LIS &Obter referência para o valor contido no elemento
* ****/
void * LIS_ObterValor( LIS_tppLista pLista )
{
#ifdef _DEBUG
assert( pLista != NULL ) ;
#endif
if ( pLista->pElemCorr == NULL )
{
return NULL ;
} /* if */
return pLista->pElemCorr->pValor ;
} /* Fim função: LIS &Obter referência para o valor contido no elemento */
/***************************************************************************
*
* Função: LIS &Ir para o elemento inicial
* ****/
void IrInicioLista( LIS_tppLista pLista )
{
#ifdef _DEBUG
assert( pLista != NULL ) ;
#endif
pLista->pElemCorr = pLista->pOrigemLista ;
} /* Fim função: LIS &Ir para o elemento inicial */
/***************************************************************************
*
* Função: LIS &Ir para o elemento final
* ****/
void IrFinalLista( LIS_tppLista pLista )
{
#ifdef _DEBUG
assert( pLista != NULL ) ;
#endif
pLista->pElemCorr = pLista->pFimLista ;
} /* Fim função: LIS &Ir para o elemento final */
/***************************************************************************
*
* Função: LIS &Avançar elemento
* ****/
LIS_tpCondRet LIS_AvancarElementoCorrente( LIS_tppLista pLista ,
int numElem )
{
int i ;
tpElemLista * pElem ;
#ifdef _DEBUG
assert( pLista != NULL ) ;
#endif
/* Tratar lista vazia */
if ( pLista->pElemCorr == NULL )
{
return LIS_CondRetListaVazia ;
} /* fim ativa: Tratar lista vazia */
/* Tratar avançar para frente */
if ( numElem > 0 )
{
pElem = pLista->pElemCorr ;
for( i = numElem ; i > 0 ; i-- )
{
if ( pElem == NULL )
{
break ;
} /* if */
pElem = pElem->pProx ;
} /* for */
if ( pElem != NULL )
{
pLista->pElemCorr = pElem ;
return LIS_CondRetOK ;
} /* if */
pLista->pElemCorr = pLista->pFimLista ;
return LIS_CondRetFimLista ;
} /* fim ativa: Tratar avançar para frente */
/* Tratar avançar para trás */
else if ( numElem < 0 )
{
pElem = pLista->pElemCorr ;
for( i = numElem ; i < 0 ; i++ )
{
if ( pElem == NULL )
{
break ;
} /* if */
pElem = pElem->pAnt ;
} /* for */
if ( pElem != NULL )
{
pLista->pElemCorr = pElem ;
return LIS_CondRetOK ;
} /* if */
pLista->pElemCorr = pLista->pOrigemLista ;
return LIS_CondRetFimLista ;
} /* fim ativa: Tratar avançar para trás */
/* Tratar não avançar */
return LIS_CondRetOK ;
} /* Fim função: LIS &Avançar elemento */
/***************************************************************************
*
* Função: LIS &Procurar elemento contendo valor
* ****/
LIS_tpCondRet LIS_ProcurarValor( LIS_tppLista pLista ,
void * pValor )
{
tpElemLista * pElem ;
#ifdef _DEBUG
assert( pLista != NULL ) ;
#endif
if ( pLista->pElemCorr == NULL )
{
return LIS_CondRetListaVazia ;
} /* if */
for ( pElem = pLista->pElemCorr ;
pElem != NULL ;
pElem = pElem->pProx )
{
if ( pElem->pValor == pValor )
{
pLista->pElemCorr = pElem ;
return LIS_CondRetOK ;
} /* if */
} /* for */
return LIS_CondRetNaoAchou ;
} /* Fim função: LIS &Procurar elemento contendo valor */
/***** Código das funções encapsuladas no módulo *****/
/***********************************************************************
*
* $FC Função: LIS -Liberar elemento da lista
*
* $ED Descrição da função
* Elimina os espaços apontados pelo valor do elemento e o
* próprio elemento.
*
***********************************************************************/
void LiberarElemento( LIS_tppLista pLista ,
tpElemLista * pElem )
{
#ifdef _DEBUG
CED_MarcarEspacoNaoAtivo(pElem);
#endif
if ( ( pLista->ExcluirValor != NULL )
&& ( pElem->pValor != NULL ))
{
pLista->ExcluirValor( pElem->pValor ) ;
} /* if */
free( pElem ) ;
pLista->numElem-- ;
} /* Fim função: LIS -Liberar elemento da lista */
/***********************************************************************
*
* $FC Função: LIS -Criar o elemento
*
***********************************************************************/
tpElemLista * CriarElemento( LIS_tppLista pLista ,
void * pValor ,
char * tipo)
{
tpElemLista * pElem ;
pElem = ( tpElemLista * ) malloc( sizeof( tpElemLista )) ;
if ( pElem == NULL )
{
return NULL ;
} /* if */
pElem->pValor = pValor ;
pElem->pAnt = NULL ;
pElem->pProx = NULL ;
#ifdef _DEBUG
CED_MarcarEspacoAtivo(pElem);
#endif
strcpy(pElem->tipo, tipo);
pLista->numElem ++ ;
return pElem ;
} /* Fim função: LIS -Criar o elemento */
/***********************************************************************
*
* $FC Função: LIS -Limpar a cabeça da lista
*
***********************************************************************/
void LimparCabeca( LIS_tppLista pLista )
{
pLista->pOrigemLista = NULL ;
pLista->pFimLista = NULL ;
pLista->pElemCorr = NULL ;
pLista->numElem = 0 ;
} /* Fim função: LIS -Limpar a cabeça da lista */
/********** Fim do módulo de implementação: LIS Lista duplamente encadeada **********/