Advertisement
mbah_bejo

Mod1.1

Feb 27th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. /* Struktur Node */
  7.  
  8. typedef struct stackNode_t {
  9.     char data;
  10.     struct stackNode_t *next;
  11. } StackNode;
  12.  
  13. /* Struktur ADT Stack */
  14.  
  15. typedef struct stack_t {
  16.     StackNode *_top;
  17.     unsigned _size;
  18. } Stack;
  19.  
  20. /* Function prototype */
  21.  
  22. void stack_init(Stack *stack);
  23. bool stack_isEmpty(Stack *stack);
  24. void stack_push(Stack *stack, char value);
  25. void stack_pop(Stack *stack);
  26. char stack_top(Stack *stack);
  27. unsigned stack_size(Stack *stack);
  28.  
  29. /* Function definition below */
  30.  
  31. void stack_init(Stack *stack)
  32. {
  33.     stack->_size = 0;
  34.     stack->_top = NULL;
  35. }
  36.  
  37. bool stack_isEmpty(Stack *stack) {
  38.     return (stack->_top == NULL);
  39. }
  40.  
  41. void stack_push(Stack *stack, char value)
  42. {
  43.     StackNode *newNode = (StackNode*) malloc(sizeof(StackNode));
  44.     if (newNode) {
  45.         stack->_size++;
  46.         if (stack_isEmpty(stack)) newNode->next = NULL;
  47.         else newNode->next = stack->_top;
  48.  
  49.         newNode->data = value;
  50.         stack->_top = newNode;
  51.     }
  52. }
  53.  
  54. void stack_pop(Stack *stack)
  55. {
  56.     if (!stack_isEmpty(stack)) {
  57.         StackNode *temp = stack->_top;
  58.         stack->_top = stack->_top->next;
  59.         free(temp);
  60.         stack->_size--;
  61.     }
  62. }
  63.  
  64. char stack_top(Stack *stack)
  65. {
  66.     if (!stack_isEmpty(stack))
  67.         return stack->_top->data;
  68. }
  69.  
  70. unsigned stack_size(Stack *stack) {
  71.     return stack->_size;
  72. }
  73.  
  74. Stack reverseStack(Stack *stack){
  75.     Stack newStack;
  76.     StackNode *prevTemp0;
  77.     StackNode *temp,*tempHead;
  78.     tempHead=stack->_top;
  79.     stack_init(&newStack);
  80.     prevTemp0=NULL;
  81.     while(prevTemp0!=tempHead){
  82.         temp=tempHead;
  83.         while(temp->next!=NULL){
  84.             prevTemp0=temp;
  85.             temp=temp->next;
  86.         }
  87.         if(stack_isEmpty(&newStack)) newStack._top=temp;
  88.         temp->next=prevTemp0;
  89.         prevTemp0->next=NULL;
  90.     }
  91.     return newStack;
  92. }
  93.  
  94. void printStack(Stack *stack){
  95.     StackNode *node;
  96.     node=stack->_top;
  97.     while(node!=NULL) printf("%c ",node->data);
  98. }
  99.  
  100. int checkPalindrom(Stack *stack,char string[]){
  101.     int j=0;;
  102.     while(j!=strlen(string)/2){
  103.         if(stack_top(stack) == string[j++]){
  104.             stack_pop(stack);
  105.         }
  106.         else return 0;
  107.     }
  108.     return 1;
  109. }
  110.  
  111. int main(int argc, char const *argv[])
  112. {
  113.     // Buat objek Stack
  114.     Stack myStack,secondStack;
  115.    
  116.     stack_init(&myStack);
  117.    
  118.     char cha[100];
  119.     gets(cha);
  120.     int i;
  121.     for(i=0;i<strlen(cha);i++){
  122.         stack_push(&myStack,cha[i]);
  123.     }
  124.    
  125. //    while(!stack_isEmpty(&myStack)) {
  126. //        printf("%c ", stack_top(&myStack));
  127. //        stack_pop(&myStack);
  128. //    }
  129.    
  130.     printf("Palindrom? %d\n",checkPalindrom(&myStack,cha));
  131.    
  132.     puts("");
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement