Stacks 1
Stacks 1
Decimal to Binary
#include<stdio.h>
#define TRUE 1
#define FALSE 0
struct stack{
int item[10];
int top;
};
struct stack s;
void initialisestack(void){
s.top=-1;
}
int isempty(void){
if(s.top==-1){
return TRUE;
}
else{
return FALSE;
}
}
void push(int x){
if(s.top==9){
printf("stack overflow");
}
else {
s.top=s.top+1;
s.item[s.top]=x;
}
}
int stacktop(void){
int x;
x=s.item[s.top];
return x;
}
int pop(void){
int x;
if(s.top==-1){
printf("stack underflow");
}
else {
x=s.item[s.top];
s.top=s.top-1;
return x;
}
}
int main(){
int y,r,n;
initialisestack();
printf("enter any decimal number");
scanf("%d",&n);
while(n!=0){
r=n%2;
push(r);
n=n/2;
}
printf("binary number is");
while(!isempty()){
y=pop();
printf("%d",y);
}
return 0;
}
2.Dcimal to Octal
#include <stdio.h>
#define TRUE 1
#define FALSE 0
struct stack
{
int item[10];
int top;
};
struct stack s;
void initialisestack(void)
{
s.top = -1;
}
int isempty(void)
{
if (s.top == -1)
{
return TRUE;
}
else
{
return FALSE;
}
}
void push(int x)
{
if (s.top == 9)
{
printf("stack overflow");
}
else
{
s.top = s.top + 1;
s.item[s.top] = x;
}
}
int stacktop(void)
{
int x;
x = s.item[s.top];
return x;
}
int pop(void)
{
int x;
if (s.top == -1)
{
printf("stack underflow");
}
else
{
x = s.item[s.top];
s.top = s.top - 1;
return x;
}
}
int main()
{
int y, r, n;
initialisestack();
printf("enter any decimal number");
scanf("%d", &n);
while (n != 0)
{
r = n % 8;
push(r);
n = n / 8;
}
printf("octal number is");
while (!isempty())
{
y = pop();
printf("%d", y);
}
return 0;
}
3.Hexadecimal
#include<stdio.h>
#define TRUE 1
#define FALSE 0
struct stack{
int item[10];
int top;
};
struct stack s;
void initialisestack(void){
s.top=-1;
}
int isempty(void){
if(s.top==-1){
return TRUE;
}
else{
return FALSE;
}
}
void push(int x){
if(s.top==9){
printf("stack overflow");
}
else {
s.top=s.top+1;
s.item[s.top]=x;
}
}
int stacktop(void){
int x;
x=s.item[s.top];
return x;
}
int pop(void){
int x;
if(s.top==-1){
printf("stack underflow");
}
else {
x=s.item[s.top];
s.top=s.top-1;
return x;
}
}
int main()
{
int y, r, n;
initialisestack();
printf("enter any decimal number");
scanf("%d", &n);
while (n != 0)
{
r = n % 16;
push(r);
n = n / 16;
}
return 0;
}
4.Anybase
#include<stdio.h>
#define TRUE 1
#define FALSE 0
struct stack{
int item[10];
int top;
};
struct stack s;
void initialisestack(void){
s.top=-1;
}
int isempty(void){
if(s.top==-1){
return TRUE;
}
else{
return FALSE;
}
}
void push(int x){
if(s.top==9){
printf("stack overflow");
}
else {
s.top=s.top+1;
s.item[s.top]=x;
}
}
int stacktop(void){
int x;
x=s.item[s.top];
return x;
}
int pop(void){
int x;
if(s.top==-1){
printf("stack underflow");
}
else {
x=s.item[s.top];
s.top=s.top-1;
return x;
}
}
int main()
{
int y, r, n, d;
int
A[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}
;
initialisestack();
printf("enter any decimal number");
scanf("%d", &n);
printf("enter the base to convert");
scanf("%d", &d);
while (n != 0)
{
r = n % d;
push(r);
n = n / d;
}
return 0;
}
5. Program for Stack Primitive Operations
#include<stdio.h>
#define TRUE 1
#define FALSE 0
struct stack{
int item[10];
int top;
};
struct stack s;
void initialisestack(void){
s.top=-1;
}
int isempty(void){
if(s.top==-1){
return TRUE;
}
else{
return FALSE;
}
}
void push(int x){
if(s.top==9){
printf("stack overflow");
}
else {
s.top=s.top+1;
s.item[s.top]=x;
}
}
int stacktop(void){
int x;
x=s.item[s.top];
return x;
}
int pop(void){
int x;
if(s.top==-1){
printf("stack underflow");
}
else {
x=s.item[s.top];
s.top=s.top-1;
return x;
}
}
void main(){
int y;
initialisestack();
push(100);
push(200);
push(300);
push(400);
y=pop();
printf("popped value= %d",y);
struct stack
{
char item[STACKSIZE];
int Top;
};
struct stack S;
/********************/
void InitializeStack(void)
{
S.Top=-1;
}
/********************/
int IsEmpty(void)
{
if(S.Top==-1)
return TRUE;
else
return FALSE;
}
/********************/
void Push(char x)
{
if(S.Top==STACKSIZE-1)
{
printf("\nstack overflows");
exit(1);
}
S.Top=S.Top+1;
S.item[S.Top]=x;
}
/********************/
char Pop(void)
{
char x;
if(S.Top==-1)
{
printf("\nstack underflows");
exit(1);
}
x=S.item[S.Top];
S.Top=S.Top-1;
return x;
}
/********************/
char StackTop(void)
{
char x;
x=S.item[S.Top];
return x;
}
/********************/
int prcd(char a,char b)
{
if(a=='^'||a=='*'||a=='/'||a=='%')
{
if(b=='^')
return FALSE;
else
return TRUE;
}
else
{
if(a=='+'||a=='-')
{
if(b=='+'||b=='-')
return TRUE;
else
return FALSE;
}
}
}
/********************/
void main()
{
char postfix[20],infix[20];
int j=0,i=0;
char x,symbol;
InitializeStack();
scanf("%s",infix);
while(infix[i]!='\0')
{
symbol=infix[i];
if(symbol>='a'&&symbol<='z')
{
postfix[j]=symbol;
j++;
}
else
{
while(!IsEmpty()&&prcd(StackTop(),symbol))
{
x=Pop();
postfix[j]=x;
j++;
}
Push(symbol);
}
i++;
}
while(!IsEmpty())
{
x=Pop();
postfix[j]=x;
j++;
}
postfix[j]='\0';
printf("Postfix Expression is:=> %s",postfix);
}
void push(char a)
{
stack[top] = a;
top++;
}
void pop()
{
if (top == -1)
{
printf("expression is invalid\n");
exit(0);
}
else
{
top--;
}
}
int main()
{
int i;
char a[100];
printf("Check expression correctly parenthesized");
printf("\nEnter expression : ");
scanf("%s",a);
for (i = 0; a[i] != '\0';i++)
{
if (a[i] == '(')
{
push(a[i]);
}
else if (a[i] == ')')
{
pop();
}
}
if (top == -1)
printf("Expression is valid\n\n");
else
printf("Expression is invalid\n\n");
return 0;
}
char stack[100];
int top = -1;
void push(char c)
{
stack[++top] = c;
}
char pop()
{
return(stack[top--]);
}
void main()
{
char str[100];
int i, count = 0, len;
printf("Enter string to check it is palindrome or not : ");
scanf("%s", str);
len = strlen(str);
for (i = 0; i < len; i++)
{
push(str[i]);
}
for (i = 0; i < len; i++)
{
if (str[i] == pop())
count++;
}
if (count == len)
printf("%s is a Palindrome string\n", str);
else
printf("%s is not a palindrome string\n", str);
}
if(top == max-1){
printf("stack overflow");
} else {
stack[++top]=x;
}
void pop(){
printf("%c",stack[top--]);
}
Void main()
{
char str[10];
scanf("%s",&str);
int len = strlen(str);
int i;
for(i=0;i<len;i++)
push(str[i]);
for(i=0;i<len;i++)
pop();
}