}
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;
} }
} if(f==0)
int l=0,r=n-1,x,f=0; }
while(r>=l) {
{ printf("18BCE2158\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");
break; while(opt!=0)
} {
{ scanf("%d",&ch);
} scanf("%d",&n);
r=mid-1; for(i=0;i<n;i++)
} {
} scanf("%d",&ar[i]);
l=0,r=n-1; }
{ switch(ch)
int mid=l+(r-l)/2; {
{ lin(ar,n);
f=1; case 2:
break; liner(ar,n);
} break;
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");
scanf("%d",&opt);
if(opt==0)
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;
{ }//end of delete()
int ch=0; {
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();
void peep(); }
{ }
do{ {
printf("1.Push\n"); {
printf("3.IS_EMPTY\n"); }
printf("4.IS_FULL\n"); else
printf("5.Peep\n"); {
printf("7.Exit\n"); scanf("%d",&item);
printf("-----------------------"); top=top+1;
scanf("%d",&choice); }
switch(choice) }
case 1: {
break; if(top==-1)
case 2: {
break; }
break; {
item=stack[top]; {
} else
} {
{ for(i=top;i>=0;i--)
if(top==-1) {
{ printf("%d\n",stack[i]);
printf("Stack Underflow:"); }
} }
else }
void isFull()
if(top==(max_size-1))
printf("Stack Overflow");
else
void peep()
if(top==-1)
printf("\nStack is empty:");
else
void display()
int i;
if(top==-1)
return 1;
return 2;
int main()
char exp[20];
char *e, x;
scanf("%s",exp);
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c",*e);
push(*e);
Question 4(a): {
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;
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;
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;
gets(s);
init();
for(i=0;i<strlen(s);i++)
{
if(s[i]=='(')
push(s[i]);
else if(s[i]==')')
if(isempty())
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("-----------------------");
scanf("%d",&choice);
switch(choice)
case 1:
insert();
break;
case 2:
del(); void del() //deleting an element from the queue
break; {
isEmpty(); if(front==-1)
break; {
isFull(); }
break; else
case 5: {
display(); item=queue[front];
case 6: if(front==rear)
peek(); {
break; front=-1;
case 7: rear=-1;
exit(0); }
break; else
default: {
break; }
} }
} {
{ {
if(rear==(max_size-1)) }
{ else
else }
{ void isFull()
scanf("%d",&item); if(rear==(max_size-1))
rear=rear+1; {
if(front==-1) else
if(front==-1)
printf("\nQueue Underflow:");
else
int i;
if(front==-1)
printf("\nQueue is Empty:");
else
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;
void insert();
void del();
void display();
/*-------------------------------------*/
int main()
int choice;
do{
printf("1.EnQueue\n");
printf("2.DeQueue\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("-----------------------");
scanf("%d",&choice);
switch(choice)
case 1:
insert();
break; int item;
case 2: if(front==-1)
del(); {
case 3: }
display(); else
break; {
case 4: item=cqueue[front];
break; if(front==rear)
default: {
break; rear=-1;
} }
}while(choice!=4); else
return 1; {
} front=(front+1)%max_size;
int item; }
} {
else int i;
{ if(front==-1)
rear=(rear+1)%max_size; }
cqueue[rear]=item; else
{ if(front<rear)
front=0; {
rear=0; for(i=front;i<=rear;i++)
} {
printf("%d\t",cqueue[i]);
} }
{ {
printf("%d\t",cqueue[i]);
for(i=front;i<max_size;i++)
printf("%d\t",cqueue[i]);
}//end of display()