Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct lista
- {
- int item;
- struct lista *next;
- };
- struct pilha
- {
- struct lista *array[50];
- };
- struct pilha *criar_pilhacomlista()
- {
- struct pilha *novapilha=malloc(sizeof(struct pilha));
- int i;
- for(i=0;i<50;i++)
- {
- novapilha->array[i]=NULL;
- }
- return novapilha;
- }
- struct lista* createdlinklist()
- {
- return NULL;
- }
- struct lista* add(int item)
- {
- struct lista *novoitem = malloc(sizeof(struct lista));
- novoitem->item = item;
- novoitem->next = NULL;
- //pilha->array[indice] = novoitem;
- return novoitem;
- }
- void add_elemento(struct pilha *pilha,int item,int indice)
- {
- struct lista *aux = createdlinklist();
- aux = add(item);
- aux->next = pilha->array[indice];
- pilha->array[indice] = aux;
- }
- int is_empty(struct pilha *pilha)
- {
- if(pilha->array[0] == NULL)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- void pop(struct pilha *pilha,int maior) // remove o topo
- {
- if(is_empty(pilha))
- {
- printf("STACK UNDERFLOW\n");
- return ;
- }
- else
- {
- struct lista *aux = pilha->array[maior];
- printfila(aux);
- printf("\n");
- pilha->array[maior]=NULL;
- }
- }
- void printfila(struct lista *head)
- {
- if(head!=NULL)
- {
- printfila(head->next);
- printf("%d ", head->item);
- }
- }
- int main()
- {
- struct pilha *pilha = criar_pilhacomlista();
- int number,indice=0;
- char comando[10],espaco;
- while(scanf("%s ", comando)!=EOF)
- {
- if(strcmp(comando,"PUSH")==0)
- {
- scanf("%d%c", &number, &espaco);
- while(espaco!='\n')
- {
- add_elemento(pilha,number,indice);
- scanf(" %d%c ", &number, &espaco);
- }
- indice++;
- }
- else if(strcmp(comando,"POP")==0)
- {
- pop(pilha,--indice);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement