0% found this document useful (0 votes)
45 views22 pages

Assignment 2 - DS

Jdjdjdjdndndndjdjdjjffjjfjdjfjfjdjdjfjfjfjfjejsnzn gdhdndhdh hdhdhdj but itis not an

Uploaded by

gafiw26278
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views22 pages

Assignment 2 - DS

Jdjdjdjdndndndjdjdjjffjjfjdjfjfjdjdjfjfjfjfjejsnzn gdhdndhdh hdhdhdj but itis not an

Uploaded by

gafiw26278
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Assignment -2

Course - Data Structure (CSC 202)

Enroll .no. -2019btcse008

Q.1 Write C code for following condi on using recursive func


on.

(i) Enter a number and find factorial of that no..

(ii) Enter two no. and find GCD with help of Euclidean algorithm.

ans. (ii)

code = #include<stdio.h>

int gcd(int n1 ,int n2)

{ if(n1 == 0 )return(n2); if(n2

== 0)return(n1);

if(n1>n2)

return(gcd(n2 ,n1%n2));

else

return(gcd(n1, n2%n1));

int main()

int num1 ,num2 ;

prin ("enter two numbers\

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;

printf ("Enter a positive integer: ");

scanf("%d",&n);

prin ("Factorial ourn 0;

} long int mul plyNumbers(int

n) {

if (n>=1)

return n*mul plyNumbers(n-1);


else

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.

(c) display(): Display all stack elements.


ans. code =

#include<stdio.h> int

stack[100],choice,n,top,x,i,s;

void push(void); void

pop(void); void display(void);

int main() {

//clrscr(); top=-1; prin ("\n Enter the


size of STACK[MAX=100]:"); scanf("%d",&n);
prin ("\n\t STACK OPERATIONS USING ARRAY");

prin ("\n\t--------------------------------");
("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t
prin 4.EXIT");
do

prin ("\n Enter the Choice:");

scanf("%d",&choice); switch(choice)
{ case

1: {

push();

break;

case 2: {

pop();

break; }

case 3: {

display();

break; }

case 4:

prin ("\n\t EXIT POINT

"); break; }

default:

prin ("\n\t Please Enter a Valid Choice(1/2/3/4)");

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 ("\n The elements in STACK \


n"); for(i=top; i>=0; i--) prin
("\n%d",stack[i]);
("\n Press Next Choice");

prin }

else

{
("\n The STACK is
prin empty");
}

} output

(ii)Convert Infix Arithme c Expressions into PostFix Arithme c Expressions

and evaluate them. code =

#include<stdio.h>

#include<conio.h>
#define n 50

char stack[n];

int top=-1,j=0;

char pos ix[50];

void push(char);

char pop(); int

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.

(a+b)\n"); prin ("Expression :"); gets(infix); prin ("\nSymbol\tStack content\tpos ix

expression");

for(i=0;infix[i]!=NULL;i+

+)

{ ch=infix[i];

if(ch>='a' && ch<='z')

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)

prin ("stack is underflow");

exit(0);

else
{

top=top-1;

item=stack[top+1];

stack[top+1]=NULL;

return item; } int priority(char

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

(iii) solve the hanoi tower problem

code =
/*

* C program for Tower of Hanoi using Recursion

*/

#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 0;

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);


}

output =

q.3 Write Code to perform opera on on circular queue.

(i) Enque() : Enter element into queue.

(ii) Deque() : Delete element from

queue.

code = #include <stdio.h>

#include <stdlib.h>
#include <limits.h>

// Queue capacity

#define CAPACITY 100

int queue[CAPACITY];

unsigned int size = 0; unsigned int rear = CAPACITY - 1;

int front = 0;

int enqueue(int data);

int dequeue();

int isFull();

int isEmpty();

int getRear();

nit getFront();
int main()

int ch, data;

/* Run indefinitely un l user manually terminates */

while (1)

/* Queue menu */
prin ("--------------------------------------\n");
(" QUEUE ARRAY IMPLEMENTATION PROGRAM \
prin
n");
prin ("--------------------------------------\n");

prin ("1. Enqueue\n");

prin ("2. Dequeue\n");

prin ("3. Size\n");

prin ("4. Get Rear\n");

prin ("5. Get Front\n");

prin ("0. Exit\n");

prin ("--------------------------------------\n");

prin ("Select an op on: ");

scanf("%d", &ch);
/* Menu control switch */

switch (ch)

case 1: prin

("\nEnter

data to

enqueue: ");

scanf("%d",d

ata);

// Enqueue func on returns 1 on success

// otherwise 0

if (enqueue(data))

Printf ("Element added to queue.");

else

printf ("Queue is full.");

break;

case 2:

data = dequeue();

// on success dequeue returns element removed

// otherwise returns

INT_MIN

if (data == INT_MIN)

printf("Queue is empty.");
else

printf ("Data => %d", data);

break;

case 3:

// isEmpty() func on returns 1 if queue is emtpy

// otherwise returns 0

if (isEmpty())

printf("Queue is empty.");

else

printf("Queue size => %d", size);

break;

case 4:

if (isEmpty())

printf ("Queue is empty.");

else

printf("Rear => %d", getRear());

break;

case 5:
if (isEmpty())

printf ("Queue is empty.");

else

printf("Front => %d", getFront());

break;

case 0:
printf("Exi ng from app.\n");

exit(0);

default:

printf("Invalid choice, please input number between (0-5).");

break;

printf("\n\

n");

int enqueue(int

data)

// Queue is full throw Queue out of capacity error.


if (isFull())

{ return

0;

// Ensure rear never crosses array bounds

rear = (rear + 1) % CAPACITY;

// Increment queue size

size++;

// Enqueue new element to queue

queue[rear] = data;

// Successfully enqueued element to queue

return 1;

/**

* Dequeue/Remove an element from the queue.

*/ int dequeue() {

int data = INT_MIN;

// Queue is empty, throw Queue underflow

error
if (isEmpty())

return INT_MIN;

// Dequeue element from queue

data = queue[front];

// Ensure front never crosses array bounds

front = (front + 1) % CAPACITY;

// Decrease queue size

size--;

return data;

/**

* Checks if queue is full or not. It returns 1 if queue is full,

* overwise returns 0.

*/

int isFull() {

return (size ==

CAPACITY);

}
/**

* Checks if queue is empty or not. It returns 1 if queue is empty,

* otherwise returns 0.

*/ int

isEmpty()

{
return (size == 0);

/**

* Gets, front of the queue. If queue is empty return INT_MAX


otherwise

* returns front of queue.

*/

int getFront() {

return (isEmpty())

? INT_MIN

: queue[front];

/**

* Gets, rear of the queue. If queue is empty return INT_MAX


otherwise

* returns rear of queue.


*/ int getRear() {

return (isEmpty())

? INT_MIN

: queue[rear];

} output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy