Dsa File1-9
Dsa File1-9
#include <stdio.h>
void rotate(int arr[],int n,int k)
{
int temp;
for(int i=0;i<k/2;i++)
{
temp=arr[i];
arr[i]=arr[k-i-1];
arr[k-i-1]=temp;
}
for(int i=k;i<(n+k)/2;i++){
temp=arr[i];
arr[i]=arr[n-i-1+k];
arr[n-i-1+k]=temp;
}
for(int i=0;i<n/2;i++)
{
temp=arr[i];
arr[i]=arr[n-i-1];
arr[n-i-1]=temp;
}
}
int main()
{
int n,k,test;
printf("Enter test-cases: ");
scanf("%d",&test);
while(test!=0){
printf("\nEnter size of array: ");
scanf("%d",&n);
int arr[n];
printf("enter %d elements\n",n);
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter number of rotation: ");
scanf("%d",&k);
rotate(arr,n,k);
for(int i=0;i<n;i++){
printf("%d ",arr[i]);
}
test--;
}
return 0;
}
OUTPUT:
PS C:\Users\acetr\OneDrive\Desktop\DSA> cd "c:\Users\acetr\OneDrive\Desktop\DSA\C\" ;
if ($?) { g++ 1.C -o 1 } ; if ($?) { .\1 }
Enter test-cases: 3
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&mat[i][j]);
}
}
rotate(n,mat);
printf("elements of matrix after rotation in clockwise direction:\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
printf("%d ",mat[i][j]);
}
printf("\n");
}
test--;
}
return 0;
}
OUTPUT:
PS C:\Users\acetr\OneDrive\Desktop\DSA> cd "c:\Users\acetr\OneDrive\Desktop\DSA\c\" ;
if ($?) { gcc 6.c -o 6 } ; if ($?) { .\6 }
Enter test-cases: 3
if(choice==1)
{
printf("Enter element: ");
scanf("%d",&n);
push(size,stack,&top,n);
}
else if(choice==2)
{
printf("Poped: %d",pop(stack,&top));
}
else if(choice==3)
{
printf("Top element: %d",topelement(stack,top));
}
else if(choice==4)
{
display(stack,top);
}
else if(choice==5)
{
printf("Size: %d",SIZE(top));
}
else if(choice==6)
{
if(isEmpty(&top))
printf("Stack is Empty\n");
else
printf("Stack is Not Empty\n");
}
else if(choice==7)
{
printf("End\n");
break;
}
}
return 0;
}
OUTPUT:
PS C:\Users\acetr\OneDrive\Desktop\DSA> cd "c:\Users\acetr\OneDrive\Desktop\DSA\c\" ;
if ($?) { gcc 7.c -o 7 } ; if ($?) { .\7 }
Enter size of stack: 10
1 for Push
2 for Pop
3 to Print top element
4 to Print all elements
5 for Size of stack
6 for IsEmpty?
7 to Quit
Enter your choice: 1
Enter element: 34
1 for Push
2 for Pop
3 to Print top element
4 to Print all elements
5 for Size of stack
6 for IsEmpty?
7 to Quit
Enter your choice: 1
Enter element: 42
1 for Push
2 for Pop
3 to Print top element
4 to Print all elements
5 for Size of stack
6 for IsEmpty?
7 to Quit
Enter your choice: 1
Enter element: 6
1 for Push
2 for Pop
3 to Print top element
4 to Print all elements
5 for Size of stack
6 for IsEmpty?
7 to Quit
Enter your choice: 4
6 42 34
1 for Push
2 for Pop
3 to Print top element
4 to Print all elements
5 for Size of stack
6 for IsEmpty?
7 to Quit
Enter your choice: 2
Poped: 6
1 for Push
2 for Pop
3 to Print top element
4 to Print all elements
5 for Size of stack
6 for IsEmpty?
7 to Quit
Enter your choice: 4
42 34
1 for Push
2 for Pop
3 to Print top element
4 to Print all elements
5 for Size of stack
6 for IsEmpty?
7 to Quit
Enter your choice: 5
Size: 2
1 for Push
2 for Pop
3 to Print top element
4 to Print all elements
5 for Size of stack
6 for IsEmpty?
7 to Quit
Enter your choice: 6
Stack is Not Empty
1 for Push
2 for Pop
3 to Print top element
4 to Print all elements
5 for Size of stack
6 for IsEmpty?
7 to Quit
Enter your choice: 7
End
/* Ques 8: Given an expression string consisting of opening and closing brackets
“{“,”}”,”(“,”)”,”[“,”]”, design an algorithm and a program to check whether this
expression has balanced parenthesis or not.
Name :- Tejassvi Bhhati
Section :- I2
Roll No.:-119
Course :- B.Tech (CSE)
*/
#include <stdio.h>
#include <string.h>
int check(char str[],int size)
{
if(size%2!=0)
{
return 0;
}
int stack[size];
int top=-1;
for(int i=0;i<size;i++)
{
if(str[i]=='(' || str[i]=='[' || str[i]=='{')
{
push(stack,&top,str[i]);
}
else if(str[i]==')')
{
if(top==-1)
{
return 0;
}
else if(stack[top]=='(')
{
pop(&top);
}
else
{
return 0;
}
}
else if(str[i]==']')
{
if(top==-1)
{
return 0;
}
else if(stack[top]=='[')
{
pop(&top);
}
else
{
return 0;
}
}
else if(str[i]=='}')
{
if(top==-1)
{
return 0;
}
else if(stack[top]=='{')
{
pop(&top);
}
else
{
return 0;
}
}
}
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}
OUTPUT:
PS C:\Users\acetr\OneDrive\Desktop\DSA> cd "c:\Users\acetr\OneDrive\Desktop\DSA\c\" ;
if ($?) { gcc 8.c -o 8 } ; if ($?) { .\8 }
Enter test-cases: 3
OUTPUT:
PS C:\Users\acetr\OneDrive\Desktop\DSA> cd "c:\Users\acetr\OneDrive\Desktop\DSA\c\" ;
if ($?) { gcc 9.c -o 9 } ; if ($?) { .\9 }
Enter test-cases: 3