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

Ds Lab Assignment 8

The document discusses implementing a stack data structure using an array and includes code to perform push and pop operations on the stack while handling overflow and underflow conditions. It also includes code for a menu driven program to demonstrate pushing and popping values from the stack as well as displaying the stack contents.

Uploaded by

S.ganga priya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

Ds Lab Assignment 8

The document discusses implementing a stack data structure using an array and includes code to perform push and pop operations on the stack while handling overflow and underflow conditions. It also includes code for a menu driven program to demonstrate pushing and popping values from the stack as well as displaying the stack contents.

Uploaded by

S.ganga priya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Q1.

Implement stack data structure using an array and implement


following operations:
(a) pop operation
(b) push operation
// Further, consider underflow and overflow conditions carefully
#include <stdio.h>

int stack[100],i,j,choice=0,n,top=-1;

void push();

void pop();

void show();

void main ()

printf("Enter the number of elements in the stack ");

scanf("%d",&n);

while(choice != 4)

printf("Chose one from the below options...\n");

printf("\n1.Push\n2.Pop\n3,show\n4.Exit");

printf("\n Enter your choice \n");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:
{

pop();

break;

case 3:

show();

break;

case 4:

printf("Exiting....");

break;

default:

printf("Please Enter valid choice ");

};

void push ()

int val;

if (top == n )

printf("\n Overflow");

else

printf("Enter the value?");

scanf("%d",&val);
top = top +1;

stack[top] = val;

void pop ()

if(top == -1)

printf("Underflow");

else

top = top -1;

void show()

for (i=top;i>=0;i--)

printf("%d\n",stack[i]);

if(top == -1)

printf("Stack is empty");

OUTPUT: Enter the number of elements in the stack 5

Chose one from the below options...

1.Push
2.Pop

3,show

4.Exit

Enter your choice

Enter the value?3

Chose one from the below options...

1.Push

2.Pop

3,show

4.Exit

Enter your choice

Enter the value?4

Chose one from the below options...


1.Push

2.Pop

3,show

4.Exit

Enter your choice

Enter the value?3

Chose one from the below options...

1.Push

2.Pop

3,show

4.Exit

Enter your choice


2

Chose one from the below options...

1.Push

2.Pop

3,show

4.Exit

Enter your choice

43

Chose one from the below options...

1.Push

2.Pop

3,show
4.Exit

Enter your choice

Exiting....

Q2.
Write a program to evaluate a Postfix expression.
// mention all your assumptions and limitations of your program
#include <stdio.h>

#include <math.h>

#include <stdbool.h>

#include <stdlib.h>

# define MAXCOL 80

struct stack {

int top;

int items[MAXCOL];

};

bool empty(struct stack *ps){

if(ps->top == -1)

return true;

else

return false;

}
int pop(struct stack *ps){

if(empty(ps)){

printf("%s", "undeflow condition\n");

exit(1);

return(ps->items[ps->top--]);

void push(struct stack *ps , int x){

if(ps->top == MAXCOL-1){

printf("%s", "overflow condition\n");

exit(1);

ps->items[++ps->top] = x;

bool isDigit(char symb){

return (symb >= '0' && symb <= '9');

float oper(char symb, float op1, float op2){

switch(symb){

case '+' : return (op1 + op2);

case '-' : return (op1 - op2);

case '*' : return (op1 * op2);

case '/' : return (op1 / op2);

case '$' : return (pow(op1 , op2));

default : printf("%s", "Illegal Operation");

exit(1);

}
float eval(char expr[]){

char c;

int position;

float op1,op2, value;

struct stack opndstk;

opndstk.top= -1;

for(position =0; (c= expr[position])!='\0'; position++)

if(isDigit(c))

push(&opndstk, (float)(c-'0'));

else {

op2 = pop(&opndstk);

op1 = pop(&opndstk);

value = oper(c, op1, op2);

push(&opndstk, value);

return (pop(&opndstk));

int main()

printf("enter expression\n");

char expr[MAXCOL];

int position =0;

while((expr[position++] = getchar()) != '\n');

expr[--position] = '\0';
printf("%s %s\n", "the original postfix expression is:", expr);

printf("%s %f\n", "expression's value after evaluation:", eval(expr));

return 0;

OUTPUT: enter expression

623+-382/+*2$3+

the original postfix expression is: 623+-382/+*2$3+

expression's value after evaluation: 52.000000

Q3.
Write a program to convert an expression from infix to Postfix.
// mention all your assumptions and limitations of your program
#include <stdio.h>

#include <math.h>

#include <stdbool.h>

#include <stdlib.h>

# define MAXCOL 80

struct stack {

int top;

int items[MAXCOL];

};

bool empty(struct stack *ps){


if(ps->top == -1)

return true;

else

return false;

int pop(struct stack *ps){

if(empty(ps)){

printf("%s", "undeflow condition\n");

exit(1);

return(ps->items[ps->top--]);

void push(struct stack *ps , int x){

if(ps->top == MAXCOL-1){

printf("%s", "overflow condition\n");

exit(1);

ps->items[++ps->top] = x;

int stackTop(struct stack *ps){

int item = pop(ps);

push(ps, item);

return item;

bool isOperand(char ch){

return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'));
}

bool prcd(char op1, char op2){

switch(op1){

case '$':

case '/':

case '*': switch(op2){

case '/':

case '*':

case '+':

case '-':

return true;

case '$':

return false;

case '+':

case '-': switch(op2){

case '+':

case '-':

return true;

case '$':

case '/':

case '*':

return false;

}
float postfix(char infix[], char postr[]){

int position;

int outpos = 0;

char topsymb;

char symb;

struct stack opstk;

opstk.top = -1;

for(position = 0; (symb = infix[position])!= '\0'; position++)

if(isOperand(symb))

postr[outpos++] = symb;

else {

while(!empty(&opstk) && prcd(stackTop(&opstk), symb)){

topsymb = pop(&opstk);

postr[outpos++] = topsymb;

push(&opstk, symb);

while(!empty(&opstk))

postr[outpos++] = pop(&opstk);

postr[outpos] ='\0';

int main()

printf("Enter expression\n");

char infix[MAXCOL];

char postr[MAXCOL];

int pos = 0;
while((infix[pos++] = getchar()) != '\n');

infix[--pos] = '\0';

printf("%s %s\n", "the original infix expression is:", infix);

postfix(infix, postr);

printf("%s %s\n", "expression after converting into postfix is:", postr);

return 0;

OUTPUT: Enter expression

A+B*C-D

the original infix expression is: A+B*C-D

expression after converting into postfix is: ABC*+D-

You might also like