Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- /* Stack has three properties. capacity stands for the maximum number of elements stack can hold.
- Size stands for the current size of the stack and elements is the array of elements */
- typedef struct Stack
- {
- int capacity;
- int size;
- int *elements;
- } Stack;
- /* crateStack function takes argument the maximum number of elements the stack can hold, creates
- a stack according to it and returns a pointer to the stack. */
- Stack * createStack(int maxElements)
- {
- /* Create a Stack */
- Stack *S;
- S = (Stack *)malloc(sizeof(Stack));
- /* Initialise its properties */
- S->elements = (int *)malloc(sizeof(int)*maxElements);
- S->size = 0;
- S->capacity = maxElements;
- /* Return the pointer */
- return S;
- }
- void pop(Stack *S)
- {
- S->size--;
- return;
- }
- int top(Stack *S)
- {
- return S->elements[S->size-1];
- }
- void push(Stack *S,int element)
- {
- /* Push an element on the top of it and increase its size by one*/
- S->elements[S->size++] = element;
- }
- main()
- {
- Stack *S = createStack(40);
- char input[40];
- int i,n;
- printf("Enter Input\n");
- gets(input);
- for(i=0; input[i]!='\0'; i++)
- {
- if(input[i]<'10'&&input[i]>='0')
- push(S,input[i]);
- if(input[i]=='+')
- {
- n=top(S)-'0';
- pop(S);
- n=n+top(S)-'0';
- pop(S);
- push(S,n);
- }
- if(input[i]=='-')
- {
- n=top(S);
- pop(S);
- n=n-top(S);
- pop(S);
- push(S,n);
- }
- if(input[i]=='*')
- {
- n=top(S)-'0';
- pop(S);
- n=n*(top(S)-'0');
- pop(S);
- push(S,n);
- }
- if(input[i]=='/')
- {
- n=top(S)-'0';
- pop(S);
- n=n/(top(S)-'0');
- pop(S);
- push(S,n);
- }
- }
- printf("answer = %d",top(S));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement