Advertisement
NB52053

Maybe Final

Nov 14th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1.  
  2. // MAYBE FINAL
  3.  
  4.  
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. struct node
  10. {
  11.     int info;
  12.     struct node *ptr;
  13. }*top, *top1, *temp;
  14.  
  15.  
  16.  
  17. void push(int data);
  18. void pop();
  19.  
  20. int count = 0;
  21.  
  22. char A[100];
  23.  
  24.  
  25. int main () {
  26.  
  27.     int i ;
  28.     printf("Enter the string : ");
  29.     gets(A);
  30.  
  31.     for(i=0 ; A[i] != '\0' ; i++){
  32.  
  33.         if ( A[i] <= 'z' &&  A[i] >='a' ||  A[i] <= 'Z' &&  A[i] >='A'){
  34.             push (A[i]);
  35.         }
  36.        
  37.         else if (A[i] <= '9' &&  A[i] >='0') {
  38.             pop ();
  39.         }
  40.  
  41.     }
  42.    
  43. }
  44.  
  45.  
  46. void push(int data){
  47.     if (top == NULL)
  48.     {
  49.         top = (struct node *)malloc(1 * sizeof(struct node));
  50.         top->ptr = NULL;
  51.         top->info = data;
  52.     }
  53.     else
  54.     {
  55.         temp = (struct node *)malloc(1 * sizeof(struct node));
  56.         temp->ptr = top;
  57.         temp->info = data;
  58.         top = temp;
  59.     }
  60.     count++;
  61. }
  62.  
  63.  
  64.  
  65. void pop()
  66. {
  67.     top1 = top;
  68.  
  69.     if (top1 == NULL)
  70.     {
  71.         printf("\n Error : Trying to pop from empty stack");
  72.         return;
  73.     }
  74.     else{
  75.         top1 = top1->ptr;
  76.     printf("\n Popped value : %c\n", top->info);
  77.     free(top);
  78.     top = top1;
  79.     count--;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement