labrt
labrt
h>
#include<stdlib.h>
#define MAX 3
int deque[MAX],item,front=-1,rear=-1;
void inser ront(int item){
if(front==-1 && rear==-1){
front=rear=0;
deque[front]=item;
}
else if(front==0){
front=MAX-1;
deque[front]=item;
}
else
deque[--front]=item;
}
void insertrear(int item){
if(front==-1 && rear==-1){
front=rear=0;
deque[rear]=item;
}
else{
rear=(rear+1)%MAX;
deque[rear]=item;
}
}
void deletefront(){
item=deque[front];
if(front==rear){
front=rear=-1;
}
else
front=(front+1)%MAX;
}
void deleterear(){
item=deque[rear];
if(front==rear){
front=rear=-1;
}
else if(rear==0)
rear=MAX-1;
else
item=deque[rear--];
}
void display(){
int i;
for (i=front;i!=rear;i=(i+1)%MAX)
prin ("%d \t",deque[i]);
prin ("%d \t",deque[i]);
}
void main(){
int choice;
while (1){
prin ("\nDeque Opera ons \n 1.Insert from front \t 2.Insert from rear \t 3.Delete from front \t 4.Delete from rear \t
5.Display \t 6.exit \n Enter the choice:");
scanf("%d",&choice);
switch (choice){
case 1:if((rear+1)%MAX==front){
prin ("Queue is full \n");
break;
}
prin ("Enter the element to be inserted \n");
scanf("%d",&item);
inser ront(item);
prin ("Element %d is successfully inserted in the deque \n",deque[front]);
break;
case 2:if((rear+1)%MAX==front){
prin ("Queue is full \n");
break;
}
prin ("Enter the element to be inserted \n");
scanf("%d",&item);
insertrear(item);
prin ("Element %d is successfully inserted in the deque \n",deque[rear]);
break;
case 3:if(front==-1 && rear==-1){
prin ("Queue is empty \n");
break;
}
deletefront();
prin ("Element %d is successfully deleted from the deque \n",item);
break;
case 4:if(front==-1 && rear==-1){
prin ("Queue is empty \n");
break;
}
deleterear();
prin ("Element %d is successfully deleted from the deque \n",item);
break;
case 5:if(front==-1 && rear==-1){
prin ("Queue is empty \n");
break;
}
display();
break;
case 6:exit(1);
break;
default:
prin ("Enter a valid op on \n");
}
}
}
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define MAX 100
int stack[MAX],top=-1;
void push(char item){
if(top>=MAX-1)
prin ("Stack is full \n");
else
stack[++top]=item;
}
int pop(){
char item;
if(top==-1)
prin ("Stack is empty \n");
else{
item=stack[top--];return item;}
}
int isp(char item){
if(item=='+'||item=='-')
return 1;
if(item=='*'||item=='/')
return 3;
if(item=='^')
return 6;
if(item==')')
return 0;
if(item=='(')
return 7;
}
int icp(char item){
if(item=='+'||item=='-')
return 2;
if(item=='*'||item=='/')
return 4;
if(item=='^')
return 5;
if(item==')')
return 0;
}
void prefix(char *input,char *output){
int i=0,j=0;
push(')');
strcat(input,"(");
while(input[i]!='\0'){
char ch=input[i++];
if(isalnum(ch))
output[j++]=ch;
else if(ch==')')
push(ch);
else if(ch=='('){
while(stack[top]!=')')
output[j++]=pop();
pop();
}
else if(isp(stack[top])>=icp(ch)){
while(isp(stack[top])>=icp(ch))
output[j++]=pop();
push(ch);
}
else
push(ch);
}
output[j]='\0';
strrev(output);
prin ("The prefix expression is %s \n",output);
}
void main(){
prin ("Enter the infix expression: ");
char exp[MAX],pre[MAX];
scanf("%s",exp);
strrev(exp);
prefix(exp,pre);
}
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *link;
};
struct node *head=NULL;
void insertbeg(){
struct node *temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL){
prin ("Memory not avaliable \n");return;}
else{
prin ("Enter the value to be inserted:");
scanf("%d",&temp->data);
temp->link=NULL;
temp->link=head;
head=temp;
prin ("The element %d is successfully inserted at the beginning\n",temp->data);
}
}
void insertend(){
struct node *ptr,*temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL){
prin ("Memory not avaliable \n");return;}
else{
prin ("Enter the value to be inserted:");
scanf("%d",&temp->data);
temp->link=NULL;
if(head==NULL)
head=temp;
else{
ptr=head;
while(ptr->link!=NULL)
ptr=ptr->link;
ptr->link=temp;
}
prin ("The element %d is successfully inserted at the end\n",temp->data);
}
}
void insertpos(){
int key;
struct node *temp=(struct node *)malloc(sizeof(struct node)),*ptr;
if(temp==NULL){
prin ("Memory not avaliable \n");return;}
else{
prin ("Enter the value to be inserted:");
scanf("%d",&temp->data);
prin ("Enter the key of the previous node");
scanf("%d",&key);
ptr=head;
while(ptr->data!=key && ptr->link!=NULL)
ptr=ptr->link;
if(ptr->link==NULL)
prin ("Inser on not posible\n");return;
temp->link=ptr->link;
ptr->link=temp;
prin ("The element %d is successfully inserted\n",temp->data);
}
}
void deletebeg(){
struct node *ptr=head,*ptr1;
if(ptr==NULL){
prin ("Linked list is empty \n");return;}
else{
ptr1=ptr->link;
head=ptr1;
prin ("The deleted element is %d\n",ptr->data);
free(ptr);
}
}
void deleteend(){
struct node *ptr=head,*ptr1;
if(ptr==NULL){
prin ("Linked list is empty \n");return;}
else{
while(ptr->link!=NULL){
ptr1=ptr;
ptr=ptr->link;
}
ptr1->link=NULL;
prin ("The deleted element is %d\n",ptr->data);
free(ptr);
}
}
void deletepos(){
int key;
struct node *ptr=head,*ptr1;
prin ("Enter the key of the node to be deleted:");
scanf("%d",&key);
while(ptr!=NULL){
if(ptr->data!=key){
ptr1=ptr;
ptr=ptr->link;
}
else{
ptr1->link=ptr->link;
prin ("The deleted element is %d\n",ptr->data);
free(ptr);
break;
}
}
if(ptr==NULL)
prin ("Dele on not posible\n");
}
void display(){
struct node *ptr;
if(head==NULL){
prin ("Linked list is empty \n");return;}
else{
ptr=head;
prin ("The elements are \n");
while(ptr!=NULL){
prin ("%d \t",ptr->data);
ptr=ptr->link;
}
prin ("\n");
}
}
void main(){
while(1){
int choice;
prin ("Linked list opera ons \n1.Insert at beginning \t2.Insert at end \t3.Insert at a specified posi on \t4.Delete from
beginning \t5.Delete from end \t6.Delete fron a specified posi on \t7.Display \t8.Exit \n");
prin ("Enter the choice:");
scanf("%d",&choice);
switch(choice){
case 1:insertbeg();
break;
case 2:insertend();
break;
case 3:insertpos();
break;
case 4:deletebeg();
break;
case 5:deleteend();
break;
case 6:deletepos();
break;
case 7:display();
break;
case 8:exit(1);
break;
default:prin ("Invalid choice \n");
}
}
}
#include<stdio.h>
#include<stdlib.h>
struct node{
int coeff;
int exp;
struct node *link;
};
struct node *insert(struct node *start,int coeff,int exp){
struct node *ptr,*temp=(struct node *)malloc(sizeof(struct node));
temp->coeff=coeff;
temp->exp=exp;
if(start==NULL){
temp->link=start;
start=temp;
}
else{
ptr=start;
while(ptr->link!=NULL)
ptr=ptr->link;
temp->link=ptr->link;
ptr->link=temp;
}return start;
}
struct node *read(struct node *start){
int i,n,exp,coeff;
prin ("\nEnter the number of terms:");
scanf("%d",&n);
for(i=1;i<=n;i++){
prin ("Enter coefficient:");
scanf("%d",&coeff);
prin ("Enter exponent:");
scanf("%d",&exp);
start=insert(start,coeff,exp);
}return start;
}
void display(struct node *ptr){
if(ptr==NULL){
prin ("\nEmpty\n");return;}
while(ptr!=NULL){
prin ("%dx^%d",ptr->coeff,ptr->exp);
ptr=ptr->link;
if(ptr!=NULL)
prin ("+");
else
prin ("\n");
}
}
void add(struct node *p1,struct node*p2){
struct node*start3=NULL;
while(p1!=NULL && p2!=NULL){
if(p1->exp > p2->exp){
start3=insert(start3,p1->coeff,p1->exp);
p1=p1->link;
}
else if(p2->exp > p1->exp){
start3=insert(start3,p2->coeff,p2->exp);
p2=p2->link;
}
else if(p1->exp == p2->exp){
start3=insert(start3,p1->coeff+p2->coeff,p1->exp);
p1=p1->link;
p2=p2->link;
}
}
while(p1!=NULL){
start3=insert(start3,p1->coeff,p1->exp);
p1=p1->link;
}
while(p2!=NULL){
start3=insert(start3,p2->coeff,p2->exp);
p2=p2->link;
}
prin ("Sum of polynomials is:");
display(start3);
}
void reduce(struct node *head){
if(head==NULL) return;
struct node* ptr=head,*ptr2,*prev;
while(ptr!=NULL){
ptr2=ptr->link;
prev=ptr;
while(ptr2!=NULL){
if(ptr->exp==ptr2->exp){
ptr->coeff+=ptr2->coeff;
prev->link=ptr2->link;
free(ptr2);
ptr2=prev->link;
}
else{
prev=ptr2;
ptr2=ptr2->link;
}
}
ptr=ptr->link;
}
}
void mul ply(struct node *p1,struct node *p2){
struct node *start3=NULL,*p2beg=p2;
while(p1!=NULL){
p2=p2beg;
while(p2!=NULL){
start3=insert(start3,p1->coeff*p2->coeff,p1->exp+p2->exp);
p2=p2->link;
}
p1=p1->link;
}
reduce(start3);
prin ("Product of polynomials is:");
display(start3);
}
void main(){
struct node *start1=NULL,*start2=NULL;
prin ("Enter first polynomial");
start1=read(start1);
prin ("Enter the second polynomial");
start2=read(start2);
add(start1,start2);
mul ply(start1,start2);
}
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *le ,*right;
};
struct Node *root=NULL;
struct Node *insert(struct Node *node,int key){
if(node==NULL){
struct Node *temp=(struct Node *)malloc(sizeof(struct Node *));
temp->le =temp->right=NULL;
temp->data=key;
if(root==NULL)
root=temp;
else{
node=temp;return temp;}
prin ("The element %d is successfully inserted\n",key);
}
else{
if(key<node->data)
node->le =insert(node->le ,key);
else if(key>node->data)
node->right=insert(node->right,key);
}return node;
}
void inorder(struct Node *node){
if(node==NULL) return;
inorder(node->le );
prin ("%d\t",node->data);
inorder(node->right);
}
void preorder(struct Node *node){
if(node==NULL) return;
prin ("%d\t",node->data);
preorder(node->le );
preorder(node->right);
}
void postorder(struct Node *node){
if(node==NULL) return;
postorder(node->le );
postorder(node->right);
prin ("%d\t",node->data);
}
int inordersuccessor(struct Node *node){
struct Node *current=node;
while(current!=NULL && current->le !=NULL)
current=current->le ;return current->data;
}
struct Node *delete(struct Node *node,int key){
if(node==NULL) return NULL;
if(key<node->data)
node->le =delete(node->le ,key);
else if(key>node->data)
node->right=delete(node->right,key);
else{
if(node->le ==NULL && node->right==NULL){
prin ("The element %d is deleted successfully\n",node->data);
free(node);return NULL;
}
else if(node->le ==NULL){
struct Node *temp=node->right;
prin ("The element %d is deleted successfully\n",node->data);
free(node);return temp;
}
else if(node->right==NULL){
struct Node *temp=node->le ;
prin ("The element %d is deleted successfully\n",node->data);
free(node);return temp;
}
else{
int rightmin=inordersuccessor(node->right);
node->data=rightmin;
node->right=delete(node->right,rightmin);
}
}return node;
}
void main(){
int choice,data,val;
while(1){
prin ("\nBinary Search Tree Opera on \n1.Insert \t2.Inorder Traversal \t3.Preorder Traversal \t4.Postorder Traversal
\t5.Delete \n6.Exit \nEnter the choice:");
scanf("%d",&choice);
switch(choice){
case 1:prin ("Enter the data to be inserted:");
scanf("%d",&data);
insert(root,data);
break;
case 2:if(root==NULL){
prin ("Tree is empty\n");
break;}
inorder(root);
break;
case 3:if(root==NULL){
prin ("Tree is empty\n");
break;}
preorder(root);
break;
case 4:if(root==NULL){
prin ("Tree is empty\n");
break;}
postorder(root);
break;
case 5:if(root==NULL){
prin ("Tree is empty\n");
break;}
prin ("Enter the data to delete:");
scanf("%d",&val);
delete(root,val);
break;
case 6:exit(1);
break;
default:prin ("Invalid choice \n");
}
}
}
#include<stdio.h>
void read(int a[],int n){
prin ("Enter the elements:");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display(int a[],int n){
prin ("The elements are\n");
for(int i=0;i<n;i++)
prin ("%d\t",a[i]);
}
void swap(int* a,int* b){
int temp=*a;
*a=*b;
*b=temp;
}
void inser onsort(int a[],int n){
prin ("A er inser on sor ng\n");
int i=1,j;
while(i<n){
j=i;
while(a[j]<a[j-1] && j>0){
swap(&a[j],&a[j-1]);
j--;
}i++;
}
}
int par on(int a[],int lb,int ub){
int pivot=a[lb],down=lb,up=ub;
while(down<up){
while(pivot>=a[down] && down<=up)
down++;
while(pivot<a[up])
up--;
if(down<=up)
swap(&a[down],&a[up]);
}
swap(&a[lb],&a[up]);return up;
}
void quicksort(int a[],int lb,int ub){
if(lb<ub){
int loc=par on(a,lb,ub);
quicksort(a,lb,loc-1);
quicksort(a,loc+1,ub);
}
}
void main(){
int a[50],n,choice;
prin ("Enter the no of elements to sort:");
scanf("%d",&n);
read(a,n);
prin ("Elements without sor ng\n");
display(a,n);
prin ("\nWhich sor ng method \n1.Inser on sort \t2.Quicksort \nEnter the choice:");
scanf("%d",&choice);
if(choice==1)
inser onsort(a,n);
else if(choice==2){
prin ("A er quicksor ng\n");
quicksort(a,0,n-1);
}
else
prin ("Inavlid choice\n");
display(a,n);
}
#include<stdio.h>
void read(int a[],int n){
prin ("Enter the elements:");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display(int a[],int n){
prin ("The elements are\n");
for(int i=0;i<n;i++)
prin ("%d\t",a[i]);
}
void merge(int a[],int start,int mid,int end){
int i=start,j=mid+1,k=start,temp[50];
while(i<=mid && j<=end){
if(a[i]<=a[j]){
temp[k]=a[i];
i++;
k++;
}
else{
temp[k]=a[j];
j++;
k++;
}
}
while(i<=mid){
temp[k]=a[i];
i++;
k++;
}
while(j<=end){
temp[k]=a[j];
j++;
k++;
}
k=start;
while(k<=end){
a[k]=temp[k];
k++;
}
}
void mergesort(int a[],int start,int end){
if(start<end){
int mid=(start+end)/2;
mergesort(a,start,mid);
mergesort(a,mid+1,end);
merge(a,start,mid,end);
}
}
void main(){
int a[50],n;
prin ("Enter the no of elements to sort:");
scanf("%d",&n);
read(a,n);
prin ("Elements without sor ng\n");
display(a,n);
prin ("\nA er mergingsor ng\n");
mergesort(a,0,n-1);
display(a,n);
}
#include<stdio.h>
void read(int a[],int n){
prin ("Enter the elements:");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display(int a[],int n){
prin ("The elements are\n");
for(int i=0;i<n;i++)
prin ("%d\t",a[i]);
}
void swap(int* a,int* b){
int temp=*a;
*a=*b;
*b=temp;
}
void heapify(int a[],int n,int i){
int large=i,le =2*i+1,right=2*i+2;
if(le <n && a[le ]>a[large])
large=le ;
if(right<n && a[right]>a[large])
large=right;
if(large!=i){
swap(&a[i],&a[large]);
heapify(a,n,large);
}
}
void heapsort(int a[],int n){
prin ("\nA er heap sor ng\n");
for(int i=n/2-1;i>=0;i--)
heapify(a,n,i);
for(int i=n-1;i>=0;i--){
swap(&a[0],&a[i]);
heapify(a,i,0);
}
}
void main(){
int a[50],n,choice;
prin ("Enter the no of elements to sort:");
scanf("%d",&n);
read(a,n);
prin ("Elements without sor ng\n");
display(a,n);
heapsort(a,n);
display(a,n);
}