Sunteți pe pagina 1din 47

/* Program to implement STACKS using arrays */

#include<stdio.h>
#include<conio.h>
#define max 10
int x,a[10],top=-1;
void push();
void pop();
void display();
void main()
{
int ch;
clrscr();
while(1)
{
printf("\nchoices are:\n");
printf("1.push\n2.pop\n3.display\n4.exit\n");
printf("\nenter the choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: if(top==(max-1))
{
printf("\nstack overflow");
}
else
push();
break;
case 2: if(top==-1)
{
printf("stack underflow");
}
else
pop();
break;
case 3: printf("\ncontents of stack are");
display();
break;
case 4:exit(0);
}
}
getch();
}
void push()
{
printf("\nenter element to be inserted: ");
scanf("%d",&x);
top++;
a[top]=x;
}
void pop()
{
int y;
y=a[top];
top--;
printf("\ndeleted element is: %d",y);
}
void display()
{
int i;
for(i=0;i<=top;i++)
printf("%3d",a[i]);
}

program to convert INFIX expression to POSTFIX expression using STACKS */

#include<stdio.h>
char a[20], in[20],post[20];
int top=-1;
void postfix(char *);
void push(char c);
char pop();
int precedence(char c);
main()
{
printf("enter the infix expression: ");
scanf("%s",in);
postfix(in);
printf("\nconverted postfix expression: %s",post);
}
void postfix(char *in)
{
int i=0,j=0;
while(in[i]!='\0')
{
if(in[i]>='a' && in[i]<='z')
{
post[j]=in[i];
j++;
}
else if(in[i]=='(')
{
push('(');
}
else if(in[i]==')')
{
while(a[top]!='(')
{
post[j]=pop();
j++;
}
pop();
}
else
{
while((precedence(a[top])>=precedence(in[i])) && (top!=-1))
{
post[j]=pop();
j++;
}
push(in[i]);
}
i++;
}
while(top!=-1)
{
post[j]=pop();
j++;
}
post[j]='\0';
}
void push(char c)
{
top++;
a[top]=c;
}
char pop()
{
char c;
c=a[top];
top--;
return c;
}
int precedence(char c)
{
switch(c)
{
case '$': return 4;
case '*':
case '/': return 3;
case '+':
case '-': return 2;
default : return 0;
}
}

OUTPUT:

enter the infix expression: a*b/(c-d)+e*(f-g)


converted postfix expression: ab*cd-/efg-*+

/* Program to convert INFIX expression to PREFIX expression using STACKS */

#include<stdio.h>
char ch,a[30],in[30],pre[30];
int top=-1;
void push(char);
int precedence(char);
char pop();
void prefix(char *);
int i=0,j=0,k;
void main()
{
printf("enter the infix expression: ");
scanf("%s",in);
prefix(in);
}
void prefix(char *in)
{
k=strlen(in)-1;
while(k>=0)
{
if(in[k]>='a'&&in[k]<='z')
{
pre[j]=in[k];
j++;
}
else if(in[k]==')')
{
push(in[k]);
}
else if(in[k]=='(')
{
while(a[top]!=')')
{
pre[j++]=pop();
}
ch=pop();
}
else
{
while((precedence(in[k])<precedence(a[top]))&&(top!=-1))
{
pre[j++]=pop();
}
push(in[k]);
}
k--;
}
while(top!=-1)
pre[j++]=pop();

printf("\nConverted prefix expression: ");


for(i=j-1;i>=0;i--)
printf("%c",pre[i]);
getch();
}
void push(char c)
{
top++;
a[top]=c;
}
char pop()
{
char c;
c=a[top];
top--;
return;
}
int precedence(char p)
{
switch(p)
{
case '$': return 5;
case '*':
case '/': return 4;
case '+':
case '-': return 3;
default : return 0;
}
}

OUTPUT:

enter the infix expression : a*b/(c-d)+e*(f-g)


converted prefix expression: +/*ab-cd*e-fg
/* Program to evaluate the POSTFIX expression using STACKS */

#include<stdio.h>
#include<math.h>
void push();
int pop();
int s[100];
int top=-1;
void main()
{
char post[20];
printf("enter the postfix expression: ");
gets(post);
eva(post);
printf("\nvalue of postfix expression: %d",s[top]);
}
eva(char post[])
{
int i,x,op1,op2;
for(i=0;post[i]!='\0';i++)
{
if(post[i]>='a'&&post[i]<='z')
{
printf("\nenter the value for %c: ",post[i]);
scanf("%d",&x);
push(x);
}
else
{
op2=pop();
op1=pop();
switch(post[i])
{
case '$': x=pow(op1,op2);
break;
case '+': x=op1+op2;
break;
case '-': x=op1-op2;
break;
case '*': x=op1*op2;
break;
case '/': x=op1/op2;
break;
case '%': x=op1%op2;
break;
}
push(x);
}
}
}
void push(int x)
{
s[++top]=x;
}
int pop()
{
return(s[top--]);
}

OUTPUT:

enter the postfix expression: abc*de$-f/+


enter the value for a: 1
enter the value for b: 2
enter the value for c: 3
enter the value for d: 4
enter the value for e: 5
enter the value for f: 6
value of postfix expression: -168

/* Program to evaluate the PREFIX expression using STACKS */

#include<stdio.h>
#include<math.h>
#include<string.h>
#define max 100
int pop(); void push();
int stack[max],top=-1;
void main()
{
char prefix[100];
printf("enter the prefix expression: ");
gets(prefix);
eva(prefix);
printf("\nvalue of prefix expression: %d",stack[top]);
}
eva(char prefix[])
{
int l,i,k,op1,op2;
l=strlen(prefix)-1;
for(i=l;prefix[i]>=0;i--)
{
if(prefix[i]>='a'&&prefix[i]<='z')
{
printf("\nenter the value for %c: ",prefix[i]);
scanf("%d",&k);
push(k);
}
else
{
op1=pop();
op2=pop();
switch(prefix[i])
{
case '$': k=pow(op1,op2);
break;
case '+': k=op1+op2;
break;
case '-': k=op1-op2;
break;
case '*': k=op1*op2;
break;
case '/': k=op1/op2;
break;
case '%': k=op1%op2; break;
}
push(k);
}
}
}
void push(int k)
{
stack[++top]=k;
}
int pop()
{
return(stack[top--]);
}

OUTPUT:

enter the prefix expression: +/*ab-cd*e-fg


enter the value for g: 7
enter the value for f: 6
enter the value for e: 5
enter the value for d: 4
enter the value for c: 3
enter the value for b: 2
enter the value for a: 1
value of prefix expression: -7
/* Program to implement QUEUES using arrays */

#include<stdio.h>
#define max 10
void insert();
void del();
void display();
int que[max],rear=-1,front=0;
int x;
void main()
{
int ch;
clrscr();
while(1)
{
printf("\nchoices are:");
printf("\n1.Insert\n2.Delete\n3.Display\n4.exit\n");
printf("\nenter ur choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: if(rear==max-1)
printf("Queue overflow");
else
insert();
break;
case 2: if(front>rear)
printf("Queue underflow");
else
del();
break;
case 3: if(front>rear)
printf("Queue underflow");
else
display();
break;
case 4: exit(0);
}
}
}
void insert()
{
printf("enter an element: ");
scanf("%d",&x);
rear++;
que[rear]=x;
}

void del()
{
int y;
y=que[front];
front++;
printf("deleted element is %d",y);
}
void display()
{
int i;
printf("\ncontents of queues are: ");
for(i=front;i<=rear;i++)
printf("%d ",que[i]);
}

/* Program to implement CIRCULAR QUEUES using arrays */

#include<stdio.h>
void insert(); void del(); void display();
int rear=-1,front=-1,cq[10],i,ch,c,max,item;
main()
{
printf("enter size of cq: ");
scanf("%d",&max);
while(1)
{
printf("\nchoices are:\n");
printf("\n1.insert\n2.pop\n3.display\n4.exit");
printf("\nenter ur choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(0);
break;
default:printf("invalid choice");
}
}
}
void insert()
{
if((front==0&&rear==max-1)||(front==rear+1))
printf("\ncq overflow\n");
else
{
if(front==-1)
{
front=0;
rear=0;
}
else
{
rear=rear+1;
rear=rear%max;
}
printf("\nenter a element: ");
scanf("%d",&item);
cq[rear]=item;
}
}
void del()
{
if(front==-1)
printf("\ncq underflow\n");
else
{
item=cq[front];
printf("\n%d is deleted\n",item);
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=front+1;
front=front%max;
}
}
}
void display()
{
if(front==-1)
printf("\ncq underflow\n");
else
{
printf("contents of circular queues: ");
if(front<=rear)
{
for(i=front;i<=rear;i++)
printf("%4d",cq[i]);
}
else
{
for(i=front;i<max;i++)
printf("%4d",cq[i]);
for(i=0;i<=rear;i++)
printf("%4d",cq[i]);
}
}
}

/* Program to implement DOUBLE ENDED QUEUES (DEQueues) using arrays */

#include<stdio.h>
int rear=-1,front=-1,dq[10],size=10,item,i;
void main()
{
int ch;
while(1)
{
printf("\nchoices are:\n");
printf("\n1.LInsert\n2.RInsert\n3.LDelete");
printf("\n4.RDelete\n5.Display\n6.exit");
printf("\nenter ur choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: LInsert();
break;
case 2: RInsert();
break;
case 3: LDelete();
break;
case 4: RDelete();
break;
case 5: Display(); break;
case 6: exit(0);
}
}
}
LInsert()
{
printf("enter an element: ");
scanf("%d",&item);
if(rear>=size-1)
printf("\ndequeue overflow\n");
else
{
if(front==-1)
{
front=0;
rear=0;
dq[rear]=item;
return;
}
for(i=rear;i>=front;i--)
dq[i+1]=dq[i];
dq[front]=item;
rear++;
}
}
RInsert()
{
printf("enter an element: ");
scanf("%d",&item);
if(rear>=size-1)
{
printf("\ndequeue overflow\n");
return;
}
else
{
if(front==-1)
{
front=0;
}
rear++;
dq[rear]=item;
return;
}
}
LDelete()
{
if((rear==-1)||(front>rear))
{
printf("\nDequeue underflow\n");
return;
}
item=dq[front];
front++;
printf("\ndeleted element is %d",item);
}
RDelete()
{
if((rear==-1)||(front>rear))
{
printf("\nDequeue underflow\n");
return;
}
item=dq[rear];
--rear;
printf("deleted element is %d",item);
}
Display()
{
printf("\ncontents of Dequeue are: ");
for(i=front;i<=rear;i++)
printf("%d ",dq[i]);
}
/* Program to implement SINGLE LINKED LISTS using dynamic memory
allocation(POINTERS) */

#include<stdio.h>
#include<stdlib.h>
void insertbeg();
void insertend();
void insertpos();
void delbeg();
void delend();
void delpos();
void display();
struct node
{
int info;
struct node *link;
}*first=NULL;
struct node *create();
int item,key;
main()
{
int choice;
while(1)
{
printf("\nchoices are:\n");
printf("\n1.Insertbeg\n2.Insertend\n3.Insertpos\n4.delbeg");
printf("\n5.delend\n6.delpos\n7.display\n8.exit\n");
printf("Enter U'r choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insertbeg(); break;
case 2: insertend(); break;
case 3: insertpos(); break;
case 4: delbeg(); break;
case 5: delend(); break;
case 6: delpos(); break;
case 7: display(); break;
case 8: exit(1);
default: printf("INVALID CHOICE TRY AGAIN\n");
}
}
}
struct node *create()
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
return(new);
}

void insertbeg()
{
struct node *new;
new=create();
printf("Enter element to be inserted: ");
scanf("%d",&item);
if(first==NULL)
{
new->info=item;
new->link=NULL;
first=new;
}
else
{
new->info=item;
new->link=first;
first=new;
}
}
void insertend()
{
struct node *new,*temp;
new=create();
printf("Enter element to be inserted: ");
scanf("%d",&item);
if(first==NULL)
{
new->info=item;
new->link=NULL;
first=new;
}
else
{
temp=first;
while(temp->link!=NULL)
temp=temp->link;
new->info=item;
new->link=NULL;
temp->link=new;
}
}
void insertpos()
{
struct node *new,*prev,*temp;
new=create();
printf("Enter element to be inserted: ");
scanf("%d",&item);
printf("ENter key position: ");
scanf("%d",&key);
temp=first;
while(temp->info!=key&&temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
if(temp->info==first->info&&temp->info==key)
{
new->info=item;
new->link=temp;
first=new;
}
else
{
new->info=item;
new->link=temp;
prev->link=new;
}
}
void delbeg()
{
struct node *temp;
if(first==NULL)
{
printf("LIST IS EMPTY\n");
return;
}
else
{
temp=first;
first=temp->link;
free(temp);
}
}
void delend()
{
struct node *temp,*prev;
if(first==NULL)
{
printf("LIST IS EMPTY\n");
return;
}
else
{
temp=first;
while(temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
prev->link=NULL;
free(temp);
}
}
void delpos()
{
int key;
struct node *temp,*prev;
if(first==NULL)
{
printf("LIST IS EMPTY\n");
return;
}
else
{
temp=first;
printf("Enter the KEY element which is to be deleted: ");
scanf("%d",&key);
while(temp->info!=key&&temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
if(temp->info==key)
{
prev->link=temp->link;
free(temp);
}
else
printf("key element not found in the list\n");
}
}
void display()
{
struct node *temp;
temp=first;
if(temp==NULL)
{
printf("LIST IS EMPTY\n");
return;
}
else
{
printf("Elements in Linked Lists: ");
while(temp!=NULL)
{
printf("%d->",temp->info);
temp=temp->link;
}
}
}

/* Program to implement DOUBLE LINKED LISTS using dynamic memory


allocation(POINTERS) */

#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *lptr;
struct node *rptr;
}*first=NULL;
struct node *create();
void insertbeg();
void insertend();
void insertpos();
void delbeg();
void delend();
void delpos();
void display();
int item,key;
main()
{
int choice;
while(1)
{
printf("choices are:\n");
printf("1.Insertbeg\n2.Insertend\n3.Insertpos\n4.delbeg");
printf("\n5.delend\n6.delpos\n7.display\n8.exit\n");
printf("Enter ur choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insertbeg(); break;
case 2: insertend(); break;
case 3: insertSpos(); break;
case 4: delbeg(); break;
case 5: delend(); break;
case 6: delpos(); break;
case 7: display(); break;
case 8: exit(1);
default: printf("INVALID CHOICE TRY AGAIN\n");
}
}
}
struct node *create()
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
return(new);
}

void insertbeg()
{
struct node *new;
new=create();
printf("Enter element to be inserted: ");
scanf("%d",&item);
if(first==NULL)
{
new->info=item;
new->rptr=NULL;
new->lptr=NULL;
first=new;
}
else
{
new->info=item;
new->rptr=first;
new->lptr=NULL;
first->lptr=new;
first=new;
}
}
void insertend()
{
struct node *new,*temp;
new=create();
printf("Enter element to be inserted: ");
scanf("%d",&item);
if(first==NULL)
{
new->info=item;
new->rptr=NULL;
new->lptr=NULL;
first=new;
}
else
{
temp=first;
while(temp->rptr!=NULL)
temp=temp->rptr;
new->info=item;
new->rptr=NULL;
new->lptr=temp;
temp->rptr=new;
}
}

void insertpos()
{
struct node *new,*temp;
new=create();
printf("Enter element to be inserted: ");
scanf("%d",&item);
printf("ENter key position: ");
scanf("%d",&key);
if(first==NULL)
{
new->info=item;
new->rptr=NULL;
new->lptr=NULL;
first=new;
}
else
{
temp=first;
while(temp->info!=key&&temp->rptr!=NULL)
{
temp=temp->rptr;
}
if(temp->info==first->info&&temp->info==key)
insertbeg();
else if(temp->rptr!=NULL||temp->rptr==NULL)
{
new->info=item;
new->lptr=temp->lptr;
new->rptr=temp;
temp->lptr->rptr=new;
temp->lptr=new;
}
else
{
printf("KEY NOT FOUND IN THE DOUBLY LINKED LIST\n");
return;
}
}
}
void delbeg()
{
struct node *temp;
if(first==NULL)
{
printf("DOUBLY LINKED LIST IS EMPTY\n");
return;
}
else
{
temp=first;
first=temp->rptr;
free(temp);
}
}
void delend()
{
struct node *temp;
if(first==NULL)
{
printf("DOUBLY LINKED LIST IS EMPTY\n");
return;
}
else
{
temp=first;
while(temp->rptr!=NULL)
{
temp=temp->rptr;
}
if(temp->rptr==temp->lptr)
{
first=temp->rptr;
free(temp);
}
else
{
temp->lptr->rptr=temp->rptr;
free(temp);
}
}
}
void delpos()
{
int key;
struct node *temp;
if(first==NULL)
{
printf("DOUBLY LINKED LIST IS EMPTY\n");
return;
}
else
{
temp=first;
printf("Enter the KEY element which is to be deleted: ");
scanf("%d",&key);
while(temp->info!=key&&temp->rptr!=NULL)
{
temp=temp->rptr;
}

if(temp->info==first->info&&temp->info==key)
delbeg();
else if(temp->info==key&&temp->rptr==NULL)
delend();
else if(temp->info==key&&temp->rptr!=NULL)
{
temp->lptr->rptr=temp->rptr;
temp->rptr->lptr=temp->lptr;
free(temp);
}
else
printf("KEY NOT FOUND IN THE DOUBLY LINKED LIST\n");
}
}
void display()
{
struct node *temp;
temp=first;
if(temp==NULL)
{
printf("LIST IS EMPTY\n");
return;
}
printf("Contents in DLL: ");
while(temp!=NULL)
{
printf("%d->",temp->info);
temp=temp->rptr;
}
}
/* Program to implement STACKS using Single Linked Lists */

#include<stdio.h>
#include<stdlib.h>
void push();
void pop();
void display();
int item;
struct node
{
int info;
struct node *link;
};
struct node* create(),*top=NULL,*first;
main()
{
int choice;
clrscr();
while(1)
{
printf("\nchoices are:\n");
printf("1.push\n");
printf("2.pop\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter the choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default:printf("invalid choice\n");
}
}
}
struct node *create()
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
return new;
}

void push()
{
struct node *new,*temp;
new=create();
printf("enter element: ");
scanf("%d",&item);
if(top==NULL)
{
new->info=item;
new->link=NULL;
top=new;
first=new;
}
else
{
temp=first;
while(temp!=top)
{
temp=temp->link;
}
new->info=item;
new->link=NULL;
temp->link=new;
top=new;
}
}
void pop()
{
struct node *temp;
if(top==NULL)
{
printf("stack is empty\n");
return;
}
else
{
temp=first;
if(temp->link!=NULL)
{
while(temp->link!=top)
{
temp=temp->link;
}
temp->link=NULL;
free(top);
top=temp;
}

else
{
free(temp);
top=NULL;
}
}
}
void display()
{
struct node *temp;
if(top==NULL)
{
printf("stack is empty\n");
return;
}
else
{
temp=first;
printf("Elements of stack: ");
do
{
printf("%d->",temp->info);
temp=temp->link;
}while(temp!=NULL);
}
}
/* Program to implement QUEUES using Single Linked Lists */

#include<stdio.h>
#include<stdlib.h>
void insert();
void del();
void display();
int item;
struct node
{
int info;
struct node *link;
};
struct node *create(),*top=NULL,*front,*rear;
main()
{
int choice;
clrscr();
while(1)
{
printf("\nchoices are:\n");
printf("1.insert\n");
printf("2.delete\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter the choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(1);
break;
default:printf("invalid choice\n");
}
}
}
struct node *create()
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
return new;
}

void insert()
{
struct node *new;
new=create();
printf("enter element into queue: ");
scanf("%d",&item);
if(rear==NULL)
{
new->info=item;
new->link=NULL;
front=new;
rear=new;
}
else
{
new->info=item;
new->link=NULL;
rear->link=new;
rear=new;
}
}
void del()
{
struct node *temp;
if(front==NULL)
{
printf("queue is empty\n");
return;
}
else
{
temp=front;
front=front->link;
free(temp);

}
}
void display()
{
struct node *temp;
if(front==NULL)
{
printf("stack is empty\n");
return;
}
else
{
temp=front;
printf("Elements in queue are: ");

do
{
printf("%d->",temp->info);
temp=temp->link;
}while(temp!=NULL);
}
}

/* Program to implement Binary Search Trees using Linked Lists */

#include<stdio.h>
#include<stdlib.h>
struct tree
{
int info;
struct tree *rchild,*lchild;
};
struct tree *new1=NULL,*root=NULL;
struct tree *create(struct tree *);
struct tree *insert(struct tree *,int);
struct tree *delete1(struct tree *);
void traverse(struct tree *);
void preorder(struct tree *);
void postorder(struct tree *);
void inorder(struct tree *);
void main()
{
int ch,item;
system("clear");
while(1)
{
printf(“\nchoices are:\n”);
printf("\n1.CREATE TREE\n2.INSERT\n3.DELETE\n");
printf("4.TRAVERSE\n5.EXIT");
printf("\nenter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: root=create(root);
break;
case 2: printf("Enter element :");
scanf("%d", &item);
root=insert(root,item);
break;
case 3: root=delete1(root);
break;
case 4: traverse(root);
break;
case 5: exit(0);
break;
default:printf("\n Enter correct choice:");
continue;
}
}
}

struct tree *create(struct tree *t)


{
int item;
while(1)
{
printf("Enter a element, enter -1 to exit:");
scanf("%d", &item);
if(item==-1)
{
return t; break;
}
if(t==NULL)
{
new1=(struct tree *) malloc(sizeof(struct tree));
new1->info=item;
new1->lchild=NULL;
new1->rchild=NULL;
t=new1;
}
else
insert(t,item);
}
}
struct tree *insert(struct tree *t,int item)
{
struct tree *temp=NULL,*ftemp=NULL;
new1=(struct tree *)malloc(sizeof(struct tree));
new1->lchild=NULL;
new1->rchild=NULL;
new1->info=item;
if(t==NULL)
{
t=new1;
return t;
}
temp=t;
while(temp!=NULL)
{
ftemp=temp;
if(temp->info > item)
temp=temp->lchild;
else
temp=temp->rchild;
}
if(ftemp->info >item)
ftemp->lchild=new1;
else
ftemp->rchild=new1;
return t;
}
void traverse(struct tree *t)
{
int ch;
printf(“\nchoices in traverse are:\n”);
printf("\n1.PREORDER\n2.INORDER\n3.POSTORDER\n");
printf("Enter your choice:");
scanf("%d", &ch);
switch(ch)
{
case 1: preorder(t); break;
case 2: inorder(t); break;
case 3: postorder(t); break;
}
}
void preorder(struct tree *t)
{
if(t!=NULL)
{
printf("%5d", t->info);
preorder(t->lchild);
preorder(t->rchild);
}
}
void inorder(struct tree *t)
{
if(t!=NULL)
{
inorder(t->lchild);
printf("%5d", t->info);
inorder(t->rchild);
}
}
void postorder(struct tree *t)
{
if (t!=NULL)
{
postorder(t->lchild);
postorder(t->rchild);
printf("%5d", t->info);
}
}
struct tree *delete1(struct tree *t)
{
struct tree *p,*fp,*rp,*frp;
int item;
p=fp=rp=frp=NULL;
printf("Enter element to delete;");
scanf("%d", &item);
p=t;

while(p->info != item && p!= NULL)


{
fp=p;
if(p->info > item)
p=p->lchild;
else
p=p->rchild;
}
if(p==NULL)
{
printf("\n Eleemtn doesnot exit\n");
return 0;
}
if(p->lchild==NULL)
frp=p->rchild;
else if(p->rchild==NULL)
frp=p->lchild;
else
{
rp=p->rchild;
while(rp->lchild!=NULL)
{
rp=rp->lchild;
}
rp->lchild=p->lchild;
frp=p->rchild;
}
if(fp==NULL)
{
return frp;
}
if(fp->lchild==p)
fp->lchild=frp;
else
fp->rchild=frp;
return t;
}

/* Program to implement LINEAR SEARCH using Arrays */

#include<stdio.h>
main()
{
int a[10],i,n;
clrscr();
printf("enter how many elements do u want to enter: ");
scanf("%d",&n);
printf("enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
lin_sea(a,n);
getch();
}
lin_sea(int a[],int n)
{
int i,j,key,pos,flag=1;
printf("enter which element to be searched: ");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
pos=i+1;
flag=1;
}
}
if(flag==0)
printf("search unsuccessful");
else
printf("search successful and it is found at %d position",pos);
}

/* Program to implement BINARY SEARCH using Arrays */

#include<stdio.h>
main()
{
int a[10],i,n;
clrscr();
printf("enter how many elements do u want to enter: ");
scanf("%d",&n);
printf("enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bin_sea(a,n);
getch();
}
bin_sea(int a[],int n)
{
int i,j,temp,low,high,mid,key,flag=1;
printf("\nArranging elements in ascending order: ");
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nelements in ascending order: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\nenter which element to be searched: ");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key<a[mid])
high=mid-1;
else if(key>a[mid])
low=mid+1;

else if(key==a[mid])
{
printf("\nsearch success\n");
printf("%d found at %d position\n",key,mid+1);
flag=0;
break;
}
}
if(flag)
{
printf("search unsuccessful");
}
}
/* Program to implement BINARY SEARCH using Recursion */

#include<stdio.h>
int key;
main()
{
int a[50],i,n,loc;
clrscr();
printf("enter the size of the array: ");
scanf("%d",&n);
printf("enter array elements in assending order: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nthe entered elements are: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\nenter the element to search: ");
scanf("%d",&key);
loc=rec_bin_sea(a,0,n-1);
if(loc==0)
printf("unsuccessful search,%d not found\n",key);
else
{
printf("\nsuccessful search : ");
printf("%d found at position %d\n",key,loc);
}
}
int rec_bin_sea(int b[],int low,int high)
{
int mid,p;
if(low<=high)
{
mid=(low+high)/2;
printf("mid element is %d ", mid);
if(key<b[mid])
{
high=mid-1;
p=rec_bin_sea(b,low,mid-1);
return p;
}
else if(key>b[mid])
{
low=mid+1;
p=rec_bin_sea(b,mid+1,high);
return p;
}

else if(key==b[mid])
{
printf("The searching element: %d",b[mid]);
return(mid + 1);
}
}
else
return(0);
}

/* Program to implement BUBBLE SORT */

#include<stdio.h>
main()
{
int a[10],i,n;
clrscr();
printf("enter how many elements do u want to enter: ");
scanf("%d",&n);
printf("enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bub_sort(a,n);
getch();
}
bub_sort(int a[],int n)
{
int i,j,temp,limit;
limit=n-1;
for(i=0;i<limit;i++)
{
for(j=0;j<limit-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("final sorted array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

/* Program to implement SELECTION SORT */

#include<stdio.h>
main()
{
int i,n,a[50];
clrscr();
printf("enter how many elements do u want to enter: ");
scanf("%d",&n);
printf("enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sel_sort(a,n);
getch();
}
sel_sort(int a[],int n)
{
int i,j,temp,pass,min;
for(pass=0;pass<n-1;pass++)
{
min=pass; /*assume that pass is the minimum index*/
for(j=pass+1;j<n;j++)
{
if(a[min]>a[j]) /*update min*/
min=j;
}
if(min!=pass) /*swaping elements*/
{
temp=a[pass];
a[pass]=a[min];
a[min]=temp;
}
}
printf("final sorted array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

/* Program to implement INSERTION SORT */

#include<stdio.h>
main()
{
int a[20],i,n;
clrscr();
printf("enter how many elements do u want to enter: ");
scanf("%d",&n);
printf("enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ins_sort(a,n);
getch();
}
ins_sort(int a[],int n)
{
int i,j,k;
for(j=1;j<n;j++)
{
k=a[j];
for(i=j-1;i>=0&&k<a[i];i--)
{
a[i+1]=a[i];
}
a[i+1]=k;
printf("element %d inserted in appropriate location",k);
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
printf("final sorted array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

/* Program to implement MERGE SORT */

#include<stdio.h>
main()
{
int a[10],m,n,b[10],p;
static int i;
clrscr();
printf("enter the size of the first array: ");
scanf("%d",&m);
printf("enter the elements into thae array: ");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
printf("enter the size of the second array: ");
scanf("%d",&n);
printf("enter the elements into the array: ");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
merge_sort(a,b,m,n);
getch();
}
merge_sort(int a[],int b[],int m,int n)
{
int c[25],p;
static int i,j,k;
i=0;
j=0;
while(i<m && j<n)
{
if(a[i]<b[j])
{
c[k]=a[i];
k++;
i++;
}
else if(a[i]>b[j])
{
c[k]=b[j];
k++;
j++;
}
else
{
c[k]=a[i];
i++;
k++;
}
}

if(i<m)
{
for(p=i;p<m;p++)
{
c[k]=a[i];
k++;
i++;
}
}
else if(j<n)
{
for(p=j;p<n;p++)
{
c[k]=b[j];
k++;
j++;
}
}
printf("\nAfter sorting the elements are: ");
for(i=0;i<k;i++)
printf("%d ",c[i]);
}

/* Program to implement QUICK SORT */

#include<stdio.h>
int n;
main()
{
int a[20],i,l,r;
void qsort();
clrscr();
printf("enter how many elements do u want to enter: ");
scanf("%d",&n);
printf("enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
l=0;
r=n-1;
qsort(a,l,r);
printf("final sorted array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
void qsort(int b[],int left,int right)
{
int i,j,k,p,temp,finished;
if(right>left)
{
i=left;
j=right;
p=b[left];
printf("\n P,the partioning element is: %d\n",p);
finished=0;
while(!finished)
{
do
{
++i;
}while((b[i]<=p)&&(i<=right));
while((b[j]>=p)&&(j>left))
{
--j;
}
if(j<i)
finished=1;
else
{
printf("\nswapping %d and %d. \n",b[i],b[j]);
temp=b[i];
b[i]=b[j];
b[j]=temp;

for(k=0;k<n;k++)
printf("%5d",b[k]);
printf("\n");
}
}
printf("\n I is greater than J,");
printf("so b[left] and b[j] are swapped.");
printf("Swapping %d and %d.\n\n",b[left],b[j]);
temp=b[left];
b[left]=b[j];
b[j]=temp;
for(k=0;k<n;k++)
printf("%5d",b[k]);
printf("\n");
printf("\nsub file one is : elements %d to %d.\n",left,j-1);
qsort(b,left,j-1);
printf("\nsub file two is : elements %d to %d.\n",i,right);
qsort(b,i,right);
}
else
{
if(right!=left)
printf("Right (%d) is less than Left(%d).\n",right,left);
else
{
printf("Right (%d) is less than Left(%d).\n",right,left);
printf("subfile has only one element.\n");
}
}
return;
}
/* Program to implement HEAP SORT */
#include<stdio.h>
main()
{
int a[25],n,i;
printf("enter the size of the array: ");
scanf("%d",&n);
printf("enter %d elements into the array: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
heap(a,i,a[i]);
}
heapsort(a,n);
printf("sorted elements are: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
heap(int a[],int pos,int k)
{
int f;
f=(pos-1)/2;
while(pos>0 && k>a[f])
{
a[pos]=a[f];
pos=f;
f=(pos-1)/2;
}
a[pos]=k;
}
heapsort(int a[],int n)
{
int k,key,i,j;
for(i=n-1;i>0;i--)
{
key=a[0];
a[0]=a[i];
a[i]=key;
for(j=0;j<i;j++)
{
heap(a,j,a[j]);
}
}
}
OUTPUT:
enter the size of the array: 5
enter 5 elements into the array: 34 12 65 46 24
sorted elements are: 12 24 34 46 65

S-ar putea să vă placă și