0% found this document useful (0 votes)
107 views

Program To Convert Decimal To Binary Using Stack

The document contains several C programs that use stacks and other data structures to solve problems related to prime numbers, binary conversion, palindrome checking, parenthesis balancing, postfix expression evaluation, infix to postfix conversion, and sparse matrix representation and transposition. Each program includes functions to implement a stack with push and pop operations and applies the stack to solve the given problem, outputting the results.

Uploaded by

Vivek Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

Program To Convert Decimal To Binary Using Stack

The document contains several C programs that use stacks and other data structures to solve problems related to prime numbers, binary conversion, palindrome checking, parenthesis balancing, postfix expression evaluation, infix to postfix conversion, and sparse matrix representation and transposition. Each program includes functions to implement a stack with push and pop operations and applies the stack to solve the given problem, outputting the results.

Uploaded by

Vivek Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

PROGRAM TO PRINT PRIME NUMBERS BY SEIVE OF ERATOSTHENES METHOD

#include <stdio.h>
void seiveoferatosthenes(int n)
{
int i,p;
int prime[1000]={0};
for(i=2;i*i<=n;i++)
{
if(prime[i]==0)
{
for(p=i*2;p<=n;p+=i)
{
prime[p]=1;
}
}
}
for(i=2;i<=n;i++)
{
if(prime[i]==0)
printf("%d ",i);
}

}
int main()
{
int n;
printf("enter n\n");
scanf("%d",&n);
printf("the prime no. are \n");
seiveoferatosthenes(n);
return 0;
}

PROGRAM TO CONVERT DECIMAL TO BINARY USING STACK


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 40
int stack[MAX];
int top=-1;
int isfull()
{
return top==MAX-1;
}
int isempty()
{
return top==-1;
}
void push(int c)
{
if(isfull())
{
printf("\nSTACK OVERFLOW\n");
return;
}
stack[++top]=c;
}
int pop()
{
int item;
if(isempty())
{
printf("\nSTACK UNDERFLOW");
exit(1);
}
item=stack[top];
top--;
return item;
}
int main()
{
int n,i;
printf("enter the no.\n");
scanf("%d",&n);
if(n==0)
printf("0");

while(n!=0)
{
push(n%2);
n=n/2;
}

while(!isempty())
{
printf("%d",pop());
}
return 0;
}
PROGRAM TO CHECK WHETHER A GIVEN STRING IS A PALLINDROME OR NOT
THROUGH STACK

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 40
char stack[MAX];
int top=-1;
int isfull()
{
return top==MAX-1;
}
int isempty()
{
return top==-1;
}
void push(char c)
{
if(isfull())
{
printf("\nSTACK OVERFLOW\n");
return;
}
stack[++top]=c;
}
char pop()
{
char item;
if(isempty())
{
printf("\nSTACK UNDERFLOW");
exit(1);
}
item=stack[top];
top--;
return item;
}
int main()
{
char string[MAX],p;
int i=0;
printf("enter string\n");
scanf("%s",string);

while(string[i]!='\0')
{
push(string[i]);
i++;
}
i=0;
while(!isempty())
{
p=pop();
if(p!=string[i])
break;
i++;

}
if(isempty())
printf("palindrome\n");
else
printf("not a pallindrome\n");

CHECK FOR BALANCED PARENTHESIS BY STACK

#include<stdio.h>
#define MAX 50
int stack[MAX];
int top=-1;

int isfull()
{
return (top==MAX-1);
}
int isempty()
{
return top==-1;
}
void push(char op)
{
if(isfull())
{
printf("stack overflow\n");
exit(1);
}
stack[++top]=op;
}
char pop()
{ if(isempty())
{
return '$';
}
char c = stack[top];
top--;
return c;
}

int isbalanced(char *exp)


{
int i=0;
char x;
while(exp[i]!='\0')
{
if(exp[i]=='('||exp[i]=='{'||exp[i]=='[')
{
push(exp[i]);

}
if(exp[i]=='}'||exp[i]==']'||exp[i]==')')
{
if(isempty())
return 0;

switch(exp[i])
{
case ')':
x=pop();
if(x=='{'||x=='[')
return 0;
break;
case '}':
x=pop();
if(x=='['||x=='(')
return 0;
break;
case ']':
x=pop();
if(x=='('||x=='{')
return 0;
break;
}
}
i++;
}
return isempty();
}

int main()
{
char exp[MAX];
scanf("%s",exp);
if(isbalanced(exp))
printf("balanced");
else
printf("unbalanced");
return 0;
}
PROGRAM TO EVALUATE POSTFIX EXPRESSION USING STACK

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 40
int stack[MAX];
char postfix[MAX];
int top=-1;
int isfull()
{
return top==MAX-1;
}
int isempty()
{
return top==-1;
}
void push(int c)
{
if(isfull())
{
printf("\nSTACK OVERFLOW\n");
return;
}
stack[++top]=c;
}
int pop()
{
int item;
if(isempty())
{
printf("\nSTACK UNDERFLOW");
exit(1);
}
item=stack[top];
top--;
return item;
}
void postfixeval()
{
int i;char sym;
int y,x;
for(i=0;i<strlen(postfix);i++)
{
sym=postfix[i];
if(sym>='0'&&sym<='9')
{
push(sym-'0');
}
else
{
y=pop();
x=pop();
switch(sym)
{
case '+':
push(y+x);
break;
case '*':
push(x*y);
break;
case '/':
push(x/y);
break;
case '-':
push(x-y);
break;
case '%':
push(x%y);
break;
case '^':
push(pow(x,y));
break;
}
}
}
printf("\n%d",pop());
}
int main()
{
scanf("%s",postfix);
postfixeval();
return 0;
}
PROGRAM TO CONVERT AN INFIX EXPRESSION TO POSTFIX EXPRESSION
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 40
int stack[MAX];
char infix[MAX],postfix[MAX];
int top=-1;
int isfull()
{
return top==MAX-1;
}
int isempty()
{
return top==-1;
}
void push(char c)
{
if(isfull())
{
printf("\nSTACK OVERFLOW\n");
return;
}
stack[++top]=c;
}
char pop()
{
char item;
if(isempty())
{
printf("\nSTACK UNDERFLOW");
exit(1);
}
item=stack[top];
top--;
return item;
}
int priority(char c)
{
switch(c)
{
case '^':
return 3;
break;
case '*':
case '%':
case '/':
return 2;
break;
case '+':
case '-':
return 1;
break;
default:
return 0;
}
}
int rightass(char c)
{
if(c=='^')
return 1;
return 0;
}
void infixtopostfix()
{ int p=0,i;
char sym,temp;
for(i=0;i<strlen(infix);i++)
{
sym=infix[i];
switch(sym)
{
case '(':
push(sym);
break;
case ')':
while(stack[top]!='(')
{
postfix[p++]=pop();
}
temp=pop();
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
if(priority(stack[top])==priority(sym))
{
if(!rightass(sym))
postfix[p++]=pop();
}
while(!isempty()&& (priority(stack[top])>priority(sym)))
{
if(priority(stack[top])==priority(sym))
{
if(!rightass(sym))
postfix[p++]=pop();

}
else
{
postfix[p++]=pop();
}
}
push(sym);
break;
default:
postfix[p++]=sym;

}
}
while(!isempty())
postfix[p++]=pop();
postfix[p]='\0';
}
int main()
{
scanf("%s",infix);
infixtopostfix();
printf("\n%s",postfix);
}
PROGRAM TO CONVERT AN INFIX EXPRESSION TO PREFIX EXPRESSION.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 40
int stack[MAX];
char infix[MAX],postfix[MAX];
int top=-1;
int isfull()
{
return top==MAX-1;
}
int isempty()
{
return top==-1;
}
void push(char c)
{
if(isfull())
{
printf("\nSTACK OVERFLOW\n");
return;
}
stack[++top]=c;
}
char pop()
{
char item;
if(isempty())
{
printf("\nSTACK UNDERFLOW");
exit(1);
}
item=stack[top];
top--;
return item;
}
int priority(char c)
{
switch(c)
{
case '^':
return 3;
break;
case '*':
case '%':
case '/':
return 2;
break;
case '+':
case '-':
return 1;
break;
default:
return 0;
}
}
int rightass(char c)
{
if(c=='^')
return 1;
return 0;
}
void reverseexpr()
{
int i=0,j=strlen(infix)-1;
char temp;
while(i<j)
{

temp=infix[i];
infix[i]=infix[j];
infix[j]=temp;
i++;
j--;

}
for(i=0;i<strlen(infix);i++)
{
if(infix[i]==')')
infix[i]='(';
else if(infix[i]=='(')
infix[i]=')';
}

}
void reversestr()
{
int i=0,j=strlen(postfix)-1;
char temp;
while(i<j)
{

temp=postfix[i];
postfix[i]=postfix[j];
postfix[j]=temp;
i++;
j--;

}
}
void infixtopostfix()
{ int p=0,i;
char sym,temp;
for(i=0;i<strlen(infix);i++)
{
sym=infix[i];
switch(sym)
{
case '(':
push(sym);
break;
case ')':
while(stack[top]!='(')
{
postfix[p++]=pop();
}
temp=pop();
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
if(priority(stack[top])==priority(sym))
{
if(!rightass(sym))
postfix[p++]=pop();

}
while(!isempty()&& (priority(stack[top])>priority(sym)))
{
if(priority(stack[top])==priority(sym))
{
if(!rightass(sym))
postfix[p++]=pop();

}
else
{
postfix[p++]=pop();
}
}
push(sym);
break;
default:
postfix[p++]=sym;

}
}
while(!isempty())
postfix[p++]=pop();
postfix[p]='\0';
}
int main()
{
scanf("%s",infix);
reverseexpr();
infixtopostfix();
reversestr();
printf("\n%s",postfix);
}
PROGRAM TO REPRESENT A SPARSE MATRIX
#include<stdio.h>
struct sparse
{
int row;
int col;
int value;
};

int main()
{ int i,j;
struct sparse s[10];
int mat[4][4]={ {1,0,0,2},
{0,0,19.9},
{0,0,0,3},
{5,0,0,9}

};
s[0].row=4;
s[0].col=4;
s[0].value=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(mat[i][j]>0)
{s[0].value++;
s[s[0].value].row=i;
s[s[0].value].col=j;
s[s[0].value].value=mat[i][j];
}
}
}
printf("row col value\n");
for(i=0;i<=s[0].value;i++)
{
printf( "%d %d %d\n",s[i].row,s[i].col,s[i].value);
}
return 0;
}
PROGRAM TO FIND THE TRANSPOSE OF A SPARSE MATRIX
#include<stdio.h>
struct sparse
{
int row;
int col;
int value;
};

int main()
{
struct sparse s[10],b[10];
int m,n,t,i;
int s1[10]={0},k[10]={0};
printf("enter no. of rows columns and non zero values\n");
scanf("%d%d%d",&n,&m,&t);
s[0].row=m;
s[0].col=n;
s[0].value=t;
printf("enter the row no. column no. and corresponding value\n");
for(i=1;i<=t;i++)
{
scanf("%d%d%d",&s[i].row,&s[i].col,&s[i].value);
}
b[0].row=n;
b[0].col=m;
b[0].value=t;

for(i=1;i<=t;i++)
{
k[s[i].col]++;
}
s1[0]=1;
for(i=1;i<n;i++)
{
s1[i]=s1[i-1]+k[i-1];
}
for(i=1;i<=t;i++)
{

b[s1[s[i].col]].row=s[i].col;
b[s1[s[i].col]].col=s[i].row;
b[s1[s[i].col]].value=s[i].value;
s1[s[i].col]++;
}
for(i=0;i<=t;i++)
{
printf(" %d %d %d\n",b[i].row,b[i].col,b[i].value);
}
return 0;

PROGRAM TO INSERT AND DELETE AN ELEMENT IN A CIRCULAR QUEUE

#include<stdio.h>

#define MAX 50
int cqueue[MAX];
int front=-1;
int rear=-1;
int isempty()
{
return front==-1;
}
int isfull()
{
return front==(rear+1)%MAX;
}
void enque(int data)
{
if(isfull())
{
printf("overflow\n");
return;
}
if(front==-1)
front=0;
rear=(rear+1)%MAX;
cqueue[rear]=data;
}
int dequeue()
{
int item;
if(isempty())
{
printf("empty queue\n");
return -1;
}
item=cqueue[front];
if(front==rear)
{
front=rear=-1;
}
else
{
front=(front+1)%MAX;
}
return item;
}

void display()
{ int i;
if(isempty())
{
printf("queue is empty\n");
return;
}
printf("elements in queue are:\n");
if(front<=rear)
{

for(i=front;i<=rear;i++)
printf("%d ",cqueue[i]);
}
else
{
for(i=front;i<MAX;i++)
printf("%d ",cqueue[i]);
for(i=0;i<=rear;i++)
printf("%d ",cqueue[i]);
}
}

int main()
{ int item;
enque(5);
enque(10);
enque(25);
enque(100);

display();
item=dequeue();
printf("\n%d dequeued\n",item);
display();
}
PROGRAM TO REVERSE THE QUEUE (USING STACK)

#include<stdio.h>

#define MAX 50
int cqueue[MAX];
int front=-1;
int rear=-1;
int isempty()
{
return front==-1;
}
int isfull()
{
return front==(rear+1)%MAX;
}
void enque(int data)
{
if(isfull())
{
printf("overflow\n");
return;
}
if(front==-1)
front=0;
rear=(rear+1)%MAX;
cqueue[rear]=data;
}
int dequeue()
{
int item;
if(isempty())
{
printf("empty queue\n");
return -1;
}
item=cqueue[front];
if(front==rear)
{
front=rear=-1;
}
else
{
front=(front+1)%MAX;
}
return item;
}

void display()
{ int i;
if(isempty())
{
printf("queue is empty\n");
return;
}
printf("elements in queue are:\n");
if(front<=rear)
{

for(i=front;i<=rear;i++)
printf("%d ",cqueue[i]);
}
else
{
for(i=front;i<MAX;i++)
printf("%d ",cqueue[i]);
for(i=0;i<=rear;i++)
printf("%d ",cqueue[i]);
}
}

int main()
{ int item;
enque(5);
enque(10);
enque(25);
enque(100);

display();
item=dequeue();
printf("\n%d dequeued\n",item);
display();
}

PROGRAM TO IMPLEMENT A DEQUE


#include<stdio.h>
#include<stdlib.h>

#define MAX 50
int deque[MAX];
int front=-1;
int rear=-1;
int isempty()
{
return front==-1;
}
int isfull()
{
return front==(rear+1)%MAX;
}
void insert_at_rear(int data)
{
if(isfull())
{
printf("overflow\n");
return;
}
if(front==-1)
front=0;
rear=(rear+1)%MAX;
deque[rear]=data;
}
void insert_at_front(int item)
{
if(isfull())
{
printf("overflow\n");
return;
}

if(front==-1)
{ front=0;
rear=0;}

else if(front==0)
front=MAX-1;

else
front=front-1;

deque[front]=item;

}
int delete_at_front()
{
int item;
if(isempty())
{
printf("empty queue\n");
exit(1);
}
item=deque[front];
if(front==rear)
{
front=rear=-1;
}
else
{
front=(front+1)%MAX;
}
return item;
}

int delete_at_rear()
{
int item;
if(isempty())
{
printf("empty queue\n");
exit(1);
}
item=deque[rear];
if(front==rear)
{
front=-1;
rear=-1;
}
else if(rear==0)
{
rear=MAX-1;
}
else
{
rear=rear-1;
}
return item;
}

void display()
{ int i;
if(isempty())
{
printf("queue is empty\n");
return;
}
printf("\nelements in queue are:\n");
if(front<=rear)
{

for(i=front;i<=rear;i++)
printf("%d ",deque[i]);
}
else
{
for(i=front;i<MAX;i++)
printf("%d ",deque[i]);
for(i=0;i<=rear;i++)
printf("%d ",deque[i]);
}
}

int main()
{ int item;
insert_at_rear(100);
insert_at_rear(30);
insert_at_rear(3);
insert_at_rear(10);
display();
insert_at_front(23);
display();
item=delete_at_rear();
printf("\n%d deleted from rear end\n",item);
display();
item=delete_at_front();
printf("\n%d deleted from front end\n",item);
display();

PROGRAM TO IMPLEMENT A QUEUE USING STACKS


#include<stdio.h>
struct stack
{
int top;
int size;
int *array;
};
struct stack* createstack(int size)
{
struct stack* s=(struct stack*)malloc(sizeof(struct stack));
s->top=-1;
s->size=size;
s->array=(int *)malloc(size*sizeof(int));
return s;
}

int isfull(struct stack *s)


{
return s->top==s->size-1;
}
int isempty(struct stack *s)
{
return s->top==-1;
}
void push(struct stack *s,int data)
{
if(isfull(s))
{
printf("Stack overflow\n");
return;
}
s->array[++s->top]=data;
}
int pop(struct stack *s)
{ int item;
if(isempty(s))
{
printf("Stack Underflow\n");
exit(1);
}

item=s->array[s->top];
s->top--;
return item;

}
void enque(struct stack *s1,struct stack *s2,int data)
{
if(isfull(s1))
{
printf("queue is full\n");
return;
}
push(s1,data);
}
int deque(struct stack *s1,struct stack *s2)
{ int item;
if(isempty(s1))
{
printf("queue is empty\n");
exit(1);
}
while(!isempty(s1))
{
push(s2,pop(s1));
}
item=pop(s2);
while(!isempty(s2))
{
push(s1,pop(s2));
}
return item;
}
void display(struct stack *s1,struct stack *s2)

{ int i;
if(isempty(s1))
{
printf("queue is empty\n");
return;
}
while(!isempty(s1))
{
push(s2,pop(s1));
}
printf("queue is:");
for(i=s2->top;i>=0;i--)
{
printf("%d ",s2->array[i]);
}
printf("\n");
while(!isempty(s2))
{
push(s1,pop(s2));
}
}
int main()
{ int item;
struct stack *s1,*s2;
s1=createstack(10);
s2=createstack(10);
enque(s1,s2,13);
enque(s1,s2,14);
enque(s1,s2,16);
enque(s1,s2,98);
enque(s1,s2,43);
display(s1,s2);
item=deque(s1,s2);
printf("%d dequed\n",item);
item=deque(s1,s2);
printf("%d dequed\n",item);
display(s1,s2);
return 0;
}

You might also like