Advertisement
Guest User

asd

a guest
Mar 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include "stive.h"
  5. int main(/*int argc,char **argv*/)
  6. {
  7.     /*FILE *f;
  8.     char *date;
  9.     date=calloc((strlen(argv[1])+1),sizeof(char));
  10.     strcpy(date,argv[1]);
  11.     f=fopen(date,"rt");*/
  12.     stack *s;
  13.     int n,i;
  14.     char c,d,cuv[20];
  15.     scanf("%d",&n);
  16.     s=create_stack(n);
  17.     //fscanf(f,"%s",cuv);
  18.     for(i=0;i<n;i++)
  19.     {
  20.         fflush(stdin);
  21.         scanf("%c",&c);
  22.         push(s,c);
  23.         //push(s,cuv[i]);
  24.         d=top(s);
  25.         printf("%c\n",d);
  26.     }
  27.     /*while(!isempty(s))
  28.     {
  29.         c=top(s);
  30.         printf("%c ",c);
  31.         pop(s);
  32.     }*/
  33.     //fclose(f);
  34.  
  35. }
  36.  
  37. #include<stdio.h>
  38. #include<string.h>
  39. #include<stdlib.h>
  40. typedef struct Elem
  41. {
  42.     int top;
  43.     int capacity;
  44.     char *array;
  45.  
  46. }stack;
  47. stack* create_stack(int n)
  48. {
  49.     stack *s=(stack*)malloc(sizeof(stack));
  50.     if(!s)
  51.     return NULL;
  52.     s->capacity=n;
  53.     s->top=0;
  54.     s->array=(char*)malloc(sizeof(char) * s->capacity);
  55.     if(!s->array)
  56.     return NULL;
  57.     return s;
  58. }
  59. int isfull(stack *s)
  60. {
  61.     //printf("stack full\n",s->top);
  62.     return (s->top==s->capacity);
  63. }
  64. int isempty(stack *s)
  65. {
  66.     return (s->top==0);
  67. }
  68. void increase_capacity(stack *s)
  69. {
  70.     s->capacity*=2;
  71.     s->array=(char*)realloc(s->array,s->capacity);
  72. }
  73. void push(stack *s,char c)
  74. {
  75.    if(isfull(s))
  76.    increase_capacity(s);
  77.    s->array[s->top++]=c;
  78.   // printf("%c ",s->array[s->top-1]);
  79. }
  80. void pop(stack *s)
  81. {
  82.     if(!isempty(s))
  83.     s->top--;
  84.     else
  85.     return;
  86.  
  87. }
  88. char top(stack *s)
  89. {
  90.     if(!isempty(s))
  91.     return s->array[s->top-1];
  92.     printf("empty\n");
  93.    // else return -1;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement