0% found this document useful (0 votes)
10 views13 pages

Stacks 1

Uploaded by

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

Stacks 1

Uploaded by

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

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;
}

printf("hexadecimal number is");


while (!isempty())
{
y = pop();
if (y <= 9)
{
printf("%d", y);
}
else
{
printf("%c", 'A' + y - 10);
}
}

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;
}

printf("the given conversion is");


while (!isempty())
{
y= pop();
printf("%d",y);
}

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);

8. Program for Infix to Postfix Coversion


#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define STACKSIZE 100

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);
}

10. Program to check the validity of Parenthesized


Arithmetic Expression using Stack
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int top = -1;
char stack[100];

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;
}

12. Program to check if the given number is a palindrome using stacks


#include <stdio.h>
#include <string.h>

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);
}

13. Program to Reverse the given String using Stack


#include <stdio.h>
#include <string.h>

#define max 100


int top,stack[max];

void push(char x){

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();
}

You might also like