Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define new(x) (x*)malloc(sizeof(x))
- typedef struct node
- {
- char value;
- struct node* next;
- } Node;
- typedef struct stack{
- Node* head; // Posisi teratas dari Stack
- int size; // Banyak elemen di Stack
- } Stack;
- void push(Stack* st, char value)
- {
- Node* temp = new(Node);
- temp->value = value;
- temp->next = NULL;
- if(!st->size)
- { // Kasus apabila Stack kosong
- st->head = temp;
- }
- else
- { // Kasus Stack sudah terisi
- temp->next = st->head;
- st->head = temp;
- }
- st->size++; // Jangan lupa increment size
- }
- char pop(Stack* st)
- {
- if(st->size <= 0) return -1;
- Node* temp = st->head;
- int value = temp->value;
- st->head = temp->next;
- st->size--;
- free(temp);
- return value;
- }
- void printAll(Stack* lk)
- {
- Node* tmp=lk->head;
- while(tmp!=NULL)
- {
- printf("%c",tmp->value);
- tmp=tmp->next;
- }
- printf("\n");
- return;
- }
- int
- main()
- {
- Stack st,z;
- st.head=NULL;
- st.size=0;
- z.head=NULL;
- z.size=0;
- char f;
- // printf("%d-%d\t%d-%d\t%d-%d\n",'(',')','[',']','{','}');
- while( scanf("%c",&f)==1 )
- {
- if(f=='('|| f=='['|| f=='{')
- {
- push(&st,f);
- }
- else
- {
- if(f==')')
- {
- if(st.head==NULL)
- {
- printf(":(");
- return 0;
- }
- else if(st.head->value=='(') pop(&st);
- else
- {
- printf(":(");
- return 0;
- }
- }
- else if(f==']')
- {
- if(st.head==NULL)
- {
- printf(":(");
- return 0;
- }
- else if(st.head->value=='[') pop(&st);
- else
- {
- printf(":(");
- return 0;
- }
- }
- else if(f=='}')
- {
- if(st.head==NULL)
- {
- printf(":(");
- return 0;
- }
- else if(st.head->value=='{') pop(&st);
- else
- {
- printf(":(");
- return 0;
- }
- }
- }
- }
- if(st.head==NULL)
- {
- printf(":)");
- // printf("%d",st.size);
- return 0;
- }
- else
- {
- printf(":(");
- // printf("%d",st.size);
- return 0;
- }
- // printAll(&st);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement