Advertisement
xNot_Found

[C] Pembalik Kata STACK

Oct 15th, 2016
25,749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "string.h"
  3. #include "string"
  4. #include "iostream"
  5. #define maxstack 50
  6. using namespace std;
  7.  
  8. typedef char ItemType;
  9. string input = "";
  10. typedef struct
  11. {
  12.     ItemType Item[maxstack];
  13.     int Count;
  14. }Stack;
  15.  
  16. void InitializedStack(Stack *S)
  17. {
  18.     S->Count = 0;
  19. }
  20.  
  21. int Empty(Stack *S){
  22.     return(S->Count == 0);
  23. }
  24.  
  25. int Full(Stack *S){
  26.     return(S->Count == input.length());
  27. }
  28.  
  29. void Push(ItemType x, Stack *S)
  30. {
  31.     if (Full(S))
  32.         printf("Stack penuh! Data tidak dapat masuk");
  33.     else
  34.     {
  35.         S->Item[S->Count] = x;
  36.         ++S->Count;
  37.     }
  38. }
  39.  
  40. char Pop(Stack *S)
  41. {
  42.     if (Empty(S))
  43.         printf("");
  44.         //printf("Stack masih kosong");
  45.     else
  46.     {
  47.         --(S->Count);
  48.         return (S->Item[S->Count]);
  49.     }
  50. }
  51.  
  52. void main()
  53. {
  54.     int i;
  55.     Stack tumpukan;
  56.     InitializedStack(&tumpukan);
  57.  
  58.     //for (i = 0; i < maxstack; i++){
  59.     printf("\t\t\t-----------------------\n");
  60.     printf("\t\t\t| PEMBALIK KATA STACK |\n");
  61.     printf("\t\t\t-----------------------\n");
  62.     printf("\nMasukkan kata = ");
  63.    
  64.     getline(cin, input);
  65.    
  66.     for (i = 0;i<input.length(); i++){
  67.             Push(input[i], &tumpukan);
  68.    
  69.     for (i = input.length(); i > 0; i--){
  70.         printf("%c", Pop(&tumpukan));
  71.     }
  72.          getch(); //http://nhnotes.blogspot.com
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement