Beruflich Dokumente
Kultur Dokumente
topo 10 20 3 5
#include<stdio.h>
#include<stdlib.h>
struct topo{
int m;
struct no *No;
};
struct no{
int d;
struct no *prox;
};
********************************************************************************
int mostrar(struct topo *p)
{
struct no *aux;
if(p->No==NULL)return(0);
aux=p->No;
do{
printf("%d\n",aux->d);
aux=aux->prox;
}while(aux!=NULL);
printf("\n");
}
********************************************************************************
int inserir(struct topo *p,int y)
{
struct no novono;
struct no *aux;
aux=(struct no *)malloc(sizeof(struct no));
aux->d=y;
aux->prox=p->No;
p->No=aux;
p->m++;
}
********************************************************************************
int remover(struct topo *p)
{
struct no *aux;
if(p->No==NULL) printf("\nLista Vazia ou Final da Lista\n");
else
{
aux=p->No;
p->No=aux->prox;
}
}
********************************************************************************
int main()
{
struct topo head;
struct topo *p;
p=(struct topo *)malloc(sizeof(struct topo));
p->m=0;
p->No=NULL;
inserir(p,10);
inserir(p,20);
inserir(p,30);
inserir(p,40);
inserir(p,50);
mostrar(p);
remover(p);
mostrar(p);
remover(p);
mostrar(p);
remover(p);
mostrar(p);
printf("\nNos Criados: %d\n\n",p->m);
system("pause");
}
2 – Considere a seguinte estrutura. Supondo-se uma fila ligada, construa as operações para inserir e
remover.
saida
entrada 10 30 50 5
#include<stdio.h>
#include<stdlib.h>
struct cabeca{
int contano;
struct no *entrada;
struct no *saida;
};
struct no{
int d;
struct no *proximo;
};
********************************************************************************
int remover(struct cabeca *c)
{
struct no *n,*aux;
n=c->entrada;
while(n->proximo!=NULL)
{
aux=n;
n=n->proximo;
}
aux->proximo=NULL;
c->saida=aux;
}
********************************************************************************
int mostrar(struct cabeca *c)
{
struct no *n;
if(c->entrada==NULL) return(0);
n=c->entrada;
do{
printf(" %d",n->d);
n=n->proximo;
}while(n!=NULL);
}
********************************************************************************
int inserir(struct cabeca *c,int y)
{
struct no *n;
n=(struct no *)malloc(sizeof(struct no));
c->contano++;
n->d=y;
n->proximo=c->entrada;
if(c->entrada==NULL) c->saida=n;
c->entrada=n;
}
**************** ***************************************************************
int main()
{
struct cabeca *c;
// gerando a cabeça
c=(struct cabeca *)malloc(sizeof(struct cabeca));
c->contano=0;
c->entrada=NULL;
c->saida=NULL;
printf("\nInseri (10):");
inserir(c,10);
mostrar(c);
printf("\nInseri (20):");
inserir(c,20);
mostrar(c);
printf("\nInseri (30):");
inserir(c,30);
mostrar(c);
printf("\nInseri (40):");
inserir(c,40);
mostrar(c);
printf("\n\nRemove o Ultimo da Fila:");
remover(c);
mostrar(c);
printf("\nRemove o Ultimo da Fila:");
remover(c);
mostrar(c);
printf("\nRemove o Ultimo da Fila:");
remover(c);
mostrar(c);
printf("\n\n");
system("pause");
}
Universidade Guarulhos
Estrutura de Dados
Exercícios
Grupo: _____________________________RA_____________
_____________________________RA_____________
_____________________________RA_____________
_____________________________RA_____________
_____________________________RA_____________
Guarulhos (2008)