You are on page 1of 19

#include<stdio.

h>

#include<stdlib.h>

#include<string.h>

int order(char op) {

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

return 1;

else if (op == '*' || op == '/')

return 2;

else if (op == '%')

return 3;

else

return 0;

void infixToPrefix(char* infix, char* prefix) {

char stack[100];

int top = -1;

int index = 0;

for (int i = 0; infix[i] != '\0'; ++i) {

char ch = infix[i];

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {

prefix[index++] = ch;

if (ch == '(') {

stack[++top] = ch;

if (ch == ')') {

while (top != -1 && stack[top] != '(') {


prefix[index++] = stack[top--];

--top; // Pop '('

if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') {

while (top != -1 && order(ch) <= order(stack[top])) {

prefix[index++] = stack[top--];

stack[++top] = ch;

while (top != -1) {

prefix[index++] = stack[top--];

prefix[index] = '\0';

int main() {

char infix[100];

char prefix[100];

printf("Enter Infix Expression:\n");

scanf("%s", infix);

strrev(infix);

infixToPrefix(infix, prefix);

strrev(prefix);

printf("Prefix Expression: %s\n", prefix);


return 0;

#include<stdio.h>

#include <stdbool.h>

#include <stdlib.h>

#include <string.h>

#define MAX_SIZE 100

bool isValid(char str[])

for(int i=0;i<strlen(str);++i)

if(str[i]!='(' && str[i]!=')'&&str[i]!='[' && str[i]!=']'&&str[i]!='{' &&


str[i]!='}'&&str[i]!='<'&&str[i]!='>')

return false;

return true;

bool isBalanced(char str[])

char stack[MAX_SIZE];

int top=-1;

for(int i=0;i<strlen(str);++i)

if(str[i]!='(' || str[i]!='['|| str[i]!='{'|| str[i]!='<')


{

stack[++top]=str[i];

else if(str[i]!=')' || str[i]!=']'||str[i]!='}'||str[i]!='>')

if(top==-1)

return false;

char topchar=stack[top--];

if(str[i]!=')' || str[i]!=']'||str[i]!='}'||str[i]!='>')

return false;

return top==-1;

int main()

int size;

printf("enter the size for stack:");

scanf("%d",&size);

char str[size];

printf("enter string:");

scanf("%s",str);

if(!isValid(str))

printf("invalid");

}
else if(isBalanced(str))

printf("balanced");

else

printf("not balanced:");

return 0;

#include<stdio.h>

typedef struct Node

int data;

struct Node* next;

}Node;

Node* createNode(int value)

Node* newNode=(Node*)malloc(sizeof(Node));

newNode->data=value;

newNode->next=NULL;

return newNode;

Node* insertend(Node* head,int value)

Node* newNode=createNode(value);
if(head==NULL)

return newNode;

Node* current=head;

while(current->next!=NULL)

current=current->next;

current->next=newNode;

return head;

Node* deleteNode(Node* head,int value)

if(head==NULL)

return head;

if(head->data==value)

Node* temp=head;

head=head->next;

free(temp);

return head;

Node* current=head;

while(current->next!=NULL && current->next->data!=value)

{
current=current->next;

if(current->next==NULL)

return head;

Node*temp=current->next;

current->next=current->next->next;

free(temp);

return head;

Node* middle(Node* head)

if(head==NULL)

return head;

Node* slow=head;

Node* fast=head;

while(fast!=NULL && fast->next!=NULL)

slow=slow->next;

fast=fast->next->next;

return slow;

Node* reverse(Node* head)

{
Node* current=head;

Node* prev=NULL;

Node* next=NULL;

while(current!=NULL)

next=current->next;

current->next=prev;

prev=current;

current=next;

return prev;

Node* recRev(Node* head)

if(head==NULL || head->next==NULL)

return head;

Node* rev=recRev(head->next);

head->next=NULL;

return rev;

Node* displayList(Node*head)

Node* current=head;

while(current!=NULL)

printf("%d",current->data );

current=current->next;
}

printf("\n");

int main()

Node* head=NULL;

head=insertend(head,40);

head=insertend(head,50);

head=insertend(head,90);

Node* middleNode = middle(head);

if (middleNode != NULL)

printf("%d\n", middleNode->data);

else

printf("List is empty.\n");

printf("reverse linked list:");

head=reverse(head);

head=recRev(head);

printf("Linked List after insertAtEnd: ");

displayList(head);

return 0;

#include<stdio.h>
#include <stdlib.h>

typedef struct Node

int data;

struct Node* right;

struct Node* left;

}Node;

Node* createNew(int val)

Node* newNode=(Node*)malloc(sizeof(Node));

newNode->data=val;

newNode->left=NULL;

newNode->right=NULL;

return newNode;

Node* insertTree(Node* root,int val)

Node* newNode=createNew(val);

if(root==NULL)

return newNode;

if(val<root->data)

root->left=insertTree(root->left,val);
}

else if(val>root->data)

root->right=insertTree(root->right,val);

return root;

Node* inorder(Node* root)

if(root!=NULL)

inorder(root->left);

printf("%d",root->data);

inorder(root->right);

Node* findMin(Node* root)

while(root->left!=NULL)

root=root->left;

return root;

Node* findMax(Node* root)

while(root->right!=NULL)
{

root=root->right;

return root;

Node* deleteNode(Node* root,int val)

if(root==NULL)

return root;

else if(val<root->data)

root->left=deleteNode(root->left,val);

else if(val>root->data)

root->right=deleteNode(root->right,val);

else

if(root->left==NULL)

Node* temp=root->right;

free(root);

return temp;

else if(root->right==NULL)

Node* temp=root->left;
free(root);

return temp;

Node * temp=findMin(root->right);

root->data=temp->data;

root->right=deleteNode(root->right,temp->data);

Node* search(Node* root,int val)

if(root==NULL)

return root;

if(val<root->data)

return search(root->left,val);

else if(val>root->data)

return search(root->right,val);

else

return root;

}
Node* kth(Node* root,int k,int *count)

if(root!=NULL)

Node* right=kth(root,k,count);

if(right!=NULL)

return right;

(*count)++;

if(count==k)

return root;

return kth(root->right,k,count);

return NULL;

int Lca(Node* root,int n1,int n2)

if(root==NULL)

return NULL;

if(root->data>n1&& root->data>n2)

return Lca(root->left,n1,n2);

if(root->data<n1&& root->data<n2)

return Lca(root->right,n1,n2);

return root;

}
int countMult(Node* root,int k)

if(root==NULL)

return 0;

int count=0;

if(root->data%k==0)

count++;

count+=countMult(root->left,k);

count+=countMult(root->right,k);

return count;

int isPalin(int num)

int orig=num;

int rev=0;

while(num)

rev=rev*10+num%10;

num/=10;

return orig==rev;

int countPal(Node* root)

int count=0;

Node* stack[100];

int top=-1;
while(root!=NULL||top!=-1)

while(root!=NULL)

stack[++top]=root;

root=root->left;

root=stack[top--];

if(isPalin(root->data))

printf("Node with palindrome number :%d",root->data);

count++;

root=root->right;

return count;

void freeBST(Node* root)

if(root!=NULL)

freeBST(root->left);

freeBST(root->right);

free(root);

}
int main()

Node* root = NULL;

root = insertTree(root, 50);

root = insertTree(root, 30);

root = insertTree(root, 20);

root = insertTree(root, 40);

root = insertTree(root, 70);

root = insertTree(root, 60);

root = insertTree(root, 80);

printf("In-order traversal of the BST: ");

inorder(root);

printf("\n");

int keyToDelete = 30;

root = deleteNode(root, keyToDelete);

printf("In-order traversal after deleting %d: ", keyToDelete);

inorder(root);

printf("\n");

int keyToSearch;

printf("Enter a value to search: ");

scanf("%d", &keyToSearch);

Node* srch=search(root,keyToSearch);

if(srch!=NULL)

printf("FOUND found %d",keyToSearch);

else
{

printf(" NOT FOUND %d",keyToSearch);

Node* max=findMax(root);

int k;

printf("Enter the value of k for kth smallest and kth largest: ");

scanf("%d", &k);

int count = 0;

printf("%d",max->data);

Node* k1 = kth(root, k, &count);

if (k1 != NULL)

printf("Kth Largest element at position %d: %d\n", k, k1->data);

else

printf("BST doesn't have %d elements\n", k);

freeBST(root);

return 0;

You might also like