Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

SESSION – 1.

3
QUESTION 1:
Implement the concept of Stack Data Structure with the following operations.
1) Push()
2) Pop()
3) Peek()
4) isFull()
5) isEmpty()
6) Display()

QUESTION 2:
Write a program to implement Balancing of Parenthesis using Stack Data Structures.

QUESTION 3:
i) Write a function to read an expression and which is stored in X=A+B*C+(D*E+F)*G as
infix format.
ii) Write an algorithm to transform an infix expression X into its postfix equivalent and also write
a function to evaluate the postfix expression.
iii) Write a main method to demonstrate the above functions. Assume that the infix expression X
is terminated by a character #.

QUESTION 4:
Suppose that you are part of a team developing a financial application. One of the functions used in the

application is a calculator, for infix arithmetic expressions. For example, given the expression 5∗ (((9 +
8)∗(4∗ 6))+7), the function outputs the result 2075. You are supposed to design the data structures and
implementation for this calculator function. The input may be any arithmetic expression. Your function
should read the expression from standard input, perform the computation and output the result.
(HINT: Think about using 2 stacks, one for pushing the operands and the other for the operators.
Whenever an operator or ’)’ is encountered in the input, you could pop the top 2 operands from the stack,
evaluate their value and push the result back onto the stack )
For example, when given the expression (a+b), push ’(’ and ’a’ onto the operand stack, push ’+’ onto the
operator stack. Then push ’b’ onto the operand stack. When ’)’ is encountered, pop ’b’, ’a’ and ’(’ from
the operand stack, pop ’+’ from the operator stack, perform the addition and push the result back onto
the operand stack. When no further input is encountered, return the top of the operand stack as the
result.)

717821F263-VINUVARSITH M
QUESTION 5:
Steve has a string of lowercase characters in range ascii[‘a’..’z’]. He wants to reduce the string to its shortest
length by doing a series of operations. In each operation he selects a pair of adjacent lowercase letters that
match, and he deletes them. For instance, the string aab could be shortened to b in one operation. Steve’s
task is to delete as many characters as possible using this method and print the resulting string. If the final
string is empty, print "Empty String" without quotes. Characters can be deleted only if they form a pair and
are same (i.e from aaa we can only delete 2 a's and will be left with a single a).
INPUT:
A single string, s.
CONSTRAINTS:
1<=|s|<=1000
OUTPUT:
If the final string is empty, print Empty String; otherwise, print the final non-reducible string.
SAMPLE INPUT
aaabccddd
SAMPLE OUTPUT
Abd

QUESTION 6:
There are N frustrated coders standing in a circle with a gun in their hands. Each coder has a skill value S[ i
] and he can only kill those coders that have strictly less skill than him. One more thing, all the guns have
only 1 bullet. This roulette can take place in any random order. Fortunately, you have the time stone (haaan
wo harre wala) and you can see all possible outcomes of this scenario. Find the outcome where the total sum
of the remaining coder's skill is minimum. Print this sum.
INPUT:
The first line contains N the no. of coders. The next line contains N elements where the ith element is
the S[ i ] of ith coder.
OUTPUT
Print a single line containing the minimum sum
CONSTRAINTS
1<= N <= 1000000
1<=S[ i ]<=1000
SAMPLE INPUT
6
172244
SAMPLE OUTPUT:
11

717821F263-VINUVARSITH M
Exp. No : 1.3(a)
IMPLEMENT THE CONCEPT OF STACK
Date :

AIM:
To write a C-program to Implement the concept of Stack Data Structure with stack operations.
PSEUDOCODE:
BEGIN
DEFINE
Size=4
DECLARE int Top=-1, inp_array[Size] globally;
DECLARE the FUNCTIONS
void Push();
void Pop();
void show();
DECLARE choice using integer data
type WHILE(1)
{
PRINT Operations performed by Stack
; 1.Push the element
2.Pop the element
3.Show\n
4.End;
PRINT Enter the choice:;
GET the value of choice from the
user; USE SWITCH CASE
switch(choice)
{
case 1:

Push();
break;
case 2:

Pop();

717821F263-VINUVARSITH M
break;
case 3:
show();
break;
case 4:
exit(0);
default:
PRINT ("\n Invalid choice!!");
}
}
FUNCTION PUSH
void Push()
{
DECLARE x as integer data
type IF(Top==Size-1)
{
PRINT Overflow!! ;
}
ELSE
{
PRINT Enter element to be inserted to the
stack:; GET the value of x from user;
Top=Top+1;
Inp _ array [Top]=x;
}
}
FUNCTION POP
void Pop()
{
if(Top==-1)
{
PRINT Underflow!!;

717821F263-VINUVARSITH M
}
ELSE
{

PRINT the value of


inp_array[Top]); Top=Top-1;
}
}

FUNCTION SHOW
void show()
{
if(Top==-1)
{
PRINT Underflow!!;
}
ELSE
{
PRINT Elements present in the stack:;
USE FOR LOOP
for(int i=Top;i>=0;--i)
PRINT the value of inp_array[i];
}
}

SOURCE CODE:
#include<stdio.h>
#define size 10
int s[size],top=-1;
void push(int x)
{
if(top==size-1)
printf("Overflow");
else
{

717821F263-VINUVARSITH M
top++;
s[top]=x;
}
}
int pop()
{
int x;
if(top==-1)
printf("Underflow");
else
{

x=s[top];
top--;

return x;
}
}
int peak()
{
int x;
if(top==-1)
printf("Underflow");
else
{

x=s[top];
return x;
}
}

void display()
{
int i;
for(i=top;i>=0;i--)

717821F263-VINUVARSITH M
{
printf("%d ",s[i]);
}
}
int main()
{
int choice,n;
while(1)
{
printf("\n1.PUSH\n2.POP\n3,PEAK\n4.DISPLAY\n5.EXIT");
printf("\nEnter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:

printf("Enter the Number:");


scanf("%d",&n);
push(n);
break;
case 2:

printf("%d is poped
out",pop()); break;
case 3:

printf("%d is the peak


value",peak()); break;
case 4:

display();
break;
case 5:

printf("Thank you :)");


exit(0);
deafult:

717821F263-VINUVARSITH M
printf("Please Enter the Valid Choice");
}
}
return 0;
}

OUTPUT:

RESULT:
Thus the implementation of stack is executed and the output is obtained successfully.

717821F263-VINUVARSITH M
Exp. No : 1.3 (b) IMPLEMENT BALANCING OF PARENTHESIS USING STACK

Date :

AIM:
To write a C-program for balancing paranthesis .

PSEUDOCODE:
BEGIN
DECLARE expression[50] of char type array
DECLARE and INITIALIZE x=0, i=0 of integer data
type PRINT Enter an expression
GET string for expression from user
SCAN the expression until we reach the end of the expression using while loop
WHILE (expression[i]!= '\0')
{
CHECK the symbol '(' using
if IF(expression[i]=='(')
{
x++;
}
CHECK the symbol ')' using else if
ELSE IF(expression[i]==')')
{
x--;
IF(x<0)
break;
}
i++;
}
}
CHECK whether x is equal to 0 or
not. IF(x==0)

717821F263-VINUVARSITH M
{
PRINTExpression is balanced;
}
ELSE
{
PRINT Expression is unbalanced;
}
END

SOURCE CODE:
#include<stdio.h>
int main()
{
char expression [50];
int x=0, i=0;
printf ("\n Enter an
expression"); scanf ("%s",
expression);
while(expression[i]!= '\0')
{
if(expression[i]=='(')
{
x++;
}
else if(expression[i]==')')
{x
--;
if(x<0)
break;
}
i++;
}
if(x==0)
{

717821F263-VINUVARSITH M
printf("Expression is balanced");
}
else
{
printf("Expression is unbalanced");
}
return 0;
}

OUTPUT:

RESULT:
Thus the program for balancing parenthesis is executed and the output is obtained successfully

717821F263-VINUVARSITH M
Exp. No : 1.3 (c)
INFIX TO POSTFIX
Date :

AIM:
To write a C-program for infix to postfix

PSEUDOCODE:
BEGIN
DECLARE stack[100] of character data type and top = -1 of integer data type.
DECLARE exp[100] ,*e, x using character data type
PRINT Enter the expression
GET string for expression from
user INITIALIZE e = exp;
USE WHILE loop to scan the expression until reaching end of expression
while(*e != '\0')
{
IF(isalnum(*e))
PRINT *e;
ELSE IF(*e == '(')
push(*e);
ELSE IF(*e == ')')
{
WHILE((x = pop()) != '(')
PRINT the value of x;
}
ELSE
{
WHILE(priority(stack[top]) >= priority(*e))
{
PRINT pop();
push(*e);
}

717821F263-VINUVARSITH M
e++;
}
WHILE(top != -1)
{
PRINT pop();
}
FUNCTION PUSH
void push(char x)
{
stack[++top] = x;
}
FUNCTION POP
char pop()
{
IF(top == -1)
return -1;
ELSE
return stack[top--];
}
FUNCTION PRIORITY
int priority(char x)
{
IF(x == '(')
return 0;
IF(x == '+' || x ==
'-')
return 1;
IF(x == '*' || x ==
} '/') return 2;
END

717821F263-VINUVARSITH M
SOURCE CODE:
#include<stdio.h>
#include<ctype.h>
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);

717821F263-VINUVARSITH M
printf("\n");
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e)
; else if(*e ==
')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
els
e
{
while(priority(stack[top]) >=
priority(*e)) printf("%c ",pop());
push(*e);

}
e++;
}
while(top != -1)
{
printf("%c ",pop());
}
return 0;
}

717821F263-VINUVARSITH M
OUTPUT:

RESULT:
Thus the program for infix to postfix conversion is executed successfully and the output is verified.

717821F263-VINUVARSITH M
Exp. No : 1.3(d)
EVALUATING EXPRESSION
Date :

AIM:
To write a C-program for removing duplicate elements

PSEUDOCODE:
BEGIN
GET top=-1,stack[100],i,op1,op2,res,x
GET char a[50],ch
VOID PUSH(INT)
INT POP
INT eval(CHAR,INT,INT)
GETS(a)
FOR i=0;a[i]!='\0';i++
ASSIGN a[i] to ch
IF ch>='0' && ch<='9'
PUSH('0')
ELSE

op2 = POP( );
op1 = POP( );
res = eval (ch, op1,
op2); PUSH (res);
ENDI
F
ASSIGN pop ( ) to x
PRINT x
GETCH ( );
VOID PUSH (INT n)
TOP++;
STACK [TOP] =
n;
INT POP( )
{

717821F263-VINUVARSITH M
res ;
INT res = STACK [top];
top--;
RETURN res;
INT eval (char ch, int op1,
int op2){ SWITCH (ch)
{
CASE '+' : RETURN
(op1+op2); CASE '-' : RETURN
(op1-op2); CASE '*' : RETURN
(op1*op2); CASE '/' : RETURN
} (op1/op2);
}
END

SOURCE CODE:
#include<stdio.h>
int top = -1, stack [100];
int main ( )
{
char a[50], ch;
int i,op1,op2,res,x;
void push (int);
int pop( );
int eval (char, int, int);
printf("enter a postfix
expression:"); gets (a);
for(i=0; a[i]!='\0'; i++)
{
ch = a[i];
if (ch>='0' &&
ch<='9') push('0');
else

717821F263-VINUVARSITH M
{
op2 = pop (
); op1 = pop
( );
res = eval (ch, op1, op2);
push (res);
}
x = pop ( );
printf("evaluated value = %d",
x); getch ( );
}
}
void push (int n)
{
top++;
stack [top] = n;
}
int pop ( )
{
int res ;
res = stack
[top]; top--;
return res;
}
int eval (char ch, int op1, int op2)
{
switch (ch)
{
case '+' : return
(op1+op2); case '-' : return
(op1-op2); case '*' : return
(op1*op2); case '/' : return
(op1/op2);
}

717821F263-VINUVARSITH M
}

OUTPUT:

RESULT:
Thus the program for evaluating the postfix expression is executed successfully and the
output is verified.

717821F263-VINUVARSITH M
Exp. No : 1.3 (e)
REMOVING DUPLICATE ELEMENT
Date :

AIM:
To write a C-program for removing duplicate elements

PSEUDOCODE:

BEGIN
GET a[50],i,j,k,count=0,dup[50],number
GET number
FOR i=0;i<number;i++
GET a[i]
ASSIGN dup[i]=-1
FOR i=0;i<number;i++
PRINT a[i]
FOR i=0;i<number;i++
FOR j=j+1;j<number;j++
IF a[i]==a[j]
FOR k = j; k <number; k+
+ ASSIGN a[k] = a[k+1]
DECREMENT j
DECREMENT NUMBER
ENDIF
END

SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[50],i,j,k, count = 0, dup[50],
number; printf("Enter size of the array\
n");

717821F263-VINUVARSITH M
scanf("%d",&number);
printf("Enter Elements of the array:\
n"); for(i=0; i<number; i++)
{
scanf("%d",&a[i]);
dup[i] = -1;
}
printf("Entered element are: \
n"); for(i=0; i<number; i++)
{
printf("%d ",a[i]);
}
for(i=0; i<number; i++)
{
for(j = i+1; j < number; j++)
{
if(a[i] == a[j])
{
for(k = j; k <number; k++)
{
a[k] = a[k+1];
}
j--;
number--;
}
}
printf("\nAfter deleting the duplicate element the Array is:\n");
for(i=0; i<number; i++)
{
printf("%d ",a[i]);
}
}

717821F263-VINUVARSITH M
OUTPUT:

RESULT:
Thus the program for finding the duplicate elements in the array is executed successfully and the
output is verified.

717821F263-VINUVARSITH M
Exp. No : 1.3 (f)
SUM OF REMAINING SKILLS
Date :

AIM:

To write a c program to sum the remaining skills of the frustrated coders.

PSEUDOCODE:
BEGIN
DECLARE n,temp,s as integer data
type INITIALIZE s=0
Print enter the no of coders
GET the value of n from
user
DECLARE a[n],c[n] as integer data
type Print enter the skill value of the
coders CHECK the condition using for
loop for(int i = 0; i < n; i++)
If the for condition is true
GET the value of a[i] from
user; INITIALIZE c[i]=0
CHECK the condition using for
loop for(int i = 0; i < n; i++)
for(int j = 0; j < n-i-1; j++)
If the loop condition are true then check the if condition
if( a[j] > a[j+1])
If the IF condition is true then ASSIGN
temp=a[j], a[j] = a[j+1] and a[j+1] = temp
INITIALIZE i=1
Check the WHILE
condition while(i!=n)
If while condition is true
then ASSIGN j=0;

717821F263-VINUVARSITH M
while(j<i)
IF condition is true then check the if
condition if((a[i]!=a[j])&&(c[j]!=1))
If the condition is true then ASSIGN c[j]=1then give break
INCREMENT j
INCREMENT i
CHECK the for loop
for( i=0;i<n;i++)
If the for loopcondition is true then check the if condition
if(c[i]==0)
COMPUTE s+=a[i];
Print the value of s
END

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,temp;
int s=0;
printf("enter the no of
coders:",n); scanf("%d",&n);
int a[n];
int c[n];
printf("enter the skill value of the
coders:"); for(int i = 0; i < n; i++)
{
scanf("%d",& a[i]);
c[i]=0;
}
for(int i = 0; i < n; i++)

717821F263-VINUVARSITH M
{
for(int j = 0; j < n-i-1; j++)
{
if( a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] =
} temp;
int i=1;
while(i!=n)
{

int j=0;
while(j<i)
{
if((a[i]!=a[j]) && (c[j]!=1))
{
c[j]=1;
break;
}
j+
+;
} i+
+;
}
for( i=0;i<n;i++)
{
if(c[i]==0)
{
s+=a[i];
}
}
printf("%d",s);

717821F263-VINUVARSITH M
return 0;
}

OUTPUT:

RESULT:
Thus the program is executed successfully and the output is verified.

717821F263-VINUVARSITH M

You might also like