Assignment 2 - DS
Assignment 2 - DS
(ii) Enter two no. and find GCD with help of Euclidean algorithm.
ans. (ii)
code = #include<stdio.h>
== 0)return(n1);
if(n1>n2)
return(gcd(n2 ,n1%n2));
else
return(gcd(n1, n2%n1));
int main()
n"); scanf("%d
%d",&num1,&num2);
printf("%d",gcd(num1,num2));
return
0; }
output =
(i) code
#include<
stdio.h>
long int
mul
plyNumb
ers(int
n); int
main() {
int n;
scanf("%d",&n);
n) {
if (n>=1)
return 1;
} output
Q.2 1. Implement Stack using array with crea on of two func ons push(), pop()
and display().
(a) push(): whenever you insert a element into stack call this func
on.
(b) pop(): whenever you delete a element from stack call this func
on.
#include<stdio.h> int
stack[100],choice,n,top,x,i,s;
int main() {
prin ("\n\t--------------------------------");
("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t
prin 4.EXIT");
do
scanf("%d",&choice); switch(choice)
{ case
1: {
push();
break;
case 2: {
pop();
break; }
case 3: {
display();
break; }
case 4:
"); break; }
default:
while(choice!=4);
return 0;
} void push()
{ if(top>=n
-1)
{
prin ("\n\tSTACK is over flow");
else
{
(" Enter a value to be
prin pushed:");
scanf("%d",&x);
top++; stack[top]=x;
} void pop()
{ if(top<=
-1)
{
("\n\t Stack is under flow");
prin }
else
{
("\n\t The popped elements is
prin %d",stack[top]);
top--;
} void
display()
{ if(top>=
0)
prin }
else
{
("\n The STACK is
prin empty");
}
} output
#include<stdio.h>
#include<conio.h>
#define n 50
char stack[n];
int top=-1,j=0;
void push(char);
priority(char);
void
main()
int i;
char element,ch;
char infix[50];
clrscr();
prin ("Enter infix expression\nNote: Put '(' at start and ')' at end of expression, ie.
expression");
for(i=0;infix[i]!=NULL;i+
+)
{ ch=infix[i];
pos ix[j]=ch;
j++; }
else if(ch=='(')
push(ch);
} else
if(ch==')')
{
while((element=pop())!='(')
pos ix[j]=element;
j+
+; }
else
while(priority(ch)<=priority(stack[top]))
if(stack[top]=='(')
break;
element=pop(); pos
ix[j]=element;
j++;
}
push(ch);
pos ix[j]=NULL;
prin ("\n%c\t%s\t\t%s",ch,stack,pos
ix);
getch();
while((element=pop())!
='(')
{
pos ix[j]=element;
j++;
getch(); } void
push(char ch)
{ if(top>=n-1)
prin ("overflow");
else
{
top=top+1;
stack[top]=ch;
} char
pop()
{ char
item;
if(top==-1)
exit(0);
else
{
top=top-1;
item=stack[top+1];
stack[top+1]=NULL;
ch) { char
operand[6]={'+','-','*','/','(','\0'}; int
prio[5]={1,1,2,2,3};
int i,a;
for(i=0;i<5;i+
+)
if(ch==operand[i])
{ a=prio[i]
; break;
} }
return a;
} output
code =
/*
*/
int
main() {
int num;
scanf("%d", &num);
are : \n");
return 0;
if (num == 1)
topeg);
return 0;
topeg);
output =
queue.
#include <stdlib.h>
#include <limits.h>
// Queue capacity
int queue[CAPACITY];
int front = 0;
int dequeue();
int isFull();
int isEmpty();
int getRear();
nit getFront();
int main()
while (1)
/* Queue menu */
prin ("--------------------------------------\n");
(" QUEUE ARRAY IMPLEMENTATION PROGRAM \
prin
n");
prin ("--------------------------------------\n");
prin ("--------------------------------------\n");
scanf("%d", &ch);
/* Menu control switch */
switch (ch)
case 1: prin
("\nEnter
data to
enqueue: ");
scanf("%d",d
ata);
// otherwise 0
if (enqueue(data))
else
break;
case 2:
data = dequeue();
// otherwise returns
INT_MIN
if (data == INT_MIN)
printf("Queue is empty.");
else
break;
case 3:
// otherwise returns 0
if (isEmpty())
printf("Queue is empty.");
else
break;
case 4:
if (isEmpty())
else
break;
case 5:
if (isEmpty())
else
break;
case 0:
printf("Exi ng from app.\n");
exit(0);
default:
break;
printf("\n\
n");
int enqueue(int
data)
{ return
0;
size++;
queue[rear] = data;
return 1;
/**
*/ int dequeue() {
error
if (isEmpty())
return INT_MIN;
data = queue[front];
size--;
return data;
/**
* overwise returns 0.
*/
int isFull() {
return (size ==
CAPACITY);
}
/**
* otherwise returns 0.
*/ int
isEmpty()
{
return (size == 0);
/**
*/
int getFront() {
return (isEmpty())
? INT_MIN
: queue[front];
/**
return (isEmpty())
? INT_MIN
: queue[rear];
} output