Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <string.h>
- /* Struktur Node */
- typedef struct stackNode_t {
- char data;
- struct stackNode_t *next;
- } StackNode;
- /* Struktur ADT Stack */
- typedef struct stack_t {
- StackNode *_top;
- unsigned _size;
- } Stack;
- /* Function prototype */
- void stack_init(Stack *stack);
- bool stack_isEmpty(Stack *stack);
- void stack_push(Stack *stack, char value);
- void stack_pop(Stack *stack);
- char stack_top(Stack *stack);
- unsigned stack_size(Stack *stack);
- /* Function definition below */
- void stack_init(Stack *stack)
- {
- stack->_size = 0;
- stack->_top = NULL;
- }
- bool stack_isEmpty(Stack *stack) {
- return (stack->_top == NULL);
- }
- void stack_push(Stack *stack, char value)
- {
- StackNode *newNode = (StackNode*) malloc(sizeof(StackNode));
- if (newNode) {
- stack->_size++;
- if (stack_isEmpty(stack)) newNode->next = NULL;
- else newNode->next = stack->_top;
- newNode->data = value;
- stack->_top = newNode;
- }
- }
- void stack_pop(Stack *stack)
- {
- if (!stack_isEmpty(stack)) {
- StackNode *temp = stack->_top;
- stack->_top = stack->_top->next;
- free(temp);
- stack->_size--;
- }
- }
- char stack_top(Stack *stack)
- {
- if (!stack_isEmpty(stack))
- return stack->_top->data;
- }
- unsigned stack_size(Stack *stack) {
- return stack->_size;
- }
- Stack reverseStack(Stack *stack){
- Stack newStack;
- StackNode *prevTemp0;
- StackNode *temp,*tempHead;
- tempHead=stack->_top;
- stack_init(&newStack);
- prevTemp0=NULL;
- while(prevTemp0!=tempHead){
- temp=tempHead;
- while(temp->next!=NULL){
- prevTemp0=temp;
- temp=temp->next;
- }
- if(stack_isEmpty(&newStack)) newStack._top=temp;
- temp->next=prevTemp0;
- prevTemp0->next=NULL;
- }
- return newStack;
- }
- void printStack(Stack *stack){
- StackNode *node;
- node=stack->_top;
- while(node!=NULL) printf("%c ",node->data);
- }
- int checkPalindrom(Stack *stack,char string[]){
- int j=0;;
- while(j!=strlen(string)/2){
- if(stack_top(stack) == string[j++]){
- stack_pop(stack);
- }
- else return 0;
- }
- return 1;
- }
- int main(int argc, char const *argv[])
- {
- // Buat objek Stack
- Stack myStack,secondStack;
- stack_init(&myStack);
- char cha[100];
- gets(cha);
- int i;
- for(i=0;i<strlen(cha);i++){
- stack_push(&myStack,cha[i]);
- }
- // while(!stack_isEmpty(&myStack)) {
- // printf("%c ", stack_top(&myStack));
- // stack_pop(&myStack);
- // }
- printf("Palindrom? %d\n",checkPalindrom(&myStack,cha));
- puts("");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement