You are on page 1of 8

//LAB-3

//Question-1

#include<stdio.h>
#define MAX 106
int qA[MAX]; //implementing queue using array
int rear1=-1,front1=-1;

//function for inserting elements in queue


void insert(int item)
{
if(rear1==MAX-1) //checking if queue is already full
printf("Queue overflow");
else
{ //inserting elements in queue
if(front1==-1)
front1=0;
rear1=rear1+1;
qA[rear1]=item;
}
}

int main()
{
int i,n,temp,qB[MAX],front2,rear2;
printf("Enter the size of list: ");
scanf("%d",&n);
printf("Enter the elements of list:\n");
for(i=0;i<n;i++)
{
scanf("%d",&temp);
insert(temp);
}
temp=n-1;

//loop for storing elements of queue A into queue B in reverse order


for(i=0;i<n;i++)
qB[temp--]=qA[i];
front2=front1;
rear2=rear1;

//loop for checking conditions given in question and printing the output
while(front1<=rear1 && front2<=rear2)
{
if(qA[front1]>qB[front2])
{
printf("1 ");
front2++;
}
else if(qA[front1]<qB[front2])
{
printf("2 ");
front1++;
}
else
{
printf("0 ");
front1++;
front2++;
}
}
return 0;
}

//Question-2

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 1000

struct stack
{
char stk[MAX];
int top;
}s; //creating stack

void push(char data) //function for push operation in stack


{
if(s.top==(MAX-1))
printf("Stack is full!!");
else
{
s.top=s.top+1;
s.stk[s.top]=data;
}
}
void pop() //function for pop operation in stack
{
if(s.top==-1)
{
printf("Stack is empty!!");
}
else
{
s.top=s.top-1;
}
}

int main()
{
char str[MAX],a[MAX][4];
int i=0,j=0,n;
s.top=-1;
printf("Enter number of strings: ");
scanf("%d",&n);
printf("Enter strings:\n");

for(j=0;j<n;j++)
{
scanf("%s",str);
for(i=0;i<strlen(str);i++)
{
if(str[i]=='(' || str[i]=='[' || str[i]=='{')
{
push(str[i]);
continue;
}
else if(str[i]==')' || str[i]==']' || str[i]=='}')
{
if(str[i]==')')
{
if(s.stk[s.top]=='(')
pop();
else
{
strcpy(a[j],"NO");
break;
}
}
if(str[i]==']')
{
if(s.stk[s.top]=='[')
pop();
else
{
strcpy(a[j],"NO");
break;
}
}
if(str[i]=='}')
{
if(s.stk[s.top]=='{')
pop();
else
{
strcpy(a[j],"NO");
break;
}
}
}
}
if(s.top==-1)
strcpy(a[j],"YES");
s.top=-1;
}
for(i=0;i<n;i++)
{
for(j=0;a[i][j]!='\0';j++)
printf("%c",a[i][j]);
printf("\n");
}
return 0;
}
Output
Question-1

Question-2

You might also like