Buscar

TpilhaOK

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

#include<stdio.h>
#include<stdlib.h>
struct Tpilha
{
	int ele;
	struct Tpilha *down;
};
void Insert(int e, struct Tpilha **top)
{
	
	struct Tpilha *p, *aux;
	
	p=new(struct Tpilha);
	p->ele=e;
	
	aux=*top;
	
	if(aux==NULL)		//caso lista vazia
	{
		p->down=NULL;	//primeiro elemento
		*top=p;
	}
	else				//percorre a lista
	{
		p->down=aux;	//liga no anterior
		*top=p;			//novo topo
	}
	
}
int Remove(struct Tpilha **top)
{
	
	int e;
	struct Tpilha *aux;
	
	aux=*top;
	
	if(aux==NULL)
	{
		printf("Pilha vazia.\n");
	}	
	else
	{
		e=aux->ele;				// e recebe o elemento pra retorno
		
		if(aux->down==NULL)		//caso seja o unico elemento
		{
			*top=NULL;
			delete(aux);
		}
		else					//caso tenha mais de 1 elemento
		{
			*top=aux->down;	
			delete(aux);		
		}
		printf("Elemento %d removido",e);
		return e;				//retorna elemento
	}	
}
void Print(struct Tpilha **top)
{
	
	struct Tpilha *aux;
	
	aux=*top;
	
	while(aux!=NULL)
	{
		printf("%d\n",aux->ele);
		aux=aux->down;
	}
	
}
main()
{
	
	struct Tpilha *first;
	first=NULL;
	
	int a,b;
	
	while(1)
	{
		system("cls");
		
		printf("1-Insert\n2-Remove\n3-Print\n0-Sair.\n");
		scanf("%d",&a);
		
		system("cls");
		
		if(a==1)
		{
			scanf("%d",&b);
			Insert(b,&first);
		}
		if(a==2)
		{
			b=Remove(&first);
			system("pause>>NULL");
		}
		if(a==3)
		{
			Print(&first);
			system("pause>>NULL");
		}
		
		if(a==0)
		break;
	}
	
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais