0% found this document useful (0 votes)
15 views5 pages

Lab Program 5 (1)

ds lab 5 program

Uploaded by

nehacg33
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)
15 views5 pages

Lab Program 5 (1)

ds lab 5 program

Uploaded by

nehacg33
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/ 5

5.

Design, Develop and Implement a Program in C for the following Stack Applications

a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^

b. Solving Tower of Hanoi problem with n disks.

a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^

// Evaluation of Suffix Expression

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<math.h>

#define MAX 50

int stack[MAX];

char post[MAX];

int top= -1;

/*fUNCTION PROTOYPE */

void pushstack(int tmp);

void calculator(char c);

void main()

int i;

printf("Insert a postfix notation :: ");

scanf("%s",post);

for(i=0;i<strlen(post);i++)

if(post[i]>='0' && post[i]<='9')

1
{

pushstack(i);

if(post[i]=='+' || post[i]=='-' || post[i]=='*' || post[i]=='/' || post[i]=='^')

calculator(post[i]);

printf("\n\nResult :: %d",stack[top]);

void pushstack(int tmp)

top++;

stack[top]=(int)(post[tmp]-48);

void calculator(char c)

int a,b,ans;

a=stack[top];

stack[top]='\0';

top--;

b=stack[top];

stack[top]='\0';

top--;

switch(c)

2
{

case '+':

ans=b+a;

break;

case '-':

ans=b-a;

break;

case '*':

ans=b*a;

break;

case '/':

ans=b/a;

break;

case '^':

ans=pow(b,a);

break;

default:

ans=0;

top++;

stack[top]=ans;

OUTPUT

3
b. Solving Tower of Hanoi problem with n disks.

#include <stdio.h>

void towers(int, char, char, char);

int main()

int num;

printf("Enter the number of disks : ");

scanf("%d", &num);

printf("The sequence of moves involved in the Tower of Hanoi are :\n");

towers(num, 'A', 'C', 'B');

return 0;

void towers(int num, char frompeg, char topeg, char auxpeg)

if (num == 1)

printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);

return;

towers(num - 1, frompeg, auxpeg, topeg);

printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);

towers(num - 1, auxpeg, topeg, frompeg);

4
OUTPUT

You might also like