Q1. Implement Stack Using Array. Solution
Q1. Implement Stack Using Array. Solution
- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
case 2:
{
if(top==-1)
{
printf("\nStack Underflow");
}
else
{
printf("\nPopped item is: %d",stack[top]);
top=top-1;
}
}
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
break;
case 3:
if(top==-1)
printf("Stack is empty.Nothing to dislay");
else
{
temp=top;
while(temp>=0)
{
printf("\t %d",stack[temp]);
temp--;
}
}
break;
}//switch closes
}while(choice<4);
getch();
}
OUTPUT:
1.Push
2.Pop
3.Display
4.Exit
Enter your choice1
1.Push
2.Pop
3.Display
4.Exit
Enter your choice1
1.Push
2.Pop
3.Display
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
4.Exit
Enter your choice3
20 10
1.Push
2.Pop
3.Display
4.Exit
Enter your choice2
Stack Underflow
1.Push
2.Pop
3.Display
4.Exit
Enter your choice3
Stack is empty.Nothing to dislay
1.Push
2.Pop
3.Display
4.Exit
Enter your choice4
--------------------------------
Process exited after 23.2 seconds with return value 13
Press any key to continue . . .
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
{
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c ",pop());
}return 0;
}
OUTPUT:
abc/d*+
--------------------------------
Process exited after 9.591 seconds with return value 0
Press any key to continue . . .
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
{
char a[size],infix[size],postfix[size],item,temp;
int i=0,j=0;
printf("\nEnter Infix Notation::");
gets(infix);
strrev(infix);
while(infix[i]!='\0')
{
item=infix[i];
if (item==')')
{
push(item);
}
else if(item>='A'&&item<='Z'||item>='a'&&item<='z')
{
postfix[j]=item;
j++;
}
else if(is_operator(item)==1)
{
temp=pop();
while((is_operator(temp)==1)&&(precedence(temp)>=p
recedence(item)))
{
postfix[j]=temp;
j++;
temp=pop();
}//while
push(temp);
push(item);
}//else if
else if(item=='(')
{
temp=pop();
while(temp!=')')
{
postfix[j]=temp;
j++;
temp=pop();
}//while
}//else if
else
{
printf("\n Invalid arithmetic expression");
getch();
exit(0);
}//else
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
i++;
}//first while
while(top>-1)
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';
strrev(postfix);
printf("\n Arithmetic expression In Prefix
Notation):\n");
puts(postfix);
getch();
return 0;
}//main.
Example:
Enter Infix Notation::a*b+c*d
Arithmetic expressions In Prefix Notation):+*ab*cd
--------------------------------
Process exited after 11.97 seconds with return value 0
Press any key to continue . . .
Enter Infix Notation::A*B+C*D
Arithmetic expression In Prefix Notation :)
+*AB*CD
--------------------------------
Process exited after 61.54 seconds with return value 0
Press any key to continue . . .
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
OUTPUT:
--------------------------------
Process exited after 7.959 seconds with return value 0
Press any key to continue . . .
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
int s[50];
int top=0;
int main()
{
int a,b,c,i;
char prefix[50];
clrscr();
//for(i=0;i<strlen(prefix);i++)
for(i=strlen(prefix)-1;i>=0;i--)
{
if(prefix[i]=='+')
{
c=pop()+pop();
push(c);
}
else if(prefix[i]=='-')
{
a=pop();
b=pop();
c=b-a;
push(c);
}
else if(prefix[i]=='*')
{ a=pop();
b=pop();
c=b*a;
push(c);
}
Name-Alisha Anum Reg. no.- 21MCA0234
Subject-Problem solving with data structure and algorithms Subject code- ITA5002
else if(prefix[i]=='/')
{
a=pop();
b=pop();
c=b/a;
push(c);
}
else
{
push(prefix[i]-48);
//printf("\n INT=%d - CHAR=%d",prefix[i]-48,c);
}
}
printf("\nFinal ans = %d",pop());
getch();
return 0;
}
int pop()
{
int ch;
ch=s[top];
top=top-1;
return(ch);
}
OUTPUT:
Final ans = 30
--------------------------------
Process exited after 30.09 seconds with return value 0
Press any key to continue . . .