Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <stdlib.h>
- #define MAX_SIZE 256
- typedef struct node node;
- struct node {
- int item;
- struct node *next;
- };
- typedef struct stack stack;
- struct stack{
- int current_size;
- node *items[MAX_SIZE];
- };
- node* create_list(int item)
- {
- node *head = (node*) malloc(sizeof(node));
- head->item = item;
- head->next = NULL;
- return head;
- }
- node* add_end(node *head, int item)
- {
- node *new=(node*)malloc(sizeof(node));
- new->item=item;
- new->next=NULL;
- if(head == NULL)
- {
- return new;
- }
- else
- {
- node *aux=head;
- while(aux->next!=NULL)
- {
- aux=aux->next;
- }
- aux->next=new;
- }
- return head;
- }
- void print_list(node *a)
- {
- if(a!=NULL)
- {
- node* aux=a;
- while(aux!=NULL)
- {
- printf("%d ", aux->item);
- aux=aux->next;
- }
- }
- else
- {
- printf("lista vazia\n");
- }
- }
- stack* create_stack()
- {
- stack *new_stack = (stack*) malloc(sizeof(stack));
- new_stack->current_size=0;
- int i;
- for (i = 0; i < MAX_SIZE; i++)
- {
- new_stack->items[i] = NULL;
- }
- return new_stack;
- }
- int is_empty(stack *stack)
- {
- if(stack->current_size==0)
- return 1;
- else
- return 0;
- }
- void push(stack *stack, node* item)
- {
- if(stack->current_size == MAX_SIZE)
- {
- return;//printf("Stack Overflow\n");
- }
- else
- {
- stack->items[stack->current_size++]=item;
- }
- }
- node* pop(stack *stack)
- {
- if(!is_empty(stack))
- {
- node* aux=stack->items[--stack->current_size];
- stack->items[stack->current_size]=NULL;
- return aux;
- }
- }
- int main()
- {
- char c[10];
- int n;
- stack* pilha=create_stack();
- node* head=NULL;
- node* aux1= NULL;
- while(scanf("%s", c) != EOF)
- {
- if(strcmp(c,"PUSH") == 0)
- {
- char a;
- scanf("%d%c", &n, &a);
- head=add_end(head,n);
- push(pilha,head);
- while(a == ' ')
- {
- scanf("%d%c", &n, &a);
- head=add_end(head,n);
- push(pilha,head);
- }
- head = NULL;
- }
- else
- {
- aux1=pop(pilha);
- print_list(aux1);
- printf("\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement