Write a C code to implement the following operations using stack.
Assume the Stack S is empty initially.
Read the given text character by character.
If the character read is an operand push it onto the stack.
If the character read is a special character do the following
* means pop
$ means two pops
# means pop and a duplicate of popped value
Example:
DAT*ASTRU$$CTURES**#ANDAL$$GOR##ITHM**S
Stack:
S
T
I
G
A
U
T
C
A
A
D
Output: TURTSSERRLADNRROOMH
#include<stdio.h>
int top=-1;
char arr[15];
void pop();
void push(char x);
void display();
void push(char x)
{
top++;
arr[top]=x;
}
void pop(){
int x = top;
top--;
printf("Element removed from stack is: %c\n",arr[x]);
}
void display(){
int i;
for(i=0; i<top; i++){
printf("%c ",arr[i]);
}
}
int main(){
char string[50] = "DAT*ASTRU$$CTURES**#ANDAL$$GOR##ITHM**S";
int i;
for(i=0; i<50; i++){
char c = string[i];
if(c=='*' || c=='$' || c=='#'){
}
else{
push(c);
}
}
printf("Stack size:%d\n", top);
display();
}
Comments
Leave a comment