Inclusão e exclusão em uma lista sequencial - modo fila
Autor: Luciana Sondermann | Publicado: 2011-09-01T11:10:00-03:00

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int inserir(int *fila, int elemento, int tam, int *ini,int *fim)
{
&nbsp;&nbsp; &nbsp;if(*ini==*fim &amp;&amp; *fim==-1)
&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;fila[++*fim]=elemento;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*ini=0;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return 1;
&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;if(*fim==(tam-1) &amp;&amp; *ini==0)
&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf("\nFila cheia!\n");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return 0;
&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;if(*fim==(tam-1))
&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int i;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*fim=-1;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(int k=*ini;k&lt;tam;k++)
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;fila[i++]=fila[k];
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;fila[k]=NULL;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*fim=*fim+1;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*ini=0;
&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;fila[++*fim]=elemento;
&nbsp;&nbsp; &nbsp;return 1;
}
&nbsp;&nbsp; 
int retirar(int *fila, int tam, int *ini,int *fim)
{
&nbsp;&nbsp; &nbsp;if(*fim==*ini &amp;&amp; *fim==-1)
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf("\nFila ja vazia!\n");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return 0;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;if(*fim==*ini)
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;fila[*ini]=NULL;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*ini=-1;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;*fim=-1;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return 1;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;fila[*ini]=NULL;
&nbsp;&nbsp; &nbsp;*ini=*ini+1;
&nbsp;&nbsp; &nbsp;return 1;
}
int main() {

&nbsp;&nbsp; &nbsp;char op;
&nbsp;&nbsp; &nbsp;int ok, tam, *fila, elemento,ini=-1,fim=-1,i;
&nbsp;&nbsp; &nbsp;printf("Informe o tamanho desejado para a fila: ");
&nbsp;&nbsp; &nbsp;scanf("%d",&amp;tam);
&nbsp;&nbsp; &nbsp;if((fila=(int*)malloc(tam*sizeof(int)))==NULL) exit(1);
&nbsp;&nbsp; &nbsp;do{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf("\n(i)nserir (r)etirar (s)air: ");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;getchar();
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;scanf("%c",&amp;op);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;switch (op){
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;case 'i':
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf("\nInforme o elemento a ser inserido: ");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;scanf("%d",&amp;elemento);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ok=inserir(fila,elemento,tam,&amp;ini,&amp;fim);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(ok)printf("\nElemento inserido com sucesso!\n");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else printf("\nNao foi possivel inserir o novo elemento\n");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;break;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;case 'r':
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ok=retirar(fila,tam,&amp;ini,&amp;fim);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(ok)printf("\nElemento retirado com sucesso!\n");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else printf("\nNao foi possivel retirar o elemento\n");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;break;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(i=0;i&lt;tam;i++) printf("%d\t",fila[i]);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf("\n");
&nbsp;&nbsp; &nbsp;}while(op!='s');
&nbsp;&nbsp; &nbsp;return 0;
}