Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include "stive.h"
- int main(/*int argc,char **argv*/)
- {
- /*FILE *f;
- char *date;
- date=calloc((strlen(argv[1])+1),sizeof(char));
- strcpy(date,argv[1]);
- f=fopen(date,"rt");*/
- stack *s;
- int n,i;
- char c,d,cuv[20];
- scanf("%d",&n);
- s=create_stack(n);
- //fscanf(f,"%s",cuv);
- for(i=0;i<n;i++)
- {
- fflush(stdin);
- scanf("%c",&c);
- push(s,c);
- //push(s,cuv[i]);
- d=top(s);
- printf("%c\n",d);
- }
- /*while(!isempty(s))
- {
- c=top(s);
- printf("%c ",c);
- pop(s);
- }*/
- //fclose(f);
- }
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- typedef struct Elem
- {
- int top;
- int capacity;
- char *array;
- }stack;
- stack* create_stack(int n)
- {
- stack *s=(stack*)malloc(sizeof(stack));
- if(!s)
- return NULL;
- s->capacity=n;
- s->top=0;
- s->array=(char*)malloc(sizeof(char) * s->capacity);
- if(!s->array)
- return NULL;
- return s;
- }
- int isfull(stack *s)
- {
- //printf("stack full\n",s->top);
- return (s->top==s->capacity);
- }
- int isempty(stack *s)
- {
- return (s->top==0);
- }
- void increase_capacity(stack *s)
- {
- s->capacity*=2;
- s->array=(char*)realloc(s->array,s->capacity);
- }
- void push(stack *s,char c)
- {
- if(isfull(s))
- increase_capacity(s);
- s->array[s->top++]=c;
- // printf("%c ",s->array[s->top-1]);
- }
- void pop(stack *s)
- {
- if(!isempty(s))
- s->top--;
- else
- return;
- }
- char top(stack *s)
- {
- if(!isempty(s))
- return s->array[s->top-1];
- printf("empty\n");
- // else return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement