Anda di halaman 1dari 19

Question 1 printf("Number of times element found is %d\n",occ);

}
Code:
else
//18BCE2158
{
//Yash Dungarwal
printf("Element not found\n");
#include<stdio.h>
}
void lin(int a[],int n)
}
{
int sor(int a[],int n)
int i,f=0,x,pos;
{
printf("Enter the value to be found\n");
int i,j,temp;
scanf("%d",&x);
for (i=0;i<n-1;i++)
for(i=0;i<n;i++)
{
{
for (j=0;j<n-i-1;j++)
if(x==a[i])
{
{
if(a[j]>a[j+1])
f=1;
{
pos=i+1;
temp=a[j];
break;
a[j]=a[j+1];
}
a[j+1]=temp;
}
}
if(f==1)
}
{
}
printf("Element found at position %d\n",pos);
return a;
}
}
else
void bin(int a[],int n)
{
{
printf("Element not found\n");
int l=0,r=n-1,x,f=0;
}
printf("Enter the value to be found\n");
}
scanf("%d",&x);
void liner(int a[],int n)
while(r>=l)
{
{
int i,f=0,x,occ=0;
int mid=l+(r-l)/2;
printf("Enter the value to be found\n");
if(a[mid]==x)
scanf("%d",&x);
{
for(i=0;i<n;i++)
printf("Element found at position %d\n",mid+1);
{
f=1;
if(x==a[i])
break;
{
}
f=1;
else if(a[mid]>x)
occ++;
{
}
r=mid-1;
}
}
if(f==1)
else
{
{ {

l=mid+1; r=mid-1;

} }

} else

if(f==0) {

{ l=mid+1;

printf("Element not found\n"); }

} }

} if(f==0)

void bine(int a[],int n) {

{ printf("Element not found\n");

int l=0,r=n-1,x,f=0; }

printf("Enter the value to be found\n"); }

scanf("%d",&x); int main()

while(r>=l) {

{ printf("18BCE2158\n");

int mid=l+(r-l)/2; printf("1.Linear search\n");

if((mid==0 || x>a[mid-1]) && a[mid]==x) printf("2.Linear search to find duplicate\n");

{ printf("3.Binary search\n");

printf("Element first occurred at position %d\n",mid+1); printf("4.Binary search first and last occurrence\n");

f=1; int i,n,ch,opt=1;

break; while(opt!=0)

} {

else if(x>a[mid]) printf("Enter choice\n");

{ scanf("%d",&ch);

l=mid+1; printf("Enter length of array\n");

} scanf("%d",&n);

else int ar[n];

{ printf("Enter values for the array\n");

r=mid-1; for(i=0;i<n;i++)

} {

} scanf("%d",&ar[i]);

l=0,r=n-1; }

while(r>=l) int *p;

{ switch(ch)

int mid=l+(r-l)/2; {

if((mid==n-1 || x<a[mid+1]) && a[mid]==x) case 1:

{ lin(ar,n);

printf("Element last occurred at position %d\n",mid+1); break;

f=1; case 2:

break; liner(ar,n);

} break;

else if(x<a[mid]) case 3:


p=sor(ar,n);

for (i=0;i<n;i++)

ar[i]=(*(p + i));

printf("Sorted array\n");

for(i=0;i<n;i++)

printf("%d ",ar[i]);

printf("\n");

bin(ar,n);

break;

case 4:

p=sor(ar,n);

for (i=0;i<n;i++)

ar[i]=(*(p + i));

printf("Sorted array\n");

for(i=0;i<n;i++)

printf("%d ",ar[i]);

printf("\n");

bine(ar,n);

break;

default:

printf("Invalid choice\n");

printf("Do you want to continue 1/0\n");

scanf("%d",&opt);

if(opt==0)

printf("Thanks for using");

return 1;

}
break;

case 3:

insert();

break;

case 4:

del();

break;

case 5: update();

break;

Question 2 case 6:

exit(0);
Code:
break;

#include<stdio.h>
default:
#include<stdlib.h>
printf("\nInvalid choice:\n");
int a[20];
break;
int n,p,val,i,pos;
}
/*Function Prototype*/
}while(choice!=6);
void create();
return 0;
void display();
}
void insert();
void create() //creating an array
void del();
{
void update();
printf("\nEnter the size of the array elements:\t");
int main()
scanf("%d",&n);
{
printf("\nEnter the elements for the array:\n");
int choice;
for(i=0;i<n;i++)
do{
{
printf("\n\n--------Menu-----------\n");
scanf("%d",&a[i]);
printf("1.Create\n");
}
printf("2.Display\n");
}//end of create()
printf("3.Insert\n");
void display() //displaying an array elements
printf("4.Delete\n");
{
printf("5.Update\n");
int i;
printf("6.Exit\n");
printf("\nThe array elements are:\n");
printf("-----------------------");
for(i=0;i<n;i++){
printf("\nEnter your choice:\t");
printf("%d\t",a[i]);
scanf("%d",&choice);
}
switch(choice)
}//end of display()
{
void insert() //inserting an element in to an array
case 1: create();
{
break;
printf("\n1)By Position 2)By Data:\t");
case 2:
int ch=0;
display();
scanf("%d",&ch); printf("\nThe deleted element is =%d",val);

switch(ch) break;

{ case 2:

case 1:

printf("\nEnter the position for the new printf("\nEnter the element to be deleted :\t");
element:\t");
scanf("%d",&val);
scanf("%d",&pos);
int j=0,q=0;
printf("\nEnter the element to be inserted :\t");

scanf("%d",&val);
for(i=0;i<n-1;i++)
for(i=n-1;i>=pos-1;i--)
{
{
if(a[i]==val && q==0)
a[i+1]=a[i];
{
}
for(j=i;j<n;j++)
a[pos-1]=val;
{
n=n+1;
a[j]=a[j+1];
break;
}
case 2:
n=n-1;
printf("\nEnter the element to be inserted :\t");
q=q+1;
scanf("%d",&val);
printf("\nThe element deleted
a[n]=val; at %d",i);

n=n+1; }

break;

default:printf("\nEnter valid Method :\t"); }

}//end of insert() break;

default:printf("\nEnter valid Method :\t");

void del() //deleting an array element

{ }//end of delete()

printf("\n1)By Position 2)By Data:\t"); void update() //updating an array element

int ch=0; {

scanf("%d",&ch); printf("\nEnter the position of the element to be updated:\t");

switch(ch) scanf("%d",&p);

{ int k=0,l=0;

case 1: k=a[p-1];

printf("\nEnter the position of the element to be printf("\nEnter the New element :\t");
deleted:\t");
scanf("%d",&l);
scanf("%d",&pos);
a[p-1]=l;
val=a[pos-1];
printf("\nValue of Position %d updated from %d to %d",p,k,l);
for(i=pos-1;i<n-1;i++)
}//end of update()
{

a[i]=a[i+1];

n=n-1;
case 4: isFull();

break;

case 5:
Question 3: peep();

Code: break;

#include<stdio.h> case 6:

#include<stdlib.h> display();

#define max_size 5 break;

int stack[max_size],top=-1; case 7:

/*------ Function Prototype------------*/ exit(0);

void push(); break;

void pop(); default:

void isEmpty(); printf("\nInvalid choice:\n");

void isFull(); break;

void peep(); }

void display(); }while(choice!=7);

int main() return 0;

{ }

int choice; void push() //Inserting element in to the stack

do{ {

//printf("\n"); int item;

printf("\n\n--------STACK OPERATIONS-----------\n"); if(top==(max_size-1))

printf("1.Push\n"); {

printf("2.Pop\n"); printf("\nStack Overflow:");

printf("3.IS_EMPTY\n"); }

printf("4.IS_FULL\n"); else

printf("5.Peep\n"); {

printf("6.Display\n"); printf("Enter the element to be inserted:\t");

printf("7.Exit\n"); scanf("%d",&item);

printf("-----------------------"); top=top+1;

printf("\nEnter your choice:\t"); stack[top]=item;

scanf("%d",&choice); }

switch(choice) }

{ void pop() //deleting an element from the stack

case 1: {

push(); int item;

break; if(top==-1)

case 2: {

pop(); printf("Stack Underflow:");

break; }

case 3: isEmpty(); else

break; {
item=stack[top]; {

top=top-1; printf("\nStack is Empty:");

printf("\nThe poped element: %d\t",item); }

} else

} {

void isEmpty() printf("\nThe stack elements are:\n" );

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

if(top==-1) {

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

printf("Stack Underflow:"); }

} }

else }

printf("Stack Not Empty");

void isFull()

if(top==(max_size-1))

printf("Stack Overflow");

else

printf("Stack Not Full");

void peep()

if(top==-1)

printf("\nStack is empty:");

else

printf("The topmost element of the stack is %d",stack[top]);

void display()

int i;

if(top==-1)
return 1;

if(x == '*' || x == '/')

return 2;

int main()

char exp[20];

char *e, x;

printf("Enter the expression :: ");

scanf("%s",exp);

e = exp;

while(*e != '\0')

if(isalnum(*e))

printf("%c",*e);

else if(*e == '(')

push(*e);

else if(*e == ')')

Question 4(a): {

while((x = pop()) != '(')


Code:
printf("%c", x);
#include<stdio.h>
}
char stack[20];
else
int top = -1;
{
void push(char x)
while(priority(stack[top]) >= priority(*e))
{
printf("%c",pop());
stack[++top] = x;
push(*e);
}
}

e++;
char pop()
}
{
while(top != -1)
if(top == -1)
{
return -1;
printf("%c",pop());
else
}
return stack[top--];
return 1;
}
}

int priority(char x)

if(x == '(')

return 0;

if(x == '+' || x == '-')


}

else

n1 = pop();

n2 = pop();

switch(*e)

case '+':

n3 = n1 + n2;

break;

case '-':

Question 4(b) {

n3 = n2 - n1;
Code:
break;
#include<stdio.h>
}
int stack[20];
case '*':
int top = -1;
{

n3 = n1 * n2;
void push(int x)
break;
{
}
stack[++top] = x;
case '/':
}
{

n3 = n2 / n1;
int pop()
break;
{
}
return stack[top--];
}
}
push(n3);

}
int main()
e++;
{
}
char exp[20];
printf("\nThe result of expression %s = %d\n\n",exp,pop());
char *e;
return 0;
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);
Question 4(c)
Code:
#include<stdio.h>

#include<string.h>

#include<conio.h>

struct stack

char a[30];

int top;

}ss;

void init()

ss.top=-1;

void push(char c)

ss.top++;

ss.a[ss.top]=c;

char pop()

if(ss.top==-1)

return 0;

return(ss.a[ss.top--]);

int isempty()

if(ss.top==-1)

return 1;

else

return 0;

int main()

printf("18BCE2158");

char s[30];

int i;

printf("Enter the expression\n");

gets(s);

init();

for(i=0;i<strlen(s);i++)
{

if(s[i]=='(')

push(s[i]);

else if(s[i]==')')

if(isempty())

printf("There is no matching opening parenthesis\n");

else Question 5
pop();
Code:
}
#include<stdio.h>
}
#include<stdlib.h>
if(!isempty())
#define max_size 5
printf("There are no matching closing parenthesis\n");
int queue[max_size],front=-1,rear=-1;
else
/*------ Function Prototype------------*/
printf("The expression has balanced parenthesis ");
void insert();
getch();
void del();
return 0;
void isEmpty();
}
void isFull();

void peek();

void display();

/*-------------------------------------*/

int main()

int choice;

do{

printf("\n\n--------QUEUE OPERATIONS-----------\n");

printf("1.EnQueue\n");

printf("2.DeQueue\n");

printf("3.isEmpty\n");

printf("4.isFUll\n");

printf("5.Display\n");

printf("6.Peek\n");

printf("7.Exit\n");

printf("-----------------------");

printf("\nEnter your choice:\t");

scanf("%d",&choice);

switch(choice)

case 1:

insert();

break;

case 2:
del(); void del() //deleting an element from the queue

break; {

case 3: int item;

isEmpty(); if(front==-1)

break; {

case 4: printf("\nQueue Underflow:");

isFull(); }

break; else

case 5: {

display(); item=queue[front];

break; printf("\nThe deleted element: %d\t",item);

case 6: if(front==rear)

peek(); {

break; front=-1;

case 7: rear=-1;

exit(0); }

break; else

default: {

printf("\nInvalid choice:\n"); front=front+1;

break; }

} }

}while(choice!=7); }//end of del()

return 0; void isEmpty()

} {

void insert() //Inserting an element in to the queue if(front==-1)

{ {

int item; printf("\nQueue Underflow:");

if(rear==(max_size-1)) }

{ else

printf("\nQueue Overflow:"); printf("\nQueue not Empty\t");

else }

{ void isFull()

printf("Enter the element to be inserted:\t"); {

scanf("%d",&item); if(rear==(max_size-1))

rear=rear+1; {

queue[rear]=item; printf("\nQueue Overflow:");

if(front==-1) else

front=0; printf("\nQueue not Full\t");

}//end of insert() void peek()


{

if(front==-1)

printf("\nQueue Underflow:");

else

printf("\nThe next element is: %d\t",queue[front]);

void display() //To display the queue elements

int i;

if(front==-1)

printf("\nQueue is Empty:");

else

printf("\nThe queue elements are:\n" );

for(i=front;i<=rear;i++)

printf("%d\t",queue[i]);

}//end of display()
Question 6:
Code:

#include<stdio.h>

#include<stdlib.h>

#define max_size 5

int cqueue[max_size],front=-1,rear=-1;

/*------ Function Prototype------------*/

void insert();

void del();

void display();

/*-------------------------------------*/

int main()

int choice;

do{

printf("\n\n--------CIRCULAR QUEUE OPERATIONS-----------\n");

printf("1.EnQueue\n");

printf("2.DeQueue\n");

printf("3.Display\n");

printf("4.Exit\n");

printf("-----------------------");

printf("\nEnter your choice:\t");

scanf("%d",&choice);

switch(choice)

case 1:

insert();
break; int item;

case 2: if(front==-1)

del(); {

break; printf("\nQueue Underflow:");

case 3: }

display(); else

break; {

case 4: item=cqueue[front];

exit(0); printf("\nThe deleted element: %d\t",item);

break; if(front==rear)

default: {

printf("\nInvalid choice:\n"); front=-1;

break; rear=-1;

} }

}while(choice!=4); else

return 1; {

} front=(front+1)%max_size;

void insert() //Inserting an element in to the queue }

int item; }

if(front==(rear+1)%max_size) }//end of del()

printf("\nQueue Overflow:"); void display() //To display the queue elements

} {

else int i;

{ if(front==-1)

printf("Enter the element to be inserted:\t"); {

scanf("%d",&item); printf("\nQueue is Empty:");

rear=(rear+1)%max_size; }

cqueue[rear]=item; else

if(front==-1) printf("\nThe queue elements are:\n" );

{ if(front<rear)

front=0; {

rear=0; for(i=front;i<=rear;i++)

} {

printf("%d\t",cqueue[i]);

} }

}//end of insert() else

void del() //deleting an element from the queue for(i=0;i<=rear;i++)

{ {
printf("%d\t",cqueue[i]);

for(i=front;i<max_size;i++)

printf("%d\t",cqueue[i]);

}//end of display()

Anda mungkin juga menyukai