Vous êtes sur la page 1sur 35

/* HEAP SORT */

#include<stdio.h>

#include<conio.h>

void swap(int *x,int *y)

int temp;

temp=*x;

*x = *y;

*y = temp;

void heapsort(int a[],int n)

int i,s,f;

for(i=1;i< n;++i)

s=i;

f=(s-1)/2;

while( a[f]< a[s])

swap(&a[f],&a[s]);

s=f;

if(s==0)
break;

f=(s-1)/2;

for(i=n-1;i>=1;--i)

swap(&a[0],&a[i]);

f=0;

s=1;

if(i==1)

break;

if(i>2)if(a[2]>a[1])s=2;

while( a[f]< a[s] )

swap(&a[f],&a[s]);

f=s;

s=2*f+1;

if(i>s+1 )if(a[s+1]>a[s])s=s+1;

if (s>=i)

break;

}
void main()

int a[100],n,i;

clrscr();

printf("\t\tHEAP SORT\n");

printf("\nEnter The Number Of Elements\t: ");

scanf("%d",&n);

printf("\nEnter Elements\n");

for(i=0;i< n;++i)

scanf("%d",&a[i]);

heapsort(a,n);

printf("\n\t\t\tSorted List\n");

for(i=0;i< n;++i)

printf("\t%d",a[i]);

getche();

/* Program of queue using linked list*/

#include<stdio.h>

#include<malloc.h>

struct node
{

int info;

struct node *link;

}*front=NULL,*rear=NULL;

void main()

int choice;

clrscr();

while(1)

printf("QUEUE USING LINKED LIST\n");

printf("1.Insert\n");

printf("2.Delete\n");

printf("3.Display\n");

printf("4.Quit\n");

printf("Enter your choice : ");

scanf("%d", &choice);

switch(choice)

case 1:

insert();

break;

case 2:
del();

break;

case 3:

display();

break;

case 4:

exit(1);

default :

printf("Wrong choice\n");

}/*End of switch*/

}/*End of while*/

}/*End of main()*/

insert()

struct node *tmp;

int added_item;

tmp = (struct node *)malloc(sizeof(struct node));

printf("Input the element for adding in queue : ");

scanf("%d",&added_item);

tmp->info = added_item;

tmp->link=NULL;

if(front==NULL) /*If Queue is empty*/

front=tmp;

else
rear->link=tmp;

rear=tmp;

}/*End of insert()*/

del()

struct node *tmp;

if(front == NULL)

printf("Queue Underflow\n");

else

tmp=front;

printf("Deleted element is %d\n",tmp->info);

front=front->link;

free(tmp);

}/*End of del()*/

display()

struct node *ptr;

ptr = front;

if(front == NULL)

printf("Queue is empty\n");

else
{

printf("Queue elements :\n");

while(ptr != NULL)

printf("%d ",ptr->info);

ptr = ptr->link;

printf("\n");

}/*End of else*/

}/*End of display()*/

QUICKSORT

#include<stdio.h>
#include<conio.h>

void quicksort(int [],int,int);


int partition(int [],int,int);

main()
{
int a[20],p,q,i,n;
clrscr();

printf("Enter The number Of Elements\t: ");


scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

p=0;
q=n-1;
printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
quicksort(a,p,q);

printf("\nArray After Sorting : ");


for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}

void quicksort(int a[],int p,int q)


{
int j;
if(p< q)
{
j=partition(a,p,q+1);
quicksort(a,p,j-1);
quicksort(a,j+1,q);
}
}

int partition(int a[],int m,int p)


{
int v,i,j;
int temp;
v=a[m];
i=m;j=p;
do
{
do
{
i += 1;
}
while(a[i]< v);
do
{
j -= 1;
}
while(a[j]>v);

if(i< j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
while(i< j);
a[m] =a[j];
a[j] = v;
return j;
}
STACK USING ARRAY

#include<stdio.h>

#include<conio.h>

#define size 5

char stack[size][15],ele[20];

int tos;

void push();

char* pop();

void show();

int isempty();

int isfull();

void main()

int choice;

clrscr();

tos=0;

do

printf("*********STACK USING ARRAY*******\n");

printf("1.push\n 2.pop\n 3.show\n 4.exit\n");

scanf("%d",&choice);

switch(choice)

{
case 1:

if (isfull())

printf("\nThe stack is full");

else

printf("\n Enter element to insert");

scanf("%s",ele);

push(ele);

break;

case 2:

if(isempty())

printf("\n The stack is empty");

else

printf("\nThe removed element is:%s",pop());

break;

case 3:

if(isempty())

printf("\nThe stack is empty, I cant show you any elements");

else

show();

break;

case 4:

exit(1);

default:
printf("\nDo you understand english and numbers??");

}while(1);

int isempty()

return(tos==0);

int isfull()

return(tos==size);

void push(ele)

strcpy(stack[tos],ele);

tos++;

char* pop()

tos--;

return(stack[tos]);

void show()

{
int x=tos;

printf("\nThe Stack elements are.....\n");

while(x!=0)

printf("\t%s\n",stack[--x]);

STACK USING LINKED LIST

#include<stdio.h>

#include<malloc.h>

#define maxsize 10

void push();

void pop();

void display();

struct node

int info;

struct node *link;

}*start=NULL, *new,*temp,*p;

typedef struct node N;


void main()

int ch,a;

clrscr();

do

printf("\t\t\tLinked stack");

printf("\n 1.Push");

printf("\n 2.Pop");

printf("\n 3.Display");

printf("\n 4.Exit");

printf("\n Enter your choice : ");

scanf("%d",&ch);

switch(ch)

case 1:

push();

break;

case 2:
pop();

break;

case 3:

display();

break;

case 4:

exit(0);

default:

printf("\nInvalid choice");

break;

while(ch<=3);

}
void push()

new=(N*)malloc(sizeof(N));

printf("\nEnter the item : ");

scanf("%d",&new->info);

new->link=NULL;

if(start==NULL)

start=new;

else

p=start;

while(p->link!=NULL)

p=p->link;

p->link=new;

getch();

}
void pop()

if(start==NULL)

printf("\nStack is empty");

else if(start->link==NULL)

printf("\nThe deleted element is : %d",start->info);

free(start);

start=NULL;

else

p=start;

while(p->link!=NULL)

temp=p;
p=p->link;

printf("\nDeleted element is : %d\n", p->info);

temp->link=NULL;

free(p);

void display()

if(start==NULL)

printf("\nStack is empty");

else

printf("\nThe elements are : ");

p=start;

while(p!=NULL)
{

printf("\t%d",p->info);

p=p->link;

printf("\n");

QUEUE USING ARRAY

# include <stdio.h>
# include <conio.h>
# define SIZE 10
int arr[SIZE], front = -1, rear = -1, i ;
void enqueue() ;
void dequeue() ;
void display() ;
void main()
{

int ch ;

clrscr() ;

do

{
printf("QUEUE USING ARRAY\n");
printf("[1].ENQUEUE\n [2].DEQUEUE\n [3].Display\n [4].Exit\n") ;

printf("Enter your choice [1-4] : ") ;

scanf("%d", &ch) ;

switch(ch)

{
case 1 :

enqueue() ;

break ;

case 2 :

dequeue() ;

break ;

case 3 :

display() ;

break ;

case 4 :

break ;

default :

printf("Invalid option") ;

getch() ;

} while(ch != 4) ;

getch() ;

void enqueue()

if(rear == SIZE - 1)

printf("Queue is full (overflow)") ;

getch() ;

return ;

rear++ ;

printf("Enter the element to ENQUEUE : ") ;


scanf("%d", &arr[rear]) ;

if(front == -1)

front++ ;

void dequeue()

if(front == -1)

printf("Queue is empty (underflow)") ;

getch() ;

return ;

printf("The DEQUEUE element is : %d", arr[front]) ;

getch() ;

if(front == rear)

front = rear = -1 ;

else

front++ ;

void display()

if(front == -1)

printf("Queue is empty (underflow)") ;

getch() ;

return ;

}
printf("The elements in queue are :FRONT ->") ;

for(i = front ; i <= rear ; i++)

printf(" ... %d", arr[i]) ;

printf(" ... <- REAR") ;

getch() ;

BALANCED PARENTHESIS

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include"stack.c"
void main()
{
char expr[20];
char ch;
int i,n;
clrscr();
printf("Enter the expression");
scanf("%s",expr);
n=strlen(expr);
for(i=0;i<n;i++)
{
if(expr[i]=='(')
push(expr[i]);
if(expr[i]==')')
pop();
}
if(top==-1)
printf("Balanced expression");
else
printf("Unbalanced expression");
getch();
}

INFIX TO POSTFIX

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
char inf[40],post[40];
int top=0,st[20];
void postfix();
void push(int);
char pop();
void main()
{
clrscr();
printf("\n Enter infix expression:");
scanf("%s",&inf);
postfix();
getch();
}
void postfix()
{
int i,j=0;
for(i=0;inf[i]!='\0';i++)
{
switch(inf[i])
{
case '+':
while(st[top]>=1)
post[j++]=pop();
push(1);
break;
case '-':
while(st[top]>=1)
post[j++]=pop();
push(2);
break;
case '*':
while(st[top]>=3)
post[j++]=pop();
push(3);
break;
case '/':
while(st[top]>=3)
post[j++]=pop();
push(4);
break;
case '^':
while(st[top]>=4)
post[j++]=pop();
push(5);
break;
case '(':
push(0);
break;
case ')':
while(st[top]!=0)
post[j++]=pop();
top--;
break;
default:
post[j++]=inf[i];
}
}
while(top>0)
post[j++]=pop();
printf("\n\t Postfix expression is %s",post);
}
void push(int ele)
{
top++;
st[top]=ele;
}
char pop()
{
int el;
char e;
el=st[top];
top--;
switch(el)
{
case 1:
e='+';
break;
case 2:
e='-';
break;
case 3:
e='*';
break;
case 4:
e='/';
break;
case 5:
e='^';
break;
}
return(e);
}

LINKED LIST USING ARRAY

#include<stdio.h>
#include<conio.h>
#define max 100
struct list
{
int a[max];
int last;
}s;
void main()
{
int x,p,loc,c;
void insert(int,int);
int locate(int);
void delete(int);
clrscr();
s.last==0;
while(1)
{
printf("LIST USING ARRAY \n");
printf("\n.1.INSERT\n 2.locate\n3.DELETE\n4.EXIT\n");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter the elements & It's Location\n");
scanf("%d%d",&x,&p);
insert(x,p);
break;
case 2:
printf("Enter elements to be located\n");
scanf("%d",&x);
loc=locate(x);
if(loc==0)
printf("Elements not found");
else
printf("location of x--%d\n",loc);
break;
case 3:
printf("Enter position to delete elements\n");
scanf("%d",&p);
delete(p);
break;
case 4:
exit(0);
getch();
}
}
}
void insert(int x,int p)
{
int q;
if(s.last>=max)
{
printf("LIST FULL");
}
else if((p>s.last+1)||(p<1))
printf("position does not exist");
else
{
for(q=s.last;q>=p;q--)
s.a[q+1]=s.a[q];
s.last=s.last+1;
s.a[p]=x;
}
}
int locate(int x)
{
int q;
for(q-1;q<=s.last;q++)
{
if(s.a[q]==x)
return q;
}
return(0);
}
void delete(int p)
{
int q;
if((p>s.last)||(p>1))
{
printf("position does not exist");
}
else
{
s.last=s.last-1;
for(q=p;q<=s.last;q++)
s.a[q]=s.a[q+1];
}
}
Binary Search Tree

/* C Program on binary Search Tree */

#include< stdio.h>
#include< conio.h>
#include< string.h>
#include< dos.h>
#include< stdlib.h>

struct node
{
char data[15];
struct node *left,*right;
};

void insert(struct node *r,struct node *p)


{
if((r->right==NULL)&&(strcmp(p->data,r->data)>0))
r->right=p;
else if((r->right!=NULL)&&(strcmp(p->data,r->data)>0))
insert(r->right,p);
if((r->left==NULL)&&(strcmp(p->data,r->data)< 0))
r->left=p;
else if((r->left!=NULL)&&(strcmp(p->data,r->data)< 0))
insert(r->left,p);

void tree(struct node *r,int c)


{
int top,flag;
struct node *w,*stack[20];
if(r!=NULL)
{
if(c!=4)
{
if(c == 1)
printf(" %s ",r->data);
tree(r->left,c);
if(c == 2)
printf(" %s ",r->data);
tree(r->right,c);
if(c == 3)
printf(" %s ",r->data);
}
}
if(c == 4)
{
top = 0;
w = r;
flag = 0;
while((top != -1)&&(w!=NULL))
{
while((flag == 0) && (w->left!=NULL))
{
stack[top] = w;
top++;
w = w->left;
}
printf(" %s ",w->data);
if(w->right != NULL)
{
w = w->right;
flag = 0;
}
else
{
top--;
w = stack[top];
flag = 1;
}
}
}
}
void main()
{
int choice,c,i,flag;
char temp='N',temp1[15];
struct node *s,*root,*r,*q;
root = NULL;
do
{
clrscr();
printf("\n 1. Enter");
printf("\n 2. Delete ");
printf("\n 3. Search ");
printf("\n 4. Display");
printf("\n 5. Exit");
printf("\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("***** Data Entry ***** ");
do
{
s=malloc(sizeof(struct node));
s->left=NULL;
s->right=NULL;
printf("\nEnter Data : ");
scanf("%s",&s->data);
if(root==NULL)
root=s;
else
insert(root,s);
printf("\nEnter Your Elements[y/n] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;

case 2:printf("****** Delete Operation *******\n");


do
{
printf("\nEnter Element To Be Deleted : ");
scanf("%s",temp1);
s=root;i=0;flag=0;
do
{
if(strcmp(s->data,temp1)>0)
{
r=s;
s=s->left;
i=2;
}
if(strcmp(s->data,temp1)==0)
{
flag=1;
if(i==0)
{
if(root->right!=NULL)
{
q=root->left;
root=root->right;
insert(root,q);
}
if(root->right==NULL)
root=root->left;
}
else
{
if(i==1)
{
q=s->left;
r->right=s->right;
if(s->left!=NULL)
insert(r,q);
}
if(i==2)
{
q=s->right;
r->left=s->left;
if(s->right!=NULL)
insert(r,q);
}
}
}
}
while(flag==0&&s!=NULL);
printf("\n Delete Any More[Y/N] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;
case 3:printf("****** Search Operation *******\n");
do
{
printf("\n Enter Name To Be Searched");
scanf("%s",temp1);
i=0;
s=root;
while(s!=NULL&&i==0)
{
if(strcmp(s->data,temp1)< 0)
s=s->right;
if(strcmp(s->data,temp1)>0)
s=s->left;
if(strcmp(s->data,temp1)==0)
i=1;
}
if(i==0)
printf("\nElement Not Found\n");
else
printf("\nElement Found\n");
printf("\nEnter More Elements[Y/N] : ");
scanf("%c",&temp);
}
while(temp=='y');
break;
case 4:clrscr();
do
{
clrscr();
printf("\n 1. Preorder\n 2. Inorder \n 3. Postorder \n 4. Non Recursion \n 5. Exit");
printf("\nEnter Your Choice : ");
scanf("%d",&c);
if(root==NULL)
printf("Tree Not Started Yet");
else
tree(root,c);
printf("\n Press Any Key To Continue......");
getch();
}
while(c!=5);
break;
}
}
while(choice!=5);
}
/****************** OUTPUT ****************

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 1
/****** Data Entry *******

Enter Data : Lionel


Enter More Elements(Y/N) : y
Enter Data : Dylan
Enter More Elements(Y/N) : y
Enter Data : Daniel
Enter More Elements(Y/N) : y
Enter Data : Malcolm
Enter More Elements(Y/N) : y
Enter Data : Cyril
Enter More Elements(Y/N) : y
Enter Data : Jason
Enter More Elements(Y/N) : n

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 4

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 1
Lionel Dylan Malcolm Cyril Daniel Jason

Press Any Key to Contiue....

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 2
Malcolm Cyril Dylan Lionel Jason Daniel

Press Any Key to Contiue....

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 3
Cyril Malcolm Dylan Lionel Jason Daniel

Press Any Key to Contiue....

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 4
Malcolm Cyril Dylan Lionel Jason Daniel

Press Any Key to Contiue....

1. Preorder
2. Inorder
3. Postorder
4. Non Recursion
5. Exit
Enter Your Choice : 5

Press Any Key To Continue.....

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 3

Enter Name To Be Searched : Dylan


Element Found

Enter More Elements(Y/N) : n


1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 2

****** Delete Operation *******


Enter Element To Be Deleted : Dylan
Delete Any More(Y/n) : n

1. Enter
2. Delete
3. Search
4. Display
5. Exit
Enter Your Choice : 5

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i;
struct node
{
int data;
struct node *next;
}*head,*temp,*temp1;
void main()
{
int ch,p,c,n;
clrscr();
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
while(1)
{
printf("\n1.create2.display3.make null4.insert5.delete6.locate7.exit\n");
printf("\n enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1;
printf("\nenter element");
if(head->next==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf("%d",&temp->data);
head->next=temp;
temp->next=NULL;
}
else
{
temp1=(struct node*)malloc(sizeof(struct node));
scanf("%d",&temp1->data);
temp->next=temp1;
temp1->next=NULL;
temp=temp1;
}
break;
case 2:
temp=head;
if((temp->next)==NULL)
printf("\n list empty\n");
else
{
printf("\t\t\n\n elements in list\t\n\n");
while(temp->next!=NULL)
{
printf("->%d",temp->next->data);
temp=temp->next;
}
}
break;
case 3:
temp=head;
temp->next=NULL;
break;
case 4:
temp1=(struct node*)malloc(sizeof(struct node));
printf("\n to insert 1.first2.last3.anywhere\n");
scanf("%d",&c);
printf("enter element to be inserted\n");
scanf("%d",&temp1->data);
switch(c)
{
case 1:
temp1->next=head->next;
head->next=temp1;
break;
case 2:
temp=head->next;
while(temp->next!=NULL)
temp=temp->next;
temp->next=temp1;
temp1->next=NULL;
break;
case 3:
printf("enter position\n");
scanf("%d",&p);
if(p==1)
{
temp1->next=head->next;
head->next=temp1;
}
else
{
temp=head->next;
for(i=1;i<p-1;i++)
temp=temp->next;
temp1->next=temp->next;
temp->next=temp1;
}
}break;
case 5:
temp=head ;
printf("enter position to be deleted ");
scanf("%d",&p);
for(i=1;i<p;i++)
temp=temp->next;
temp->next=temp->next->next;
break;
case 6:
temp=head->next;
printf("enter element to be located\n");
scanf("%d",&n);
for(i=1;temp->data!=n;i++)
temp=temp->next;
if(temp->data==n)
printf("element is at position %d\n",i);
else
printf("elements not found\n");
break;
case 7:
exit(0);
}
}
getch();
}

Vous aimerez peut-être aussi