Stack Data Structure C Source Code Version 1.0


  1. //      inpost.c
  2. //      
  3. //      Copyright 2010 Hafizh Herdi Naufal <twohnuvo@gmail.com>
  4. //      
  5. //      This program is free software; you can redistribute it and/or modify
  6. //      it under the terms of the GNU General Public License as published by
  7. //      the Free Software Foundation; either version 2 of the License, or
  8. //      (at your option) any later version.
  9. //      
  10. //      This program is distributed in the hope that it will be useful,
  11. //      but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //      GNU General Public License for more details.
  14. //      
  15. //      You should have received a copy of the GNU General Public License
  16. //      along with this program; if not, write to the Free Software
  17. //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. //      MA 02110-1301, USA.
  19.  
  20.  
  21. #include "inpost.h"
  22. #include "stdlib.h"
  23.  
  24. adrStack alokasiStack(infotype x)
  25. {
  26.         adrStack p = (adrStack) malloc(sizeof(ElmtStack));
  27.         p->info = x;
  28.         p->next = nil;
  29.         return p;  
  30. }
  31.  
  32. infotype inputS()
  33. {
  34.         infotype x;
  35.         printf("\nMasukkan infotype : ");
  36.         scanf("%d", &x);
  37.         return x;
  38. }
  39.  
  40. boolean isEmpty(Stack *S)
  41. {
  42.         return(S->top == nil);
  43. }
  44.  
  45. void printStack(Stack S)
  46. {
  47.     adrStack p;
  48.     for(p = S.top; p != nil; p = p->next )
  49.     {
  50.         printf("%d \n", p->info);      
  51.     }
  52. }
  53. void createStack(Stack *S)
  54. {
  55.         (*S).top = nil;
  56. }
  57.  
  58. void push(Stack *S, infotype x)
  59. {
  60.         adrStack p;
  61.         p = alokasiStack(x);
  62.         p->next = S->top;
  63.         S->top = p;
  64. }
  65.  
  66. void pop(Stack *S, adrStack *p)
  67. {  
  68.         *p = S->top;
  69.         S->top = S->top->next;
  70. }
  71.  
  72. void popWithNotice(Stack *S, adrStack *p)
  73. {  
  74.         *p = S->top;
  75.         S->top = S->top->next;
  76.         printf("Elemen %d diambil\n", (*p)->info);
  77. }
  78.  
  79. Stack createExist(Stack *T)
  80. {
  81.     Stack S;
  82.     createStack(&S);
  83.     adrStack p,q;
  84.     int i=0;
  85.    
  86.     p = T->top;
  87.     while(p!=nil)
  88.     {
  89.         pop(T, &q);
  90.         push(&S, q->info);
  91.         i+=1;
  92.         p=p->next;
  93.     }
  94.     //validasi doang cuy
  95.     if (p == nil)
  96.     {
  97.         printf("Berhasil memindahkan %d elemen \n",i);
  98.     }
  99.     return S;
  100. }
  101.  
  102. void searchElmt(Stack *S, infotype x)
  103. {
  104.     int i=0;
  105.     adrStack q, p;
  106.     boolean temu = false;
  107.     Stack T;
  108.     createStack(&T);
  109.     p = S->top;
  110.     while(p!=nil)
  111.     {
  112.         i+=1;
  113.         pop(S, &q);
  114.         push(&T, q->info);
  115.         if (q->info == x)
  116.         {
  117.             printf("berhasil menemukan %d pada tingkat ke %d \n",q->info,i);
  118.             temu = true;
  119.         }
  120.         p = p->next;
  121.     }
  122.     if (p==nil && !temu)
  123.     {
  124.         printf("\nData tidak ditemukan");
  125.     }
  126.     *S = createExist(&T);
  127. }
  128.  
  129. Stack reverseStack(Stack *S)
  130. {
  131.     Stack T;
  132.     adrStack q,p = S->top;
  133.     int i=0;
  134.     createStack(&T);
  135.     while(p!=nil)
  136.     {
  137.         pop(S, &q);
  138.         push(&T, q->info);
  139.         i+=1;
  140.         p=p->next;
  141.     }
  142.     return T;
  143. }
  144.  

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s